VBA code to copy data based on criteria

spvsr999

New Member
Joined
Aug 2, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi There,

I am looking for a VBA code that can copy all the rows (Incl Headers) based on criteria in specific column.

Scenario :-

I have a File with 8 different sheets and different information. Commonly, every sheet has a "Status" column.
I want to filter the status column and check for "Completed" and Paste special (Values & Formats) into a new workbook (Ready to save), new sheet with the Sheet name from the source file.

Example :-
Base file
Sheet Name is New Assignments --- There are 300+ records --- filtering on Status column (Column N) will give me 45 records.
Another Sheet name is Transfers --- There are 140+ records -- Filtering on Status Column (Column I) will give me 10 records

I want this the filtered lines into a new workbook

45 records + Headers into a new Worksheet with the sheet name -- New assignments
10 records + Headers into a new Worksheet with the sheet name -- Transfers

Thank you in advance for the help.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ThisWorkbook.Sheets("New Assignments")
    Set ws2 = ThisWorkbook.Sheets("Tranfers")
    Workbooks.Add
    With ws1
        .Range("A1").AutoFilter 14, "Completed"
        .AutoFilter.Range.Copy
        With Sheets(1)
            .Range("A1").PasteSpecial xlPasteValues
            .Range("A1").PasteSpecial xlPasteFormats
            .Name = ws1.Name
        End With
        .Range("A1").AutoFilter
    End With
    With ws2
        .Range("A1").AutoFilter 14, "Completed"
        .AutoFilter.Range.Copy
        With Sheets(2)
            .Range("A1").PasteSpecial xlPasteValues
            .Range("A1").PasteSpecial xlPasteFormats
            .Name = ws2.Name
        End With
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ThisWorkbook.Sheets("New Assignments")
    Set ws2 = ThisWorkbook.Sheets("Tranfers")
    Workbooks.Add
    With ws1
        .Range("A1").AutoFilter 14, "Completed"
        .AutoFilter.Range.Copy
        With Sheets(1)
            .Range("A1").PasteSpecial xlPasteValues
            .Range("A1").PasteSpecial xlPasteFormats
            .Name = ws1.Name
        End With
        .Range("A1").AutoFilter
    End With
    With ws2
        .Range("A1").AutoFilter 14, "Completed"
        .AutoFilter.Range.Copy
        With Sheets(2)
            .Range("A1").PasteSpecial xlPasteValues
            .Range("A1").PasteSpecial xlPasteFormats
            .Name = ws2.Name
        End With
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
Thank you very much for this.

However, while trying to run the script I am getting the below error

1627912999812.png


And when trying to Debug, it is pointing at " With Sheets(2) " in the code.

Hope you can help through this as well.
 
Upvote 0
How many sheets do you have in your workbook?
There are 8 sheets of data and 3 hidden sheets (if in case useful)

Also If you can kindly tweek the filters in each sheets to "Completed" and "Rejected" in statys column.. its would be of great use.

Thanking you once again ?
 
Upvote 0
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ThisWorkbook.Sheets("New Assignments")
    Set ws2 = ThisWorkbook.Sheets("Tranfers")
    Workbooks.Add
    With ws1
        .Range("A1").AutoFilter 14, "Completed"
        .AutoFilter.Range.Copy
        Sheets.Add after:=Sheets(Sheets.Count)
        Range("A1").PasteSpecial xlPasteValues
        Range("A1").PasteSpecial xlPasteFormats
        ActiveSheet.Name = ws1.Name
        .Range("A1").AutoFilter
    End With
    With ws2
        .Range("A1").AutoFilter 14, "Rejected"
        .AutoFilter.Range.Copy
        Sheets.Add after:=Sheets(Sheets.Count)
        Range("A1").PasteSpecial xlPasteValues
        Range("A1").PasteSpecial xlPasteFormats
        ActiveSheet.Name = ws2.Name
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ThisWorkbook.Sheets("New Assignments")
    Set ws2 = ThisWorkbook.Sheets("Tranfers")
    Workbooks.Add
    With ws1
        .Range("A1").AutoFilter 14, "Completed"
        .AutoFilter.Range.Copy
        Sheets.Add after:=Sheets(Sheets.Count)
        Range("A1").PasteSpecial xlPasteValues
        Range("A1").PasteSpecial xlPasteFormats
        ActiveSheet.Name = ws1.Name
        .Range("A1").AutoFilter
    End With
    With ws2
        .Range("A1").AutoFilter 14, "Rejected"
        .AutoFilter.Range.Copy
        Sheets.Add after:=Sheets(Sheets.Count)
        Range("A1").PasteSpecial xlPasteValues
        Range("A1").PasteSpecial xlPasteFormats
        ActiveSheet.Name = ws2.Name
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
Finally, the below code is working ... Thanks man !! ... Thank you very much .. You made my work a bit simpler ...

Sub CopyData()
Application.ScreenUpdating = False
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("New Assignments")
Set ws2 = ThisWorkbook.Sheets("Transfers")
Workbooks.Add
With ws1
.Range("A1").AutoFilter 14, "Completed"
.AutoFilter.Range.Copy
Sheets.Add after:=Sheets(Sheets.Count)
Range("A1").PasteSpecial xlPasteValues
Range("A1").PasteSpecial xlPasteFormats
ActiveSheet.Name = ws1.Name
.Range("A1").AutoFilter
End With
With ws2
.Range("A1").AutoFilter 18, "Completed"
.AutoFilter.Range.Copy
Sheets.Add after:=Sheets(Sheets.Count)
Range("A1").PasteSpecial xlPasteValues
Range("A1").PasteSpecial xlPasteFormats
ActiveSheet.Name = ws2.Name
.Range("A1").AutoFilter
End With
Application.ScreenUpdating = True
End Sub


Can you tell me how to add another criterion, I mean in the same column where the filter is there "Status" Column to give output for both "Completed" & "Pending"...!!

Thanks for everything !!!
 
Upvote 0
Replace this line of code:
VBA Code:
.Range("A1").AutoFilter 14, "Completed"

with this one:
VBA Code:
.Range("A1").AutoFilter 14, Criteria1:="Completed", Operator:=xlAnd, Criteria2:="Pending"
 
Upvote 0
Replace this line of code:
VBA Code:
.Range("A1").AutoFilter 14, "Completed"

with this one:
VBA Code:
.Range("A1").AutoFilter 14, Criteria1:="Completed", Operator:=xlAnd, Criteria2:="Pending"
No, if I change, the output is coming as headers ONLY and no data is being picked ..!!
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
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