循环遍历“设置范围”中单个列的行

时间:2016-07-13 20:49:52

标签: vba excel-vba excel-2010 excel

我试图遍历设定范围内单个列的行。我将范围设置为WorkingRange,然后将我想要的列设置为SystemCol。如何循环设置列中的每个?我想为所选列中具有值的每一行显示一个消息框。带有**的代码区域是我尝试插入代码的地方,但我得到的是完整列地址而不是单个单元格地址。

'===============================================================================================
'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**

1 个答案:

答案 0 :(得分:3)

您可以很好地将Option Explicit添加到代码模块的顶部,以始终确保必须声明所有变量。

您从未将SystemCol声明为范围,也未将rw声明为范围。

在循环中将.Cells添加到SystemCol之后,确保您将遍历SystemCol中的每个单独的单元格。见下文。

For Each rw In SystemCol.Cells
    Debug.Print rw.Address
Next rw