我试图遍历Excel工作表中的可见行(在应用过滤器之后),并显示这些可见行中某一列中的所有单元格。我有一个for循环,但是我希望避免包含标题的第一行打印在我的循环中。
为此,我插入了一个if条件,该条件将排除标题的字符串。但是,VBA编译器无法识别我的If块。我相信我的语法是正确的。我该如何解决?
Public Sub iterate_factIDs()
'https://social.msdn.microsoft.com/Forums/office/en-US/79f9d96d-1ec8-48e6-
b7dd-e11901a5c72b/looping-through-excel-filtered-range?forum=exceldev
Sheets("GL").Select
Dim rngC As Range
Dim ws As Worksheet
Set ws = ActiveSheet
For Each rngC In Intersect(ws.UsedRange,
ws.Range("B:B").SpecialCells(xlCellTypeVisible))
'this is the if condition that is not recognized
If rngC.Row <> 1 Then:
'MsgBox ("Found Row: " & rngC.Row)
'want to display all the numeric values in Column 2 after filter
'this also displays the title of the column which is in row 1 column B
MsgBox ("FactID: " & Cells(rngC.Row, 2))
End If
Next rngC
End Sub
即使我的代码中包含If-Then语句,编译器也会返回“ End If Without Block If”。