简体   繁体   中英

VBA- Need to loop through only visible cells

Trying to copy specific cells only from filtered rows, but this part of code keeps copying all data even from hidden cells.

Sheets(Wss).Range("A1").AutoFilter _
     Field:=8, _
      Criteria1:="Vesztesg", _
       VisibleDropDown:=False
 For j = 2 To Sheets(Wss).Range("A2").End(xlDown).End(xlDown).End(xlUp).Row - 1
     Sheets("EBS Posting template").Range("C" & i) = Sheets(Wss).Range("E" & j)
     Sheets("EBS Posting template").Range("O" & i + 1) = Sheets(Wss).Range("F" & j)
     Sheets("EBS Posting template").Range("G" & i + 2) = Sheets(Wss).Range("J" & j)
          i = i + 3
      Next j

If you are using a counter to the rows, even if the row is hidden, it is still going to give you that row value. You would require additional lines to make sure that row was not hidden.

Possibly just looping through the visible cells would suffice.

By the way: I am not sure where your values are ending up, you will have to edit that in your code. 在此处输入图片说明

The code will loop through the visible cells in column E, check the sheet name in the code as well, I wasn't sure about that either.

Sub Button2_Click()
    Dim wss As Worksheet, sh As Worksheet, LstRw As Long, rng As Range, c As Range, Lr As Long

    Set wss = Sheets("Sheets(Wss)")
    Set sh = Sheets("EBS Posting template")

    With wss
        LstRw = .Cells(.Rows.Count, 8).End(xlUp).Row
        .Range("A1").AutoFilter Field:=8, Criteria1:="Vesztesg"
        Set rng = .Range("E2:E" & LstRw).SpecialCells(xlCellTypeVisible)

        For Each c In rng.Cells
            With sh
                Lr = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
                .Cells(Lr, "C").Value = c
                .Cells(Lr, "O").Value = c.Offset(, 1)
                .Cells(Lr, "G").Value = c.Offset(, 5)
            End With
        Next c
        .AutoFilterMode = False
    End With

End Sub

To loop through visible cells only, you need to loop through a range. So instead of For i loop we use For Each that will let you do that. We add .SpecialCells(xlCellTypeVisible) to that range.

For each element in the range we declare a variable, cl . which we can use to extract data from.

Sheets(Wss).Range("A1").AutoFilter _
     Field:=8, _
      Criteria1:="Vesztesg", _
       VisibleDropDown:=False

Dim cl As Range
For Each cl In Range(Cells(2, 1), Cells(Sheets(Wss).Range("A2").End(xlDown).End(xlDown).End(xlUp).Row - 1, 1)).SpecialCells(xlCellTypeVisible) 'Apply visible cells only
     j = cl.Row 'row number of current cl value.
     Sheets("EBS Posting template").Range("C" & i) = Sheets(Wss).Range("E" & j)
     Sheets("EBS Posting template").Range("O" & i + 1) = Sheets(Wss).Range("F" & j)
     Sheets("EBS Posting template").Range("G" & i + 2) = Sheets(Wss).Range("J" & j)
          i = i + 3
Next cl 'loop to next cl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM