VBA copy entire row based on multiple criteria

GEO81

New Member
Joined
Feb 9, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello!

I'm working on a macro and i need some help.
I need to copy the entire rows from another workbook to my active workbook if a condition is met. The condition is a combination or 3 other conditions conected with OR.

I'm giving an example below.
Lets say i have the below table and my condition is: If Value1 is =<30 OR Value2=<100 or Value3 =<50 THEN copy the entire row to my active workbook/worksheet.
Just to mention that i need the Input Values 30, 100, 50 to have flexibility to amend. So I need an Input Box to prompt the user to enter those 3 conditions.
So should i use a UserForm?

Also i have to ensure that Blank cells will not be considered as cells with Value=0.

Data1Data2Data3Value1Value2Value3
Name1A
1​
20​
150​
Name2B
2​
10​
200​
10​
Name3C
3​
100​
15​
Name4D
4​
100​
50​
Name5E
5​
80​
0​
60​
Name6F
6​
35​
Name7G
7​
40​
Name8H
8​
120​
Name9I
8​
80​

The Result should be the following Table:

Data1Data2Data3Value1Value2Value3
Name1A
1​
20​
150​
Name2B
2​
10​
200​
10​
Name3C
3​
100​
15​
Name4D
4​
100​
50​
Name5E
5​
80​
0​
60​
Name7G
7​
40​

Any help is more than welcome!
Thanks!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hello,​
or just using some cells as criterion to be combined with an easy advanced filter …​
 
Upvote 0
Hello,​
or just using some cells as criterion to be combined with an easy advanced filter …​
Actually my vba is more complicated and i'm retreiving data from many excel workbooks and worksheets. So i'm not sure how the advanced filter will work.
 
Upvote 0
Hello,​
or just using some cells as criterion to be combined with an easy advanced filter …​
Hi,

I figure out something but i saw that the advanced filter requires the headers to be all the same. in my case, the Columns at the different excel sheets have the same values but the headers may differ on the description.
So the command "CopyToRange:=Sheets("sheet2").Range("I10:N10")" will not work.
How can i copy the entire row? Any suggestion please?

Below is my example

Sub AdvancedFilterCopyDemo()

Range("A:F").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("J3:L6"), _
CopyToRange:=Sheets("sheet2").Range("I10:N10")
End Sub
 
Upvote 0

Use a filter whatever according to some cells or an userform then copy the data …​
 
Upvote 0
You can also use an advanced filter to filter in place then use the Range.Copy method to copy the data …​
 
Upvote 0
the Columns at the different excel sheets have the same values but the headers may differ on the description.
That shouldn't matter provided the original data is to be copied in the same column order.
You should still be able to do an xlFilterCopy

Assuming ..
  • The first table shown in post #1 is on Sheet1, starting in cell A1
  • There is nothing to the right of this table (code can be adapted if there is)
  • The 'different' headers on Sheet2 are in I10:N10
  • There is nothing below those headers in Sheet2
.. then give this a try with a copy of your workbook.

VBA Code:
Sub FilterAndCopy()
  Dim aVals As Variant
  Dim InputVals As String
  Dim rCrit As Range
  
  InputVals = InputBox("Enter upper limits for Val 1, Val 2, and Val 3, separated by spaces. eg 30 100 50")
  aVals = Split(InputVals)
  If UBound(aVals) = 2 Then
    With Sheets("Sheet1").Range("A1").CurrentRegion
      Set rCrit = .Cells(1, .Rows.Count + 2).Resize(2)
      rCrit.Cells(2).Formula = "=OR(AND(D2<=" & aVals(0) & ",D2<>""""),AND(E2<=" & aVals(1) & ",E2<>""""),AND(F2<=" & aVals(2) & ",F2<>""""))"
      .AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=Sheets("Sheet2").Range("I11"), Unique:=False
      rCrit.ClearContents
    End With
    Sheets("Sheet2").Range("I11:N11").Delete Shift:=xlUp
  End If
End Sub
 
Upvote 0
That shouldn't matter provided the original data is to be copied in the same column order.
You should still be able to do an xlFilterCopy

Assuming ..
  • The first table shown in post #1 is on Sheet1, starting in cell A1
  • There is nothing to the right of this table (code can be adapted if there is)
  • The 'different' headers on Sheet2 are in I10:N10
  • There is nothing below those headers in Sheet2
.. then give this a try with a copy of your workbook.

VBA Code:
Sub FilterAndCopy()
  Dim aVals As Variant
  Dim InputVals As String
  Dim rCrit As Range
 
  InputVals = InputBox("Enter upper limits for Val 1, Val 2, and Val 3, separated by spaces. eg 30 100 50")
  aVals = Split(InputVals)
  If UBound(aVals) = 2 Then
    With Sheets("Sheet1").Range("A1").CurrentRegion
      Set rCrit = .Cells(1, .Rows.Count + 2).Resize(2)
      rCrit.Cells(2).Formula = "=OR(AND(D2<=" & aVals(0) & ",D2<>""""),AND(E2<=" & aVals(1) & ",E2<>""""),AND(F2<=" & aVals(2) & ",F2<>""""))"
      .AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=Sheets("Sheet2").Range("I11"), Unique:=False
      rCrit.ClearContents
    End With
    Sheets("Sheet2").Range("I11:N11").Delete Shift:=xlUp
  End If
End Sub
HI Peter! Thank you for your suggestion and sorry for the late reply.
I got sick and then busy with other priorities!
I have applied the logic you mentioned and worked for simple tables.
Unfortunately some of my excel files have merged data so I need to work a lit bit to make them listed properly.
Thank you again!
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,051
Members
452,542
Latest member
Bricklin

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