Filter data and copy to new sheets

Dave_george

New Member
Joined
Jul 20, 2023
Messages
32
Office Version
  1. 2021
  2. 2016
  3. 2013
Platform
  1. Windows
I am typing to makes changes to the below code. I have 4 different categories in column C. ( D002,D004,D005 and 2* codes). I want D002 and 2* in one sheet. D004 and D005 filtered in another sheet.


CUSTOMER_IDNamesHUBWAVECASESVOLUMEWEIGHTSEGSHIPMENT_GROUP
1002​
n/aD002
4​
584​
7.58​
3,284.79​
STD
1003​
n/aD002
4​
680​
8.14​
4,063.38​
STD
1007​
n/aD004
4​
607​
8.84​
5,798.22​
STD
1009​
n/aD002
4​
207​
2.93​
1,332.82​
STD
1012​
n/aD004
4​
1,147.00​
13.29​
4,930.20​
STD
1134​
n/aD005
4​
152​
2.04​
779.16​
STD
1171​
n/aD005
4​
365​
5.03​
1,605.54​
STD
1180​
n/aD005
4​
139​
2.4​
740.93​
STD
1350​
n/aD005
4​
688​
10.89​
7,074.39​
STD
1361​
n/aD005
4​
265​
4.86​
1,356.40​
STD
1364​
n/aD005
4​
401​
5.01​
2,080.61​
STD
1376​
n/aD005
4​
162​
1.82​
686.26​
STD
1377​
n/aD005
4​
409​
6.53​
3,500.15​
STD
2567​
n/a
2567​
4​
78​
0.93​
422.57​
STD
2605​
n/a
2605​
4​
370​
6.79​
2,482.73​
STD
2640​
n/a
2640​
4​
66​
2.04​
378.43​
STD
2641​
n/a
2641​
4​
68​
0.5​
292.69​
STD
2663​
n/a
2663​
4​
54​
0.52​
255.28​
STD
2695​
n/a
2695​
4​
54​
1.27​
279.84​
STD
2719​
n/a
2719​
4​
160​
4​
903.73​
STD
2722​
n/a
2722​
4​
748​
13.57​
6,164.86​
STD
2737​
n/a
2737​
4​
61​
0.45​
361.51​
STD


VBA Code:
Public gsDataTbl As String
Public gcolCtry As Collection

Public Const kCtryCOL = "C"

Public Sub HUB_FILTER()
Dim sCtry
Dim wsTarg As Worksheet
Dim iCtryCol As Integer, i As Integer
gsDataTbl = ActiveSheet.Name
iCtryCol = Asc(kCtryCOL) - 64
CollectCountries
For i = 1 To gcolCtry.Count
    sCtry = gcolCtry(i)
  
      
    Selection.AutoFilter
    ActiveSheet.UsedRange.AutoFilter Field:=iCtryCol, Criteria1:=sCtry
    ActiveSheet.UsedRange.Select
    Range(kCtryCOL & "1").Activate
    Selection.Copy
  
    Sheets.Add After:=ActiveSheet
    Set wsTarg = ActiveSheet
    ActiveSheet.Paste
    Application.CutCopyMode = False
  
    wsTarg.Activate
    wsTarg.Name = sCtry
      
    Sheets(gsDataTbl).Select
    Selection.AutoFilter
Next
Set wsTarg = Nothing
Set gcolCtry = Nothing
End Sub

Private Sub CollectCountries()
Set gcolCtry = New Collection
Dim sCtry As String
    Columns(kCtryCOL & ":" & kCtryCOL).Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Range("$A$1:$A$1000").RemoveDuplicates Columns:=1, Header:=xlYes
    Range("A2").Select
   
While ActiveCell.Value <> ""
    sCtry = ActiveCell.Value
    gcolCtry.Add sCtry, sCtry
  
    ActiveCell.Offset(1, 0).Select
Wend
  
    Sheets(gsDataTbl).Select
    Range("A1").Select
 
You could try this code instead: Let me know how it goes.
VBA Code:
Sub Dave_george()
Dim LR As Long
Dim Startsheet As Worksheet
LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row 'Choose a column that has data to the end to get the last row number change "A" to the column
Set Startsheet = ActiveSheet
Rows("1:1").Select
If ActiveSheet.AutoFilterMode = False Then
    Selection.AutoFilter
End If
ActiveSheet.Range("$A$1:$I$" & LR).AutoFilter Field:=3, Criteria1:=Array( _
    "2*", "D002") _
    , Operator:=xlFilterValues
Range("A1:I" & LR).SpecialCells(xlCellTypeVisible).Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Startsheet.Select
ActiveSheet.Range("$A$1:$I$" & LR).AutoFilter Field:=3, Criteria1:="=D004", _
    Operator:=xlOr, Criteria2:="=D005"
Range("A1:I" & LR).SpecialCells(xlCellTypeVisible).Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Set Startsheet = Nothing
End Sub
 
Upvote 1
I note you have 2021 on your profile, in which case you could access the Filter function. So with this on Sheet1:
Book1
ABCDEFGHI
1CUSTOMER_IDNamesHUBWAVECASESVOLUMEWEIGHTSEGSHIPMENT_GROUP
21002n/aD00245847.583,284.79STD
31003n/aD00246808.144,063.38STD
41007n/aD00446078.845,798.22STD
51009n/aD00242072.931,332.82STD
61012n/aD00441,147.0013.294,930.20STD
71134n/aD00541522.04779.16STD
81171n/aD00543655.031,605.54STD
91180n/aD00541392.4740.93STD
101350n/aD005468810.897,074.39STD
111361n/aD00542654.861,356.40STD
121364n/aD00544015.012,080.61STD
131376n/aD00541621.82686.26STD
141377n/aD00544096.533,500.15STD
152567n/a25674780.93422.57STD
162605n/a260543706.792,482.73STD
172640n/a26404662.04378.43STD
182641n/a26414680.5292.69STD
192663n/a26634540.52255.28STD
202695n/a26954541.27279.84STD
212719n/a271941604903.73STD
222722n/a2722474813.576,164.86STD
232737n/a27374610.45361.51STD
Sheet1


This on Sheet2:
Book1
ABCDEFGHI
1CUSTOMER_IDNamesHUBWAVECASESVOLUMEWEIGHTSEGSHIPMENT_GROUP
21002n/aD00245847.583,284.79STD
31003n/aD00246808.144,063.38STD
41009n/aD00242072.931,332.82STD
52567n/a25674780.93422.57STD
62605n/a260543706.792,482.73STD
72640n/a26404662.04378.43STD
82641n/a26414680.5292.69STD
92663n/a26634540.52255.28STD
102695n/a26954541.27279.84STD
112719n/a271941604903.73STD
122722n/a2722474813.576,164.86STD
132737n/a27374610.45361.51STD
Sheet2
Cell Formulas
RangeFormula
A2:I13A2=FILTER(Sheet1!A:I,(Sheet1!C:C="D002")+(LEFT(Sheet1!C:C,1)="2"),"")
Dynamic array formulas.


And this on Sheet3:
Book1
ABCDEFGHI
1CUSTOMER_IDNamesHUBWAVECASESVOLUMEWEIGHTSEGSHIPMENT_GROUP
21007n/aD00446078.845,798.22STD
31012n/aD00441,147.0013.294,930.20STD
41134n/aD00541522.04779.16STD
51171n/aD00543655.031,605.54STD
61180n/aD00541392.4740.93STD
71350n/aD005468810.897,074.39STD
81361n/aD00542654.861,356.40STD
91364n/aD00544015.012,080.61STD
101376n/aD00541621.82686.26STD
111377n/aD00544096.533,500.15STD
Sheet3
Cell Formulas
RangeFormula
A2:I11A2=FILTER(Sheet1!A:I,(Sheet1!C:C="D004")+(Sheet1!C:C="D005"),"")
Dynamic array formulas.
 
Upvote 0
Alternative code if you really want a VBA solution:
VBA Code:
Option Explicit
Sub Filter_Copy()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Set ws1 = Worksheets("Sheet1")          '<--- *** Change sheet names to suit ***
    Set ws2 = Worksheets("Sheet2")
    Set ws3 = Worksheets("Sheet3")
    
    With ws1
        If .AutoFilterMode Then .AutoFilter.ShowAllData
        With .Range("A1").CurrentRegion
            .AutoFilter 3, "D002", 2, "2*"
            If WorksheetFunction.Subtotal(3, .Columns(1)) > 1 Then
                .Copy ws2.Range("A1")
            End If
            .AutoFilter 3, "D004", 2, "D005"
            If WorksheetFunction.Subtotal(3, .Columns(1)) > 1 Then
                .Copy ws3.Range("A1")
            End If
            .AutoFilter
        End With
    End With
End Sub
 
Upvote 1
Solution
You could try this code instead: Let me know how it goes.
VBA Code:
Sub Dave_george()
Dim LR As Long
Dim Startsheet As Worksheet
LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row 'Choose a column that has data to the end to get the last row number change "A" to the column
Set Startsheet = ActiveSheet
Rows("1:1").Select
If ActiveSheet.AutoFilterMode = False Then
    Selection.AutoFilter
End If
ActiveSheet.Range("$A$1:$I$" & LR).AutoFilter Field:=3, Criteria1:=Array( _
    "2*", "D002") _
    , Operator:=xlFilterValues
Range("A1:I" & LR).SpecialCells(xlCellTypeVisible).Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Startsheet.Select
ActiveSheet.Range("$A$1:$I$" & LR).AutoFilter Field:=3, Criteria1:="=D004", _
    Operator:=xlOr, Criteria2:="=D005"
Range("A1:I" & LR).SpecialCells(xlCellTypeVisible).Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Set Startsheet = Nothing
End Sub
This works for me however it doesn't filter 2*. I am only left with D002.
 
Upvote 0
Alternative code if you really want a VBA solution:
VBA Code:
Option Explicit
Sub Filter_Copy()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Set ws1 = Worksheets("Sheet1")          '<--- *** Change sheet names to suit ***
    Set ws2 = Worksheets("Sheet2")
    Set ws3 = Worksheets("Sheet3")
   
    With ws1
        If .AutoFilterMode Then .AutoFilter.ShowAllData
        With .Range("A1").CurrentRegion
            .AutoFilter 3, "D002", 2, "2*"
            If WorksheetFunction.Subtotal(3, .Columns(1)) > 1 Then
                .Copy ws2.Range("A1")
            End If
            .AutoFilter 3, "D004", 2, "D005"
            If WorksheetFunction.Subtotal(3, .Columns(1)) > 1 Then
                .Copy ws3.Range("A1")
            End If
            .AutoFilter
        End With
    End With
End Sub
This works for me however it doesn't filter 2*. I am only left with D002.
 
Upvote 0
This works for me however it doesn't filter 2*. I am only left with D002.
It works for me:
Book1
ABCDEFGHI
1CUSTOMER_IDNamesHUBWAVECASESVOLUMEWEIGHTSEGSHIPMENT_GROUP
21002n/aD00245847.583,284.79STD
31003n/aD00246808.144,063.38STD
41009n/aD00242072.931,332.82STD
52567n/a25674780.93422.57STD
62605n/a260543706.792,482.73STD
72640n/a26404662.04378.43STD
82641n/a26414680.5292.69STD
92663n/a26634540.52255.28STD
102695n/a26954541.27279.84STD
112719n/a271941604903.73STD
122722n/a2722474813.576,164.86STD
132737n/a27374610.45361.51STD
Sheet2


Could you share your actual file via Google Drive, Dropbox or similar file sharing platform so we can get to the bottom of the problem?
 
Upvote 0
Alternative code if you really want a VBA solution:
VBA Code:
Option Explicit
Sub Filter_Copy()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Set ws1 = Worksheets("Sheet1")          '<--- *** Change sheet names to suit ***
    Set ws2 = Worksheets("Sheet2")
    Set ws3 = Worksheets("Sheet3")
   
    With ws1
        If .AutoFilterMode Then .AutoFilter.ShowAllData
        With .Range("A1").CurrentRegion
            .AutoFilter 3, "D002", 2, "2*"
            If WorksheetFunction.Subtotal(3, .Columns(1)) > 1 Then
                .Copy ws2.Range("A1")
            End If
            .AutoFilter 3, "D004", 2, "D005"
            If WorksheetFunction.Subtotal(3, .Columns(1)) > 1 Then
                .Copy ws3.Range("A1")
            End If
            .AutoFilter
        End With
    End With
End Sub
I Just made changes to D002* and it worked. Thank you.
 
Upvote 0

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