VBA to loop filter through sheets and export data column to new worksheet

MikeyT_SE

New Member
Joined
Jun 14, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello friends! I am trying to create a macro and hitting some walls. The goal is to:
  • Loop through worksheets (# of sheets may vary)
    • Filter Column D for "Equipment" and "Grid"
    • Take resulting data from Column A on each worksheet (excluding header) and paste to a new worksheet or workbook
      • End result would be a CSV list of Column A results from all worksheets, duplicates removed
The challenge I'm facing is looping through sheets until the end, so the macro would work whether there were 2 worksheets vs 10 worksheets, and output a single CSV with all results...

Your help and wisdom are appreciated!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
There’s some detail missing from your question, such as:
  • Do you want the code to be located in the workbook that contains all the sheets you want to filter/copy?
  • Are “Equipment” and “Grid” the only words within the cells in column D or are they within strings?
  • Are your headers on row 1 & does your data start in row 2?
  • Do you want your new CSV to have headers?
  • Do you want the new CSV saved to the same folder as the original data file?
  • What do you want the new CSV file to be called?
I’ve made the following assumptions in the suggested code below:
  • You want the code located in the file that contains the original data
  • Your headers are in row 1 and your data starts in row 2
  • Your new CSV doesn’t have headers
  • You want the new CSV saved in the same folder as the original data file, and
  • I’ve used a generic name for the new file
You can change all these variables to suit your particular case.

VBA Code:
Option Explicit
Sub MikeyT()
    Application.ScreenUpdating = False
    Dim wb1 As Workbook, wb2 As Workbook
    Dim wsrc As Worksheet, wdest As Worksheet
    Dim lr As Long, i As Long
    
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks.Add(1)
    Set wdest = wb2.Worksheets(1)
    lr = wdest.Cells(Rows.Count, 1).End(3).Row + 1
    If lr = 2 Then lr = 1
    
    For i = 1 To wb1.Worksheets.Count
        With wb1.Worksheets(i).Cells(1, 1).CurrentRegion
            .AutoFilter 4, "Equipment", 2, "Grid"
            .Offset(1).Copy wdest.Cells(lr, 1)
            .AutoFilter
            lr = wdest.Cells(Rows.Count, 1).End(3).Row + 1
        End With
    Next i
    
    wb2.SaveAs ThisWorkbook.Path & "\New CSV File.csv", FileFormat:=6
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top