SOLVED

VBA copy specific criteria

%3CLINGO-SUB%20id%3D%22lingo-sub-1854467%22%20slang%3D%22en-US%22%3EVBA%20copy%20specific%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1854467%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20workbook%20with%204%20sheets%3A%20first%20one%20is%20the%20raw%20data%20sheet%2C%20then%203%20target%20sheets.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20would%20need%20a%20macro%20that%20would%20look%20at%20cell%20C%20in%20raw%20data%20sheet%20and%20based%20on%20the%20criteria%20values%20between%201%20and%2030%20days)%2C%20will%20copy%20and%20paste%20the%20range%20A%3AY%20in%20%3CSTRONG%3E%221%20to%2030%20Days%20Sheet%22.%3C%2FSTRONG%3E%20Example%3A%20if%20on%26nbsp%3B%20Column%20No%20of%20Days%26nbsp%3B%20in%20raw%20data%20sheet%26nbsp%3B%20have%26nbsp%3B%20between%2031%20and%2060%20days%20%2C%20copy%20A2%3AY2%20and%20paste%20into%20%2230%20Days%20Sheet%22%2C%20same%20range%20A2%3AY2.%20If%20instead%20i%20have%20the%20value%20between%20%2231%20and%2060%22%20copy%20A2%3AY2%20and%20paste%20into%20%3CSTRONG%3E%2231%20to%2060%20days%20sheet%20%22%3C%2FSTRONG%3E.%20Then%20go%20to%20next%20row%20and%20copy-paste%20A3%3AY3%20to%20%3CSTRONG%3Esheet%2061%20to%2090%20days.%3CBR%20%2F%3E%3C%2FSTRONG%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BPasting%20my%20poor%20code%20below%3A%3CBR%20%2F%3E%3CBR%20%2F%3ESheets(%22Master%20Report%22).Select%3CBR%20%2F%3ERange(%22A2%22).Select%3CBR%20%2F%3EActiveSheet.Range(%22%24A%241%3A%24H%24627%22).AutoFilter%20Field%3A%3D6%2C%20Criteria1%3A%3D%22%26gt%3B%3D1%22%2C%20_%3CBR%20%2F%3EOperator%3A%3DxlAnd%2C%20Criteria2%3A%3D%22%26lt%3B%3D30%22%3CBR%20%2F%3EActiveWindow.SmallScroll%20Down%3A%3D-3%3CBR%20%2F%3ESelection.End(xlDown).Select%3CBR%20%2F%3ESelection.End(xlUp).Select%3CBR%20%2F%3ESelection.End(xlDown).Select%3CBR%20%2F%3ESelection.End(xlUp).Select%3CBR%20%2F%3ESelection.End(xlDown).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlToRight)).Select%3CBR%20%2F%3ERange(%22A627%3AH627%22).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlUp)).Select%3CBR%20%2F%3ERange(%22A13%3AH627%22).Select%3CBR%20%2F%3ERange(%22A627%22).Activate%3CBR%20%2F%3ESelection.Copy%3CBR%20%2F%3ESheets(%220-30%20Days%3C%2FP%3E%3CP%3E%22).Select%3CBR%20%2F%3ERange(%22B8%22).Select%3CBR%20%2F%3EActiveWindow.SmallScroll%20Down%3A%3D-9%3CBR%20%2F%3EActiveSheet.Paste%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESheets(%22Master%26nbsp%3B%20Report%22).Select%3CBR%20%2F%3ERange(%22A2%22).Select%3CBR%20%2F%3EActiveSheet.Range(%22%24A%241%3A%24H%24627%22).AutoFilter%20Field%3A%3D6%2C%20Criteria1%3A%3D%22%26gt%3B%3D31%22%2C%20_%3CBR%20%2F%3EOperator%3A%3DxlAnd%2C%20Criteria2%3A%3D%22%26lt%3B%3D60%22%3CBR%20%2F%3EActiveWindow.SmallScroll%20Down%3A%3D-12%3CBR%20%2F%3ERange(%22A8%22).Select%3CBR%20%2F%3ESelection.End(xlUp).Select%3CBR%20%2F%3ERange(%22A5%22).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlToRight)).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlDown)).Select%3CBR%20%2F%3ERange(%22A600%22).Select%3CBR%20%2F%3ESelection.End(xlDown).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlToRight)).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlUp)).Select%3CBR%20%2F%3ERange(%22A5%3AH619%22).Select%3CBR%20%2F%3ERange(%22A619%22).Activate%3CBR%20%2F%3EApplication.CutCopyMode%20%3D%20False%3CBR%20%2F%3ESelection.Copy%3CBR%20%2F%3ESheets(%2231-60%20Days%20%22).Select%3CBR%20%2F%3ERange(%22B8%22).Select%3CBR%20%2F%3EActiveWindow.SmallScroll%20Down%3A%3D-9%3CBR%20%2F%3EActiveSheet.Paste%3CBR%20%2F%3E%3CBR%20%2F%3ESheets(%22%20Master%20Report%22).Select%3CBR%20%2F%3EActiveSheet.Range(%22%24A%241%3A%24H%24627%22).AutoFilter%20Field%3A%3D6%2C%20Criteria1%3A%3D%22%26gt%3B%3D61%22%2C%20_%3CBR%20%2F%3EOperator%3A%3DxlAnd%2C%20Criteria2%3A%3D%22%26lt%3B%3D90%22%3CBR%20%2F%3ERange(%22A4%22).Select%3CBR%20%2F%3ESelection.End(xlDown).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlToRight)).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlUp)).Select%3CBR%20%2F%3ERange(%22A2%3AH626%22).Select%3CBR%20%2F%3ERange(%22A626%22).Activate%3CBR%20%2F%3EApplication.CutCopyMode%20%3D%20False%3CBR%20%2F%3ESelection.Copy%3CBR%20%2F%3ESheets(%2261-90%20Days%20%22).Select%3CBR%20%2F%3ERange(%22B8%22).Select%3CBR%20%2F%3ESelection.End(xlUp).Select%3CBR%20%2F%3ERange(%22B8%22).Select%3CBR%20%2F%3EActiveSheet.Paste%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESheets(%22%20Master%20Report%22).Select%3CBR%20%2F%3EActiveSheet.Range(%22%24A%241%3A%24H%241264%22).AutoFilter%20Field%3A%3D6%2C%20Criteria1%3A%3D%22%26gt%3B%3D91%22%2C%20_%3CBR%20%2F%3EOperator%3A%3DxlAnd%3CBR%20%2F%3EActiveWindow.SmallScroll%20Down%3A%3D-6%3CBR%20%2F%3ERange(%22A795%22).Select%3CBR%20%2F%3ESelection.End(xlUp).Select%3CBR%20%2F%3ERange(%22A794%22).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlToRight)).Select%3CBR%20%2F%3ERange(Selection%2C%20Selection.End(xlDown)).Select%3CBR%20%2F%3ESelection.Copy%3CBR%20%2F%3ESheets(%2291%2B%20%22).Select%3CBR%20%2F%3ERange(%22B8%22).Select%3CBR%20%2F%3ESelection.End(xlUp).Select%3CBR%20%2F%3ERange(%22B8%22).Select%3CBR%20%2F%3EActiveSheet.Paste%3CBR%20%2F%3ESheets(%22Main%20Menu%22).Select%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1854467%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1854643%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20copy%20specific%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1854643%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F699853%22%20target%3D%22_blank%22%3E%40dhan09%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETry%20this%20as%20starting%20point.%20You%20may%20have%20to%20change%20it%20since%20your%20macro%20and%20description%20don't%20match.%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20SplitData()%0A%20%20%20%20Dim%20wshS%20As%20Worksheet%0A%20%20%20%20Dim%20strCol%20As%20String%0A%20%20%20%20Dim%20rng%20As%20Range%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20wshS%20%3D%20Worksheets(%22Main%20Menu%22)%0A%20%20%20%20strCol%20%3D%20%22C%22%0A%20%20%20%20Set%20rng%20%3D%20wshS.Range(%22A1%22).CurrentRegion%0A%20%20%20%20rng.AutoFilter%20Field%3A%3D3%2C%20Criteria1%3A%3D%22%26gt%3B%3D1%22%2C%20_%0A%20%20%20%20%20%20%20%20Operator%3A%3DxlAnd%2C%20Criteria2%3A%3D%22%26lt%3B%3D30%22%0A%20%20%20%20rng.Copy%20Destination%3A%3DWorksheets(%220-30%20Days%22).Range(%22A1%22)%0A%20%20%20%20%20rng.AutoFilter%20Field%3A%3D3%2C%20Criteria1%3A%3D%22%26gt%3B%3D31%22%2C%20_%0A%20%20%20%20%20%20%20%20Operator%3A%3DxlAnd%2C%20Criteria2%3A%3D%22%26lt%3B%3D60%22%0A%20%20%20%20rng.Copy%20Destination%3A%3DWorksheets(%2231-60%20Days%22).Range(%22A1%22)%0A%20%20%20%20rng.AutoFilter%20Field%3A%3D3%2C%20Criteria1%3A%3D%22%26gt%3B%3D61%22%2C%20_%0A%20%20%20%20%20%20%20%20Operator%3A%3DxlAnd%2C%20Criteria2%3A%3D%22%26lt%3B%3D90%22%0A%20%20%20%20rng.Copy%20Destination%3A%3DWorksheets(%2261-90%20Days%22).Range(%22A1%22)%0A%20%20%20%20rng.AutoFilter%0A%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
New Contributor

I have a workbook with 4 sheets: first one is the raw data sheet, then 3 target sheets.

I would need a macro that would look at cell C in raw data sheet and based on the criteria values between 1 and 30 days), will copy and paste the range A:Y in "1 to 30 Days Sheet". Example: if on  Column No of Days  in raw data sheet  have  between 31 and 60 days , copy A2:Y2 and paste into "30 Days Sheet", same range A2:Y2. If instead i have the value between "31 and 60" copy A2:Y2 and paste into "31 to 60 days sheet ". Then go to next row and copy-paste A3:Y3 to sheet 61 to 90 days.
  

 Pasting my poor code below:

Sheets("Master Report").Select
Range("A2").Select
ActiveSheet.Range("$A$1:$H$627").AutoFilter Field:=6, Criteria1:=">=1", _
Operator:=xlAnd, Criteria2:="<=30"
ActiveWindow.SmallScroll Down:=-3
Selection.End(xlDown).Select
Selection.End(xlUp).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select
Range("A627:H627").Select
Range(Selection, Selection.End(xlUp)).Select
Range("A13:H627").Select
Range("A627").Activate
Selection.Copy
Sheets("0-30 Days

").Select
Range("B8").Select
ActiveWindow.SmallScroll Down:=-9
ActiveSheet.Paste


Sheets("Master  Report").Select
Range("A2").Select
ActiveSheet.Range("$A$1:$H$627").AutoFilter Field:=6, Criteria1:=">=31", _
Operator:=xlAnd, Criteria2:="<=60"
ActiveWindow.SmallScroll Down:=-12
Range("A8").Select
Selection.End(xlUp).Select
Range("A5").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Range("A600").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlUp)).Select
Range("A5:H619").Select
Range("A619").Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("31-60 Days ").Select
Range("B8").Select
ActiveWindow.SmallScroll Down:=-9
ActiveSheet.Paste

Sheets(" Master Report").Select
ActiveSheet.Range("$A$1:$H$627").AutoFilter Field:=6, Criteria1:=">=61", _
Operator:=xlAnd, Criteria2:="<=90"
Range("A4").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlUp)).Select
Range("A2:H626").Select
Range("A626").Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("61-90 Days ").Select
Range("B8").Select
Selection.End(xlUp).Select
Range("B8").Select
ActiveSheet.Paste


Sheets(" Master Report").Select
ActiveSheet.Range("$A$1:$H$1264").AutoFilter Field:=6, Criteria1:=">=91", _
Operator:=xlAnd
ActiveWindow.SmallScroll Down:=-6
Range("A795").Select
Selection.End(xlUp).Select
Range("A794").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("91+ ").Select
Range("B8").Select
Selection.End(xlUp).Select
Range("B8").Select
ActiveSheet.Paste
Sheets("Main Menu").Select

End Sub

3 Replies
best response confirmed by dhan09 (New Contributor)
Solution

@dhan09 

Try this as starting point. You may have to change it since your macro and description don't match.

Sub SplitData()
    Dim wshS As Worksheet
    Dim strCol As String
    Dim rng As Range
    Application.ScreenUpdating = False
    Set wshS = Worksheets("Main Menu")
    strCol = "C"
    Set rng = wshS.Range("A1").CurrentRegion
    rng.AutoFilter Field:=3, Criteria1:=">=1", _
        Operator:=xlAnd, Criteria2:="<=30"
    rng.Copy Destination:=Worksheets("0-30 Days").Range("A1")
     rng.AutoFilter Field:=3, Criteria1:=">=31", _
        Operator:=xlAnd, Criteria2:="<=60"
    rng.Copy Destination:=Worksheets("31-60 Days").Range("A1")
    rng.AutoFilter Field:=3, Criteria1:=">=61", _
        Operator:=xlAnd, Criteria2:="<=90"
    rng.Copy Destination:=Worksheets("61-90 Days").Range("A1")
    rng.AutoFilter
   Application.ScreenUpdating = True
End Sub

@Hans VogelaarThanks for valuable input,  I'm trying add  below code for more than 91 days and I'm getting error 'Run time Error 9 : Subscript out of range"
 rng.AutoFilter Field:=3, Criteria1:=">=91", _
rng.Copy Destination:=Worksheets("91+ Days").Range("B7")
rng.AutoFilter

 

 

 

 

 

Sub SplitData()
    Dim wshS As Worksheet
    Dim strCol As String
    Dim rng As Range
    Application.ScreenUpdating = False
    Set wshS = Worksheets("Main Menu")
    strCol = "C"
    Set rng = wshS.Range("A1").CurrentRegion
    rng.AutoFilter Field:=3, Criteria1:=">=1", _
        Operator:=xlAnd, Criteria2:="<=30"
    rng.Copy Destination:=Worksheets("0-30 Days").Range("A1")
     rng.AutoFilter Field:=3, Criteria1:=">=31", _
        Operator:=xlAnd, Criteria2:="<=60"
    rng.Copy Destination:=Worksheets("31-60 Days").Range("A1")
    rng.AutoFilter Field:=3, Criteria1:=">=61", _
        Operator:=xlAnd, Criteria2:="<=90"
    rng.Copy Destination:=Worksheets("61-90 Days").Range("A1")
     
    rng.AutoFilter Field:=3, Criteria1:=">=91", _
      rng.Copy Destination:=Worksheets("91+ Days").Range("B7")
rng.AutoFilter
   Application.ScreenUpdating = True
End Sub

 

 

 

 

  

@dhan09 

You forgot to copy a line:

Sub SplitData()
    Dim wshS As Worksheet
    Dim strCol As String
    Dim rng As Range
    Application.ScreenUpdating = False
    Set wshS = Worksheets("Main Menu")
    strCol = "C"
    Set rng = wshS.Range("A1").CurrentRegion
    rng.AutoFilter Field:=3, Criteria1:=">=1", _
        Operator:=xlAnd, Criteria2:="<=30"
    rng.Copy Destination:=Worksheets("0-30 Days").Range("B7")
    rng.AutoFilter Field:=3, Criteria1:=">=31", _
        Operator:=xlAnd, Criteria2:="<=60"
    rng.Copy Destination:=Worksheets("31-60 Days").Range("B7")
    rng.AutoFilter Field:=3, Criteria1:=">=61", _
        Operator:=xlAnd, Criteria2:="<=90"
    rng.Copy Destination:=Worksheets("61-90 Days").Range("B7")
    rng.AutoFilter Field:=3, Criteria1:=">=91", _
        Operator:=xlAnd, Criteria2:="<=90"
    rng.Copy Destination:=Worksheets("91+ Days").Range("B7")
    rng.AutoFilter
    Application.ScreenUpdating = True
End Sub