I have this macro that add a row after the last row that contains data, and populates this new row with values in different columns.
see code below, it works perfect when a file contains the keyword "Rainguards", but if a file does not contain this keyword, the following error message pops up.
"Object variable or with block variable not set"
Is there a simple way to make this error message to go away?
see code below, it works perfect when a file contains the keyword "Rainguards", but if a file does not contain this keyword, the following error message pops up.
"Object variable or with block variable not set"
Is there a simple way to make this error message to go away?
VBA Code:
Public sr As Long, n As Long, rr As Long, Data As String, TargetCell As Range, v As String, s As String
Sub Rainguards()
sr = 2 'Starting Row
n = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*Rainguards*")
'MsgBox n
lr6 = lr3 + 1 'this is the new row at the bottom that we are going to write the data to.
Cells(lr6, "A") = Cells(lr3, "A")
Cells(lr6, "B") = "."
Cells(lr6, "C") = n 'number of Rainguards
Cells(lr6, "I") = "Purchased"
Cells(lr6, "K") = "Rainguards"
Set TargetCell = Range("K" & sr & ":K" & lr).Find(What:="Rainguards", _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
rr = TargetCell.Row
Data = ""
v = Cells(rr, "K")
s = Cells(rr, "N")
If v Like "*170*" Then Data = "F89998"
If v Like "*580*" Then Data = "F89998"
If v Like "*170*" And s Like "*Hernando*" Then Data = "F89998U"
If v Like "*580*" And s Like "*Hernando*" Then Data = "F89998U"
If v Like "*225*" Then Data = "F89999"
If v Like "*440*" Then Data = "F89999"
If v Like "*667*" Then Data = "F90003"
Cells(lr6, "D") = Data
If Cells(lr6, "C").Value Like "*0*" Then
Cells(lr6, 4) = "."
End If
End Sub