Excel VBA, filter/copy/past to new worksheet

chyno813

New Member
Joined
Mar 28, 2017
Messages
2
I received a raw data weekly, the data has 5 worksheets and I need to filter the data start from "B4" cell with criteria "I" then copy and past all result from 5 worksheet into one new file. can someone help me with more efficient way?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
This will copy all filtered data into a sheet called "Compiled Master"
It assumes data is in Columns "A" to "H"....change the line in red to suit

Code:
Sub MM3()
Dim rang As Range, sh As Worksheet, lr As Long, lr2 As Long
Worksheets.Add
ActiveSheet.Name = "Compiled Master"
For Each sh In Worksheets
    If sh.Name <> "Compiled Master" Then
        sh.Activate
        lr2 = sh.Cells(Rows.Count, "B").End(xlUp).Row
        lr = Sheets("Compiled Master").Cells(Rows.Count, "A").End(xlUp).Row + 1
        sh.UsedRange.AutoFilter Field:=2, Criteria1:="I"
        [color=red]Set rang = sh.Range("A4:H" & lr2).Offset(1, 0)[/color] 'change "H" to your last used column letter
        Set rang = rang.Resize(rang.Rows.Count - 1)
        Set rang = rang.SpecialCells(xlCellTypeVisible)
            rang.Copy Sheets("Compiled Master").Range("A" & lr)
        sh.Cells.AutoFilter
    End If
Next sh
End Sub
 
Upvote 0
thanks for help, but it show error on

Set rang = rang.SpecialCells(xlCellTypeVisible)

can you help to check
 
Upvote 0
Is one of the sheets blank ?
OR
When the filter is applied, there are no rows visible ?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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