Advanced Filtering VBA Code Issue

kc_native

Board Regular
Joined
Jan 20, 2009
Messages
176
I am trying to do an advanced filter in VBA and the code structure came from a macro I recorded, with some changes to range references. I am getting the following error: "Run-time error '1004': Application-defined or object-defined error. I would appreciate if someone could help me identify where in the code I have gone wrong. In the previous section of the code I am creating a sheet two and renaming it Target Projects. Do I need to add that new sheet name to the "CopyToRange=Range(A1)" portion of the code? The Target Project sheet is activated after adding it and re-naming it, before this section of the code executes.

Code:
[B][COLOR=#0000FF]Sheets("DATA80100").Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, DataMoCol)) _
        .AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Workbooks("Macro Launch Sheet).xlsm") _
        .Sheets("Sheet1").Range("BA1:BA36"), CopyToRange:=Range("A1"), Unique:=False[/COLOR][/B]

Thanks for any help you can give this novice.
 
I'm not sure if the criteria range in another workbook is the culprit (not tested) but, for sure, you must specify the location of the criteria range in such workbook (sheet and range)

M.

This is what the code should have looked like when I copied it into my post, and as you can see, I am referencing the sheet and range for the criteria.

Code:
[[B][COLOR=#0000ff]Sheets("DATA80100").Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, DataMoCol)) _
        .AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Workbooks("Macro Launch Sheet.xlsm") _
        .Sheets("Sheet1").Range("BA1:BA36"), CopyToRange:=Range("A1"), Unique:=False[/COLOR][/B]/CODE]
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you mean the workbook 'Macro Launch Sheet.xlsm' isn't open then I'm pretty sure that could be a problem.
 
Upvote 0
This is what the code should have looked like when I copied it into my post, and as you can see, I am referencing the sheet and range for the criteria.

Code:
[[B][COLOR=#0000ff]Sheets("DATA80100").Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, DataMoCol)) _
        .AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Workbooks("Macro Launch Sheet.xlsm") _
        .Sheets("Sheet1").Range("BA1:BA36"), CopyToRange:=Range("A1"), Unique:=False[/COLOR][/B]/CODE][/QUOTE]

Questions
Why are you using BA1:BA36?  
BA1:BA36 contains texts or numbers?
Is this an array of criteria? If so, i *think* it should be a horizontal array - maybe with Application.Transpose(....) 

M.
 
Upvote 0
oops...

Disregard this
"If so, i *think* it should be a horizontal array - maybe with Application.Transpose(....) "

I was thinking when we use an array of criteria in normal filter. In advanced filter an OR criteria should be vertical.

M.
 
Upvote 0
If you mean the workbook 'Macro Launch Sheet.xlsm' isn't open then I'm pretty sure that could be a problem.

No, it's open. It's the workbook the macro is executed from, so it will always be open when this code is run.
 
Upvote 0
oops...

Disregard this
"If so, i *think* it should be a horizontal array - maybe with Application.Transpose(....) "

I was thinking when we use an array of criteria in normal filter. In advanced filter an OR criteria should be vertical.

M.

I have re-written the code to copy the range of criteria into the worksheet I am filtering from so I am no longer referencing an external worksheet, but I still get the same error.

Maybe I should stop trying to use the advanced filter, and just make a copy of the worksheet, and then do a For Loop to delete all rows that don't contain the project numbers in the criteria? So use an index or array of numbers to compare to that would determine whether the row should stay or be deleted? I know someone on this board gave me some code a long time ago that would do something like that, but don't think I kept it.

What say you? Do you have any ideas on how it could be done other than advanced filtering?

Thanks!
 
Upvote 0
You didn't answer the question about what AB1:AB36 (criteria range) contains.
Could you please tell us?

M.
 
Upvote 0
You didn't answer the question about what AB1:AB36 (criteria range) contains.
Could you please tell us?

M.

Sorry, it is just a list of 30 or so 5 and 6-digit numbers that represent projects we analyze. So it looks like the following when placed in a column:

567234
789101
231223
121245
99872
and so on....
 
Upvote 0
You didn't answer the question about what AB1:AB36 (criteria range) contains.
Could you please tell us?

M.

Found this code in my notes. I could just make a copy of the worksheet with the data in it, and the put my list of project numbers in the filter criteria array to filter the data in the new tab I just created. What do you think?

Code:
With Range("B:B”)
.AutoFilter Field:=2, Criteria1 :=Array(“xxxxxx”,”xxxxx”,"xxxxxx”), Operator = xlFilterValues
.SpecialCells(xlCellTypeVisible).EntireRow.Delete (xlUp) 
End With

If I use numbers in the array, do I need to include the quotes around each number?
 
Upvote 0
I think this is fine, but be aware that if the criteria array is vertical and contains numbers (not text) you must use something like

Code:
Dim v as Variant

v = Split(Join(Application.Transpose(Range("AB1:AB36"))))
With Range("B:B”)
  .AutoFilter Field:=2, Criteria1 :=v, Operator = xlFilterValues
  .SpecialCells(xlCellTypeVisible).EntireRow.Delete (xlUp) 
End With


Take a look at this thread
https://www.mrexcel.com/forum/excel...er-multiple-criteria-not-working-numbers.html

M.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
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