Object Variable or With Block Variable Not Set Error

NorthbyNorthwest

Board Regular
Joined
Oct 27, 2013
Messages
178
Office Version
  1. 365
Hi, everyone. Could someone please tell me why I get an "object variable or with block variable not set" error when I run this code? Note: I do not get an error on first run of line. But I do get it on second run (line in green text). What do I have wrong?

VBA Code:
Sub ShuffleBoard()
Dim ws As Worksheet
Dim RowNum As Long
Dim ReturnRowNum As Long
Dim SearchRange As Range
Dim rng As Range
Dim rng1 As Range
Dim FindRow As Range

Call ShowCalculator

Worksheets("Assignments").Activate
Set ws = Worksheets("Assignments")

Set SearchRange = ws.Range("AP6:AP52")
Set FindRow = SearchRange.Find("Auditor", LookIn:=xlValues, LookAt:=xlWhole)

    If Not FindRow Is Nothing Then ReturnRowNum = FindRow.Row
    RowNum = FindRow.Row
    
    Set rng = ws.Range("AP6")
    rng.Cut
    ws.Range(Cells(RowNum, 42), Cells(RowNum, 42)).Insert Shift:=xlDown
    
Set SearchRange = ws.Range("M8:V39")
Set FindRow = SearchRange.Find("Auditor", LookIn:=xlValues, LookAt:=xlWhole)

    If Not FindRow Is Nothing Then ReturnRowNum = FindRow.Row
  [COLOR=rgb(65, 168, 95)]  RowNum = FindRow.Row[/COLOR]

    Set rng1 = ws.Range("M8")
    rng1.Cut
    ws.Range(Cells(RowNum, 13), Cells(RowNum, 13)).Insert Shift:=xlDown

Range("AN1").Select
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I do not get an error on first run of line. But I do get it on second run
The second run searches in another range.

Try this way:
VBA Code:
Sub ShuffleBoard()
  Dim ws As Worksheet
  Dim RowNum As Long
  Dim SearchRange As Range, rng As Range, rng1 As Range, FindRow As Range
 
  Call ShowCalculator
  Set ws = Worksheets("Assignments")
 
  Set SearchRange = ws.Range("AP6:AP52")
  Set FindRow = SearchRange.Find("Auditor", LookIn:=xlValues, LookAt:=xlWhole)
  If Not FindRow Is Nothing Then
    RowNum = FindRow.Row
    Set rng = ws.Range("AP6")
    rng.Cut
    ws.Range(ws.Cells(RowNum, 42), ws.Cells(RowNum, 42)).Insert Shift:=xlDown
  Else
    MsgBox "The word Auditor does not exist in the range 'AP6:AP52'"
  End If
 
  Set SearchRange = ws.Range("M8:V39")
  Set FindRow = SearchRange.Find("Auditor", LookIn:=xlValues, LookAt:=xlWhole)
  If Not FindRow Is Nothing Then
    RowNum = FindRow.Row
    Set rng1 = ws.Range("M8")
    rng1.Cut
    ws.Range(ws.Cells(RowNum, 13), ws.Cells(RowNum, 13)).Insert Shift:=xlDown
  Else
    MsgBox "The word Auditor does not exist in the range 'M8:V39'"
  End If

  Range("AN1").Select
End Sub

NOTES:
To put color in the code, you must use the "RICH code" icon:
1736713023598.png

You must also use the sheet reference in each cell or range object:
Rich (BB code):
ws.Range(ws.Cells(RowNum, 42), ws.Cells(RowNum, 42)).Insert Shift:=xlDown

🤗
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,225,626
Messages
6,186,092
Members
453,337
Latest member
fiaz ahmad

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