How to get my code to handle exceptions

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
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?

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
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
above your 'set targetcell' statement, add.
VBA Code:
on error resume next
 
Upvote 0
Solution

Forum statistics

Threads
1,215,461
Messages
6,124,958
Members
449,200
Latest member
indiansth

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