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
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