Excel VBA loop through visible filtered rows

Joao Santos

I have a excel table with a autofilter.

In the filtered table i only have few rows filtered.

My objective is icterate all visible rows to colect data to copy to anothe sheet.

I want a way to collect a variable with the the fisrt visible row number.

my draft code is:

Dim cnp As String
Dim nome As String
Dim filter_rng As Range
Dim rw As Range
Dim last_row As Long 'last visible data row
Dim dest_row As Long 'row to paste the colected data

Set filter_rng = Range("A5:Y" & last_row).Rows.SpecialCells(xlCellTypeVisible)
'collect data
For Each rw In filter_rng.SpecialCells(xlCellTypeVisible)
    workshett(1).Activate
    cnp = Range("a" & rw).Value
    nome = Range("b" & rw).Value

'copy data to another worksheet first data line is cell A2
    Worksheet(2).Activate
    Range("A" & dest_row + 1).Value = cnp
    Range("b" & dest_row + 1).Value = nome

Next rw
mtholen

Your code contains several errors and you provide little additional information to allow us to help you, but to put in an attempt.

Please see below code and compare to yours, the below code is closest to what you are trying to do and is tested and working.

Dim cnp As String
Dim nome As String
Dim filter_rng As Range
Dim rw As Range
Dim last_row As Long 'last visible data row
Dim dest_row As Long 'row to paste the colected data

last_row = 200
dest_row = 1

Set filter_rng = Sheets(1).Range("A5:Y" & last_row)

'collect data
For Each rw In filter_rng.SpecialCells(xlCellTypeVisible)
    'Worksheets(1).Activate
    cnp = Sheets(1).Range("A" & rw.Row).Value
    nome = Sheets(1).Range("B" & rw.Row).Value

'copy data to another worksheet first data line is cell A2
    'Worksheets(2).Activate
    Sheets(2).Range("A" & dest_row + 1).Value = cnp
    Sheets(2).Range("B" & dest_row + 1).Value = nome

Next rw

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel VBA loop through visible filtered rows

From Dev

Loop through rows and columns Excel Macro VBA

From Dev

Copying visible/filtered rows efficiently in excel

From Dev

VBA: Need to loop only through visible cells in Excel

From Dev

Excel VBA - Have VBA script loop through rows

From Dev

MS Excel VBA - Loop Through Rows and Columns (Skip if Null)

From Dev

How to properly count visible rows in VBA Excel?

From Dev

VBA Excel, loop through variables

From Dev

Excel/VBA - Loop through range

From Dev

Loop through formula Excel VBA

From Dev

Excel VBA - Loop through recordset

From Dev

Excel VBA loop through listbox

From Dev

Apply formula in visible rows that is filtered

From Dev

Excel VBA Macro - Looping through a column of a filtered table

From Dev

Select and copy a specific number of filtered rows using VBA in excel

From Dev

Excel VBA - Need to loop through delete entire 3 rows above row where cell value <> 0

From Dev

Loop through Cols & Rows with IF statement vba

From Dev

Loop through rows and compare values in Excel

From Dev

How can I loop through rows in excel using VBA if rows are sorted so row numbers aren't consecutive

From Dev

VBA Excel Loop through Specific Tables to Format

From Dev

Excel VBA loop through Pivot Items

From Dev

loop through a multidimensional array in vba excel

From Dev

Excel VBA to loop through Worksheets on a series of Workbooks

From Dev

Access VBa - loop through Excel columns

From Dev

Loop through each excel sheet using vba

From Dev

Excel VBA Loop Through Named Ranges

From Dev

When a excel column is filtered by a button, my rows should be updated. The update is about inserting values into visible cells

From Dev

Excel VBA Concatenate only visible cells of filtered column. Test code included

From Dev

Excel VBA - Stops Selecting Visible Rows on Filter after many interations

Related Related

  1. 1

    Excel VBA loop through visible filtered rows

  2. 2

    Loop through rows and columns Excel Macro VBA

  3. 3

    Copying visible/filtered rows efficiently in excel

  4. 4

    VBA: Need to loop only through visible cells in Excel

  5. 5

    Excel VBA - Have VBA script loop through rows

  6. 6

    MS Excel VBA - Loop Through Rows and Columns (Skip if Null)

  7. 7

    How to properly count visible rows in VBA Excel?

  8. 8

    VBA Excel, loop through variables

  9. 9

    Excel/VBA - Loop through range

  10. 10

    Loop through formula Excel VBA

  11. 11

    Excel VBA - Loop through recordset

  12. 12

    Excel VBA loop through listbox

  13. 13

    Apply formula in visible rows that is filtered

  14. 14

    Excel VBA Macro - Looping through a column of a filtered table

  15. 15

    Select and copy a specific number of filtered rows using VBA in excel

  16. 16

    Excel VBA - Need to loop through delete entire 3 rows above row where cell value <> 0

  17. 17

    Loop through Cols & Rows with IF statement vba

  18. 18

    Loop through rows and compare values in Excel

  19. 19

    How can I loop through rows in excel using VBA if rows are sorted so row numbers aren't consecutive

  20. 20

    VBA Excel Loop through Specific Tables to Format

  21. 21

    Excel VBA loop through Pivot Items

  22. 22

    loop through a multidimensional array in vba excel

  23. 23

    Excel VBA to loop through Worksheets on a series of Workbooks

  24. 24

    Access VBa - loop through Excel columns

  25. 25

    Loop through each excel sheet using vba

  26. 26

    Excel VBA Loop Through Named Ranges

  27. 27

    When a excel column is filtered by a button, my rows should be updated. The update is about inserting values into visible cells

  28. 28

    Excel VBA Concatenate only visible cells of filtered column. Test code included

  29. 29

    Excel VBA - Stops Selecting Visible Rows on Filter after many interations

HotTag

Archive