AutoFilter Error message

toant

New Member
Joined
Apr 24, 2021
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi, I am new to VBA and need help with this. I have an autofilter set to filter Sheet "Warehouse" column A with the set values in column A of Sheet "Filter" and copy rows that exist to Sheet "Inventory". The problem I come across is when values in Filter does not exist in "Warehouse" I get the error "No cells were found". How can I modify to get around this?

VBA Code:
Sub With_AutoFilter()
Sheets("Inventory").Rows("2:" & Sheets("Inventory").Rows.Count).ClearContents
 Dim lr As Long, c As Range
 Application.ScreenUpdating = False
 lr = Sheets("Warehouse").Cells(Rows.Count, 1).End(xlUp).Row
 For Each c In Sheets("Filter").Range("A2", Sheets("Filter").Range("A" & Rows.Count).End(xlUp))
 With Sheets("Warehouse")
 .AutoFilterMode = False
 .Range("A1:A" & lr).AutoFilter field:=1, Criteria1:=c.Value
 .Range("A2:A" & lr).SpecialCells(12).EntireRow.Copy Sheets("Inventory").Cells(Rows.Count, "A").End(xlUp)(2)
 .AutoFilterMode = False
 End With
 Next c
 Application.ScreenUpdating = True
 
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
VBA Code:
Sub With_AutoFilter()
Sheets("Inventory").Rows("2:" & Sheets("Inventory").Rows.Count).ClearContents
 Dim lr As Long, c As Range
 Application.ScreenUpdating = False
 lr = Sheets("Warehouse").Cells(Rows.Count, 1).End(xlUp).Row
 For Each c In Sheets("Filter").Range("A2", Sheets("Filter").Range("A" & Rows.Count).End(xlUp))
 With Sheets("Warehouse")
 .AutoFilterMode = False
 .Range("A1:A" & lr).AutoFilter field:=1, Criteria1:=c.Value


' This has been added. 
' It counts in worksheet WAREHOUSE if the value you are looking for exists.
' If the value exists, it will copy over the data.
' If the value does not exist, it will abort the copy/paste, and proceed to the next lookup value.
NumberOfRowsWarehouse = WorksheetFunction.CountIf(Sheets("Warehouse").Range("A2:A" & lr), c.Value)
If NumberOfRowsWarehouse > 0 Then
 .Range("A2:A" & lr).SpecialCells(12).EntireRow.Copy Sheets("Inventory").Cells(Rows.Count, "A").End(xlUp)(2)
End If




 .AutoFilterMode = False
 End With
 Next c
 Application.ScreenUpdating = True
 
End Sub
 
Upvote 0
Solution
VBA Code:
Sub With_AutoFilter()
Sheets("Inventory").Rows("2:" & Sheets("Inventory").Rows.Count).ClearContents
Dim lr As Long, c As Range
Application.ScreenUpdating = False
lr = Sheets("Warehouse").Cells(Rows.Count, 1).End(xlUp).Row
For Each c In Sheets("Filter").Range("A2", Sheets("Filter").Range("A" & Rows.Count).End(xlUp))
With Sheets("Warehouse")
.AutoFilterMode = False
.Range("A1:A" & lr).AutoFilter field:=1, Criteria1:=c.Value


' This has been added.
' It counts in worksheet WAREHOUSE if the value you are looking for exists.
' If the value exists, it will copy over the data.
' If the value does not exist, it will abort the copy/paste, and proceed to the next lookup value.
NumberOfRowsWarehouse = WorksheetFunction.CountIf(Sheets("Warehouse").Range("A2:A" & lr), c.Value)
If NumberOfRowsWarehouse > 0 Then
.Range("A2:A" & lr).SpecialCells(12).EntireRow.Copy Sheets("Inventory").Cells(Rows.Count, "A").End(xlUp)(2)
End If




.AutoFilterMode = False
End With
Next c
Application.ScreenUpdating = True

End Sub
Thank you. That worked.
 
Upvote 0

Forum statistics

Threads
1,214,629
Messages
6,120,630
Members
448,973
Latest member
ChristineC

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