I am trying to filter by a list of Condition from the Condition wb to use for the Order wb. I use a checkEmpty range in order to check if there are no matching value then I will clear the filter and start with the next condition. But my code doesn't work and the error is "Range of object_worksheet" failed. I get the error because even there is no matching value (empty range), the code still jump to Else condition. Here is my code:
Sub Order()
Dim start As Double
Dim strKeyWord As String
Dim myCount As Integer
Dim checkEmpty As Range
Dim lRow1 As Long
Dim wsOrder As Worksheet
Dim wsCondition As Worksheet
Dim wbOrder As Workbook
Dim wbCondition As Workbook
Dim OrderFile As String
Dim ConditionFile As String
'Open Order wb
OrderFile = Application.GetOpenFilename()
Set wbOrder = Workbooks.Open(OrderFile)
Set wsOrder = wbOrder.Worksheets(1)
'Open Condition wb
ConditionFile = Application.GetOpenFilename()
Set wbCondition = Workbooks.Open(ConditionFile)
Set wsCondition = wbCondition.Worksheets(1)
'using the CountA ws function (all non-blanks)
myCount = Application.CountA(wsCondition.Range("A:A")) - 1
start = 2
For I = 1 To myCount Step 1
strKeyWord = wsCondition.Range("A" & start)
wsOrder.Range("R:R").AutoFilter Field:=1, Criteria1:="=*" & strKeyWord & "*"
'lRow1 = WorksheetFunction.Max(wsOrder.Range("I65536").End(xlUp).Row)
Set checkEmpty = wsOrder.Range("I2:I100").SpecialCells(xlCellTypeVisible)
If checkEmpty Is Nothing Then
On Error Resume Next
wsOrder.ShowAllData
On Error GoTo 0
Else
wsOrder.Range("I2", Range("I" & Rows.Count).End(xlUp)).Copy
With wsCondition
.Cells(.Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial
End With
End If
start = start + 1
Next I
End Sub
Thank you very much!
For Iloop but you never useIinstead you count a separatestartvariable. I think you should use something likeFor start = 2 To myCount Step 1 … Next startand omit thestart = 2andstart = start + 1lines.wsOrder.Range("I2", Range("I" & Rows.Count)likewsOrder.Range("I2", wsOrder.Range("I" & Rows.Count)