在过滤的工作表中打印每行的多个单元格值
我有以下循环遍历一个过滤的Excel表单
Sub SpecialLoop() Dim rng As Range Set rng = Range("A2:A11") For Each cl In rng.SpecialCells(xlCellTypeVisible) Debug.Print cl Next cl End Sub
所以这打印从A2到A11的所有值。 我怎么能在循环内打印B列中的相应值?
像这样的东西:
Sub SpecialLoop() Dim rng As Range, cl As Range On Error Resume Next Set rng = Range("A2:A11").SpecialCells(xlCellTypeVisible) On Error GoTo 0 If Not rng Is Nothing Then For Each cl In rng Debug.Print cl ' col A Debug.Print cl.Offset(, 1) ' col B 'alternative way 'Debug.Print Range("B" & cl.Row) ' col B Next cl End If End Sub
如果没有可见的单元格SpecialCells(xlCellTypeVisible)
触发错误,那就是为什么我使用On Error Resume Next
和If Not rng Is Nothing Then
。
至于你原来的问题,你可以使用Debug.Print cl.Offset(, 1)
从列B中获取值(假设cl
指向列A中的单元格)。 或者另一种方式,更明确的是: Debug.Print Range("B" & cl.Row)