Vba 在集合范围内循环遍历单个列的行
我试图在集合范围内遍历单个列的行。我将范围设置为WorkingRange,然后将所需的列设置为SystemCol。如何循环set列中的每个元素?我想为所选列中具有值的每一行显示一个消息框。代码中带**的区域是我尝试插入代码的地方,但我得到的是完整的列地址,而不是单个单元格地址Vba 在集合范围内循环遍历单个列的行,vba,excel,excel-2010,Vba,Excel,Excel 2010,我试图在集合范围内遍历单个列的行。我将范围设置为WorkingRange,然后将所需的列设置为SystemCol。如何循环set列中的每个元素?我想为所选列中具有值的每一行显示一个消息框。代码中带**的区域是我尝试插入代码的地方,但我得到的是完整的列地址,而不是单个单元格地址 '=============================================================================================== 'Description:
'===============================================================================================
'Description: Loops through the selected site and adds in the vulnerability totals for each _
systems
'Originally written by: Troy Pilewski
'Date: 2016-06-30
'===============================================================================================
'Declares variables
Dim ToWorkbook As Workbook, FromWorkbook As Workbook
Dim ToWorksheet As Worksheet, FromWorkSheet As Worksheet
Dim WorkingRange As Range, WholeRange As Range
Dim FromWorkbookVarient As Variant, ShipNameList() As Variant
Dim TitleString As String, FilterName As String, CurrentSystemName As String, _
ShipNames() As String, SelectedShipName As String
Dim LastRow As Long, ShipRow As Long
Dim StartRow As Integer
Const RowMultiplyer As Integer = 47
'-----------------------------------------------------------------------------------------------
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set ToWorkbook = ActiveWorkbook
Set ToWorksheet = ToWorkbook.ActiveSheet
LastRow = ToWorksheet.Range("Y:Y").Find( _
What:="*", _
After:=ToWorksheet.Range("Y1"), _
LookAt:=xlByRows, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious _
).Row
'MsgBox _
' Prompt:="Y1:Y" & LastRow, _
' Title:="Ship Range"
ShipNameList = ToWorksheet.Range("Y1:Y" & LastRow).Value
For Each Item In ShipNameList
Dim BoundCounter As Integer
If Left(Item, 3) = "USS" Then
BoundCounter = BoundCounter + 1
End If
Next Item
ReDim ShipNames(BoundCounter - 1)
BoundCounter = 0
For Each Item In ShipNameList
If Left(Item, 3) = "USS" Then
ShipNames(BoundCounter) = Item
' Debug.Print ShipNames(BoundCounter)
BoundCounter = BoundCoutner + 1
Else
' Debug.Print UBound(ShipNames())
Exit For
End If
Next Item
TitleString = "Select a ship..."
SelectedShipName = GetChoiceFromChooserForm(ShipNames, TitleString)
If SelectedShipName = "" Then
Exit Sub
End If
ShipRow = ToWorksheet.Range("Y:Y").Find( _
What:=SelectedShipName, _
After:=ToWorksheet.Range("Y1"), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True _
).Row
'Debug.Print ShipRow
StartRow = 14
If ShipRow > 1 Then
StartRow = (RowMultiplyer * (ShipRow - 1)) + StartRow
Else
StartRow = 14
End If
Set WorkingRange = ToWorksheet.Range("B" & StartRow & ":G" & StartRow + 38)
Set SystemCol = WorkingRange.Columns(2)
'Debug.Print WorkingRange.Address
FilterName = "Excel Files (*.xls), *.xls,Excel Files (*.xlsx), *.xlsx,All Files (*.*), *.*"
TitleString = "Scan File Selection"
**For Each rw In SystemCol
Debug.Print rw.Address
Next rw**
您最好在代码模块的顶部添加
Option Explicit
,以确保所有变量都必须声明
您从未将SystemCol
声明为范围,也从未将rw
声明为范围
然后,将.Cells
添加到循环中的SystemCol
,可以确保循环通过SystemCol中的每个单元格。见下文
For Each rw In SystemCol.Cells
Debug.Print rw.Address
Next rw
您最好在代码模块的顶部添加
Option Explicit
,以确保所有变量都必须声明
您从未将SystemCol
声明为范围,也从未将rw
声明为范围
然后,将.Cells
添加到循环中的SystemCol
,可以确保循环通过SystemCol中的每个单元格。见下文
For Each rw In SystemCol.Cells
Debug.Print rw.Address
Next rw
当到达时间限制时我会的