Anirudh Chauhan
Anirudh Chauhan

Reputation: 111

Getting an error in VBA

ERROR: Object variable or with block variable not set

CODE :

x.Worksheets.Add().Name = "ab"

x.Worksheets.Add().Name = "bc"

LastRow = x.Sheets("Summary").Columns("A").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

Dim pos As Range

For j = 1 To LastRow

    Set pos = x.Sheets("Summary").Range("A1:X" & j).Find(What:="SAP_ALL", LookIn:=xlValues, LookAt:=xlWhole, _
                MatchCase:=False, SearchFormat:=False)

    a = pos.Column

Next j

Error is on the line a = pos.Column

Upvotes: 2

Views: 40

Answers (1)

ThunderFrame
ThunderFrame

Reputation: 9471

If the Find operation didn't find anything, then pos will be Nothing.

You need a guard clause:

If Not pos Is Nothing Then
    a = pos.Column
End If

Upvotes: 1

Related Questions