2016-07-13 134 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