Error Message when no data available

Foynxter1

New Member
Joined
Sep 29, 2016
Messages
40
Hi, appreciate any help with this frustrating problem.

I am trying to retrieve statistical data from various monthly sheets ( Jan, Feb, March etc.,) to a single master sheet.
I have set the code up to retrieve the data and all goes well until we get to August. As the data is not yet available (entered to the sheet) I get the error message "Run-Time error 91" - Object Variable or With block variable not set.
I have checked the sheet names for the various months and they are correct - the code "trips" when it cannot find any data present

Extract of the code below - I am sure I'm missing something simple but can't see it. Appreciate any help. Thanks
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cust As String
Dim ThisRow As Long ' make sure to declare all the variables and appropiate types
ThisRow = Target.Row
'protect Header row from any changes
If (ThisRow = 1) Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Header Row is Protected."
Exit Sub
End If


If Target.Column = 2 Then
'MsgBox "2"
cust = Range("B" & Target.Row)
'MsgBox cust

Cells(Target.Row, 6).Value = Sheets("Jan").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 1).Value
Cells(Target.Row, 7).Value = Sheets("Jan").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 2).Value
Cells(Target.Row, 8).Value = Sheets("Jan").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 3).Value
Cells(Target.Row, 9).Value = Sheets("Feb").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 1).Value
Cells(Target.Row, 10).Value = Sheets("Feb").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 2).Value
Cells(Target.Row, 11).Value = Sheets("Feb").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 3).Value
Cells(Target.Row, 27).Value = Sheets("Aug").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 1).Value
Cells(Target.Row, 28).Value = Sheets("Aug").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 2).Value
Cells(Target.Row, 29).Value = Sheets("Aug").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 3).Value
Cells(Target.Row, 30).Value = Sheets("Sep").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 1).Value
Cells(Target.Row, 31).Value = Sheets("Sep").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 2).Value
Cells(Target.Row, 32).Value = Sheets("Sep").Range("B2:E100").Find(What:=cust, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 3).Value
End if
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If you want to suppress the error you can use:

Code:
On Error Resume Next ' Suppress Errors that occur within the following code block

'Your code here

On Error GoTo 0 'Reset Error Handling
 
Upvote 0
Thanks Matt - that seems to have resolved that issue. If I could possibly impose a little further though...
Quite often I will copy 3 or 4 names at a time to the master spreadsheet but it only updates the first record - the other 2 or 3 do not alter. I'm sure there is a simple (if you know how) solution to get it to loop through any affected records. It would be appreciated if you could offer any assistance. Thank you in advance - it's driving me nuts !!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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