I'm using an advanced filter to copy data from a main worksheet 'SDC' and adding into different workbook tables with fewer headings. The filter criteria are created on a different sheet.
Now, I use the same code for the same data, but adding it into other workbook tables earlier in the code, and it works absolutely fine. Last week the same code worked for this new workbook, but today it decided to start giving errors.... without having changed any code.
I've spent 6 hours trying to figure out the problem, but no success yet.
I did a couple of tests to see where the issues come in:
- When I use the same headings as the destination table (but put them on a new sheet), and manually do the advanced filter, It works fine.
- When I tried manually doing the advanced filter to the one destination workbook, it worked, and then the next time after that nothing happens. Very strange...
- I've checked all the headings to make sure they match, no spaces anywhere. Still, nothing gets added to the destination workbook tables when I run the advanced filter manually.
- I've checked the ranges in the watch window when the macro runs step by step, and everything seems fine. (until the error comes up with the advanced filter)
I don't understand how it worked before, and suddenly starting giving errors. Then it worked for one table and then not. It seems to be this one workbook that has issues or maybe because I'm using multiple criteria for one column? (but it works manually). What am I missing?
I get an error when the advanced filter has to run in the macro: Run-time error '1004': AdvancedFilter method of range class failed on the code line
SDCRange.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=OOSdirectCrit, CopyToRange:=copyToRng, Unique:=False
Code I'm currently using (which works earlier in the code, just adding to different workbook tables)
Sub StockAvail(wb As Workbook, ws As Worksheet)
Dim SATemp As String
Dim SATempF As String
Dim wsSDC As Worksheet
Set wsSDC = MainWB.Worksheets(1)
SATemp = Dir(TemplPath & "\*Stock Availability*.xls*")
SATempF = TemplPath & "\" & SATemp
'Check if workbook open if not open it
If Not wbOpen(SATempF, wb) Then
Set wb = Workbooks.Open(SATempF)
End If
'Call function add Vendor,Region, etc
Call VInfoAdd(wb, ws)
'Create Filter Criteria ranges
With MainWB.Worksheets.Add
.Name = "FltrCrit"
Dim FltrCrit As Worksheet
Set FltrCrit = MainWB.Worksheets("FltrCrit")
End With
'Create Filter Criteria ranges
With FltrCrit
Dim OOSdirectCrit As Range
Dim myLastColumn As Long
'Create OOS - Direct Filter Criteria Range
.Cells(1, "A") = "Out of Stock - Direct"
.Cells(2, "A") = "MS"
.Cells(3, "A") = "=4"
.Cells(2, "B") = "SOH"
.Cells(3, "B") = "=0"
.Cells(2, "C") = "Sup.Vend."
.Cells(3, "C") = "<>MA05"
.Cells(2, "D") = "Sup.Vend."
.Cells(3, "D") = "<>MA07"
.Cells(2, "E") = "Sup.Vend."
.Cells(3, "E") = "<>MA09"
.Cells(2, "F") = "Sup.Vend."
.Cells(3, "F") = "<>MA11"
'get last column, set range name
With .Cells
'find last column of data cell range
myLastColumn = .Cells(2, Columns.Count).End(xlToLeft).Column
'specify cell range
Set OOSdirectCrit = .Range(.Cells(2, "A:A"), .Cells(3, myLastColumn))
End With
'Copy Filtered data to specified tables
Dim tblFiltered As ListObject
Dim copyToRng As Range, SDCRange As Range
'OOS - Direct
'Store Filtered table in variable
Set tblFiltered = wb.Worksheets("Out of Stock (Direct Supply)").ListObjects("Table_Out_of_Stock_Direct")
'Remove Filtered table Filters
tblFiltered.AutoFilter.ShowAllData
'Set Copy to range on Filtered sheet table
Set copyToRng = tblFiltered.HeaderRowRange
Set SDCRange = wsSDC.ListObjects("Table_SDCdata").Range
'Use Advanced Filter
SDCRange.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=OOSdirectCrit, CopyToRange:=copyToRng, Unique:=False
The code is for 2 separate tables with different criteria, the first one to exclude DC codes and second one to include only DC codes. I only included the first one's code above, since it's the first issue (2nd one is the same but should be fixed if I can find the problem with the first one, since it's the same issue)
Any help will be greatly appreciated, I'm lost at where to look for problems with it at this point.