Procedure fails on some sheets only

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Should return the First and Last Row numbers in column "C" for Yr in Sheet sht
VBA Code:
Sub YrRange(Yr, ByRef First, ByRef Last, sht)
    Last = ThisWorkbook.Sheets(sht).Range("C:C").Find(What:=Yr & "*", LookAt:=xlWhole, lookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    First = ThisWorkbook.Sheets(sht).Range("C:C").Find(What:=Yr & "*", LookAt:=xlWhole, lookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row
End Sub
It works correctly in some sheets but in one other I get
Run-time error '91':
Object variable or With block variable not set.
The only difference I can see in the sheet not working is it has only columns A-Z showing. And sometimes a message about can't shift object off sheet when Cut/insert row.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Run-time error '91':
Object variable or With block variable not set.
This error is because the column is empty or because it didn't find the information.

The best thing is to set the result of the Find method in an object, then you check if the object is not empty, that is, it contains information and you can now obtain the row numbers:

VBA Code:
Sub YrRange(Yr, ByRef First, ByRef Last, sht)
  Dim f As Range
  
  With ThisWorkbook.Sheets(sht).Range("C:C")
    Set f = .Find(Yr & "*", , xlValues, xlWhole, xlByRows, xlPrevious)
    If Not f Is Nothing Then
      Last = f.Row
    
      Set f = .Find(Yr & "*", , xlValues, xlWhole, xlByRows, xlNext)
      First = f.Row
    
    Else
      MsgBox "Column Empty"
    End If
  End With
End Sub

Try and comment.
 
Upvote 0
Solution
Thanks Dante, it reported "Column Empty" which made me realise in this sheet it's not Col C.
Duh! My apologies.
 
Upvote 1

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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