VBA to Copy and Paste Rows if Conditions are Met

lpw0806

New Member
Joined
Jun 14, 2018
Messages
38
Hi -

I am a first time poster on this site. I am still pretty green to VBA, but am learning and have made some small / short macros. I am familiar with SQL if that helps at all.

Right now I have a file with a "Raw Data" tab. I'd like to have a macro that states something like "IF Column A = "75" and Column B = "West" and Column C = "Local" etc etc then:

1) Create a new sheet titled 75
2) Paste that data into this new sheet.
--- note: Ideally I'd like to copy a handful of columns on the Raw Data tab instead of all of them.

I think there will need to be a "loop" involved here? Also - the number of rows is not sedentary and will always change.

Thanks in advance for the help on this!!! Much appreciated!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi & welcome to MrExcel
How about
Code:
Sub FilterCopy()
   
   With Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
      If .AutoFilterMode Then .AutoFilterMode = False
      With .Range("[COLOR=#0000ff]A1:Z1[/COLOR]")
         .AutoFilter 1, 75
         .AutoFilter 2, "West"
         .AutoFilter 3, "Local"
      End With
      Sheets.Add(, Sheets(Sheets.Count)).Name = "75"
      .AutoFilter.Range.Copy Sheets("75").Range("A1")
      .AutoFilterMode = False
   End With
End Sub
Change sheet name in red & range in blue to suit.
 
Upvote 0
Omg!!! That really helped!! Thank you!! A few follow up questions:


  • Do these numbers (AutoFilter 1, AutoFilter 2, etc) reference the column that is being filtered? EX - AutoFilter 1 = Column A and AutoFilter 26 = Column Z?
.AutoFilter 1, 75
.AutoFilter 2, "West"
.AutoFilter 3, "Local"


  • Is there a way to only copy and paste certain columns? EX - only columns B, F, G, L-O


  • Is there a way to "loop" through the raw data to do multiple searches?
    • EX - First search (what you already showed me) looks for "75", "West" and "Local" and pastes those values into a new sheet
    • Then the macro would search for values with a different criteria (ex "50", "West" and "Local") - then a third search ("75", "West" and "National"), a fourth ("50", "West" and "National") etc etc

Thank you again for all of the help!! This was great!
 
Upvote 0
Do these numbers (AutoFilter 1, AutoFilter 2, etc) reference the column that is being filtered? EX - AutoFilter 1 = Column A and AutoFilter 26 = Column Z?
Yes they do, as long as the autofilter starts in Col A.
You can create an array for the filter criteria like
Code:
Sub FilterCopy()
   Dim ary As Variant
   ary = Array(75, "West", "Local", 50, "West", "local", 75, "West", "National")
   
   For i = 0 To UBound(ary) Step 3
      With Sheets("Sheet1")
         If .AutoFilterMode Then .AutoFilterMode = False
         With .Range("A1:Z1")
            .AutoFilter 1, ary(i)
            .AutoFilter 2, ary(i + 1)
            .AutoFilter 3, ary(i + 2)
         End With
         Sheets.Add(, Sheets(Sheets.Count)).name = "75"
         Intersect(.AutoFilter.Range, Range("B:B,F:G,L:O")).Copy Sheets("75").Range("A1")
         .AutoFilterMode = False
      End With
   Next i
End Sub
But what would the sheet names be?
 
Upvote 0
So both 75, "West", "Local" and 75, "West", "National" would both go on sheet name 75.
Is that right?
 
Upvote 0
ahhh....good point. I was just using those fields as a sample but realize now that it's a little confusing - sorry about that

Here is a sample of the data. I'd like the macro to search the data and output tabs ( named 50, 75, 85, 95, 100) - but not include all of the columns listed.

Criteria

  • Opportunity Owner: Department = National, Probability (%) = 50
  • Opportunity Owner: Department = National, Probability (%) = 75
  • Opportunity Owner: Department = National, Probability (%) = 85
  • Opportunity Owner: Department = National, Probability (%) = 95
  • Opportunity Owner: Department = National, Probability (%) = 100

Columns to Include


  • Opportunity Owner
  • Account Name
  • Opportunity Name
  • Probability (%)
  • Previous Probability
  • Last Change Date
  • Duration Date
  • SP
  • DI
  • Total

Opportunity IDOpportunity OwnerOpportunity Owner: DepartmentProbability (%)Previous ProbabilityAccount NameOpportunity NameClient CategoryTypeLast Change DateDuration DaysRVSPDITotal
0064100000CMLWDBob SmithLocal10095ABC123XYZ123TravelNew6/6/20189$190,000.00$650,866.00$941,439.00$1,592,305.00
0064100000CMLY7Bob SmithLocal10095ABC124XYZ124TravelNew5/30/201816$117,188.00$993,996.00$682,723.00$1,676,719.00
0064100000Chl2BBob SmithLocal4025ABC125XYZ125TravelNew6/14/20181$35,000.00$532,134.00$878,368.00$1,410,502.00
0064100000Egf7LBob SmithLocal255ABC126XYZ126TravelNew6/7/20188$50,000.00$959,956.00$923,252.00$1,883,208.00
0064100000FEqmMBob SmithLocal255ABC127XYZ127TravelNew5/31/201815$50,000.00$593,839.00$297,582.00$891,421.00
0064100000FyJGWBob SmithLocal10095ABC128XYZ128TravelRenewal5/29/201817$120,000.00$995,399.00$574,795.00$1,570,194.00
0064100000GInahBob SmithLocal7550ABC129XYZ129TravelRenewal6/13/20182$26,000.00$716,872.00$531,627.00$1,248,499.00
0064100000GInrJBob SmithLocal9585ABC130XYZ130TravelRenewal6/7/20188$90,000.00$638,496.00$324,997.00$963,493.00
0064100000GIo4NBob SmithLocal10095ABC131XYZ131TravelRenewal5/29/201817$41,000.00$626,325.00$782,363.00$1,408,688.00
0064100000GIr1UBob SmithLocal10095ABC132XYZ132TravelRenewal6/12/20183$7,500.00$971,209.00$274,300.00$1,245,509.00
0064100000GLmHbBob SmithLocal10095ABC133XYZ133TravelRenewal6/4/201811$28,848.00$334,215.00$735,878.00$1,070,093.00
0064100000GLtlDBob SmithNational505ABC134XYZ134TravelNew6/11/20184$250,000.00$771,801.00$474,450.00$1,246,251.00
0064100000GLuspBob SmithNational7540ABC135XYZ135TravelRenewal5/29/201817$400,000.00$409,341.00$738,747.00$1,148,088.00
0064100000GM6IVBob SmithLocal9585ABC136XYZ136TravelRenewal6/13/20182$72,000.00$989,813.00$607,033.00$1,596,846.00
0064100000HPWIgBob SmithLocal10095ABC137XYZ137TravelRenewal6/4/201811$152,000.00$804,147.00$870,487.00$1,674,634.00
0064100000HPWMxBob SmithLocal10095ABC138XYZ138TravelRenewal6/6/20189$8,000.00$836,409.00$912,215.00$1,748,624.00
0064100000HPWOeBob SmithLocal10095ABC139XYZ139TravelRenewal5/30/201816$76,808.00$220,144.00$914,156.00$1,134,300.00
0064100000HPWXWBob SmithLocal9585ABC140XYZ140TravelRenewal5/29/201817$15,000.00$133,979.00$193,746.00$327,725.00
0064100000HPWc2Bob SmithLocal9585ABC141XYZ141TravelRenewal5/29/201817$46,000.00$863,354.00$149,726.00$1,013,080.00
0064100000HPWdUBob SmithLocal10095ABC142XYZ142TravelRenewal6/1/201814$47,000.00$295,988.00$48,053.00$344,041.00
0064100000HPWuGBob SmithLocal9585ABC143XYZ143TravelRenewal6/13/20182$36,000.00$725,882.00$740,921.00$1,466,803.00
0064100000HPWvnBob SmithLocal10095ABC144XYZ144TravelRenewal6/6/20189$25,000.00$205,745.00$207,827.00$413,572.00
0064100000IhMyZBob SmithNational9585ABC145XYZ145TravelNew6/14/20181$300,000.00$683,260.00$19,420.00$702,680.00
0064100000JdGOpBob SmithNational7550ABC146XYZ146TravelNew5/29/201817$300,000.00$26,221.00$472,009.00$498,230.00
0064100000JtAA4Bob SmithLocal10095ABC147XYZ147TravelRenewal5/31/201815$80,000.00$51,442.00$620,841.00$672,283.00
0064100000LEQWbBob SmithLocal9575ABC148XYZ148TravelRenewal5/30/201816$35,000.00$604,109.00$485,938.00$1,090,047.00
0064100000LEQozBob SmithLocal10095ABC149XYZ149TravelRenewal5/31/201815$40,000.00$198,456.00$745,859.00$944,315.00
0064100000LER66Bob SmithLocal10095ABC150XYZ150TravelNew6/11/20184$125,000.00$228,145.00$724,502.00$952,647.00
0064100000LEctFBob SmithLocal10095ABC151XYZ151TravelRenewal5/30/201816$55,000.00$46,831.00$757,383.00$804,214.00
0064100000LEpn5Bob SmithLocal9575ABC152XYZ152TravelRenewal5/30/201816$175,000.00$685,991.00$241,713.00$927,704.00
0064100000LF9MeBob SmithLocal8575ABC153XYZ153TravelRenewal5/31/201815$70,000.00$26,335.00$424,195.00$450,530.00
0064100000LF9NDBob SmithLocal10095ABC154XYZ154TravelRenewal6/6/20189$38,571.00$962,382.00$377,739.00$1,340,121.00

<colgroup><col><col><col><col><col><col span="2"><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>


I tried to figure out how to upload a file but I can't....thank you again for your help with this!
 
Upvote 0
Ok, how about
Code:
Sub FilterCopy()
   Dim Ary As Variant
   Dim i As Long
   Ary = Array(50, 75, 85, 95, 100)
   
   For i = 0 To UBound(Ary)
      With Sheets("Sheet1")
         If .AutoFilterMode Then .AutoFilterMode = False
         With .Range("A1:O1")
            .AutoFilter 3, "National"
            .AutoFilter 4, Ary(i)
         End With
         Sheets.Add(, Sheets(Sheets.Count)).name = Ary(i)
         Intersect(.AutoFilter.Range, .Range("B:B,D:G,J:O")).Copy Sheets(CStr(Ary(i))).Range("A1")
         .AutoFilterMode = False
      End With
   Next i
End Sub
 
Upvote 0
AMAZING!!! IT WORKED!!!

THANK YOU

Last question - I swear.

I want to format the sheets after the output is finished. I recorded a macro of me doing the formatting.

1) Where in the above macro you wrote can I copy and paste it?
2) Do I have to rewrite it for each sheet? (50, 75, etc)?

THANK YOU AGAIN!!!

Code:
Sub Formatting()
'
' Formatting Macro
'


'
    Cells.Select
    Cells.EntireColumn.AutoFit
    Columns("H:K").Select
    Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
    Range("F1").Select
    ActiveWorkbook.Worksheets("50").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("50").Sort.SortFields.Add Key:=Range("F1"), SortOn _
        :=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("50").Sort
        .SetRange Range("A2:K10000")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited by a moderator:
Upvote 0
Try
Code:
Sub Filtercopy()
   Dim ary As Variant
   Dim i As Long
   ary = Array(50, 75, 85, 95, 100)
   
   For i = 0 To UBound(ary)
      With Sheets("Sheet1")
         If .AutoFilterMode Then .AutoFilterMode = False
         With .Range("A1:O1")
            .AutoFilter 3, "National"
            .AutoFilter 4, ary(i)
         End With
         Sheets.Add(, Sheets(Sheets.Count)).name = ary(i)
         Intersect(.AutoFilter.Range, .Range("B:B,D:G,J:O")).Copy Sheets(CStr(ary(i))).Range("A1")
         .AutoFilterMode = False
      End With
   Next i
   For Each sht In ary
      With Sheets(CStr(sht))
         .UsedRange.EntireColumn.AutoFit
         .Range("H:K").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
         With .Sort
            .SortFields.Clear
            .SortFields.Add key:=Range("F1"), SortOn _
               :=xlSortOnValues, order:=xlAscending, DataOption:=xlSortNormal
            .SetRange Range("A2:K10000")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
         End With
      End With
   Next sht
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,793
Messages
6,126,936
Members
449,349
Latest member
Omer Lutfu Neziroglu

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