2016-07-13 142 views
0

我正在遍历设置范围中的单个列的行。我将范围设置为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** 

回答

3

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

您从未声明SystemCol作为范围,也不是rw作为范围。

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

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

谢谢!你的答案完美无缺。 – TroyPilewski

+1

我会达到时间限制 – TroyPilewski