Here's the summary of what I'm trying to do: I have two files, "yyy" is an extract from a system keeping track of sicknesses and holidays, "xxx" file is where we manually note down those things, so I'm trying to make a macro that would cross check the two and highlight consistency/inconsistency in the extract.
Problem: When I run the macro, I get an error "object variable or with block variable not set" when it gets to the following
Set y = Range(Workbooks("xxx.xlsm").Sheets("xxx ").Range("J2:NZ2").Find(dates.Value).Address) I tried to swap between the dates.value and dateStr as reference for what to find, but get the same error.
PS. I checked all the ranges over and over and over, everything seem to be correct. PPS. I probably have some extra defined bits, which aren't present in the code otherwise, apologies for the untidy code. I'm trying to mover past this error to finish the rest of it...
Many thanks in advance!
Option Explicit
Sub sickhol_check()
Dim names, dates, schedDates, n, schedName, x, y As Range
Dim wb, WBschedule As Workbook
Dim ws, WSschedule As Worksheet
Dim nameStr As String
Dim dateStr As Long
Set wb = Workbooks("yyy.xlsm")
Set ws = Sheets("Sheet1")
Set WBschedule = Workbooks("xxx.xlsm")
Set WSschedule = WBschedule.Sheets("xxx")
Set names = ThisWorkbook.Sheets("Sheet1").Range("D2:D1882") ‘yyy workbook
Set schedName = Workbooks("xxx.xlsm").Sheets("xxx ").Range("F4:F300")
Set schedDates = Workbooks("xxx.xlsm").Sheets("xxx ").Range("J2:NZ2")
For Each n In names ‘working in yyy workbook
If n.Value <> "" Then
Set dates = ThisWorkbook.Sheets("Sheet1").Range(n.Offset(0, -3).Address)
nameStr = n.Value
dateStr = dates.Value
If n.Value = schedName.Find(n.Value) Then
Set x = Range(Workbooks("xxx.xlsm").Sheets("xxx ").Range("F4:F300").Find(nameStr).Address)
Set y = Range(Workbooks("xxx.xlsm").Sheets("xxx ").Range("J2:NZ2").Find(dates.Value).Address)
If n.Offset(0, 11).Value = "Sick Leave" Then
If Workbooks("xxx.xlsm").Sheets("xxx ").Cells(x.Row, y.Column).Value = "Sick" Or Workbooks("xxx.xlsm").Sheets("xxx ").Cells(x.Row, y.Column).Value = "Sick - pending" Or Workbooks("xxx.xlsm").Sheets("xxx").Cells(x.Row, y.Column).Value = "Half-day Sick" Then
With n
.Interior.Color = RGB(0, 255, 0)
End With
Else:
With n
.Interior.Color = RGB(255, 0, 0)
End With
GoTo a:
End If
ElseIf n.Offset(0, 11).Value = "Holiday Annual Leave" Then
If Workbooks("xxx.xlsm").Sheets("xxx").Cells(x.Row, y.Column).Value = "Hol. Approved" Or Workbooks("xxx.xlsm").Sheets("xxx").Cells(x.Row, y.Column).Value = "Half-day Hol." Then
With n
.Interior.Color = RGB(0, 255, 0)
End With
Else:
With n
.Interior.Color = RGB(255, 0, 0)
End With
GoTo a:
End If
ElseIf n.Offset(0, 11).Value = "Leave - Unpaid" Then
If Workbooks("xxx.xlsm").Sheets("xxx").Cells(x.Row, y.Column).Value = "TimeOff Approv." Or Workbooks("xxx.xlsm").Sheets("xxx").Cells(x.Row, y.Column).Value = "Unavailable" Then
With n
.Interior.Color = RGB(0, 255, 0)
End With
Else:
With n
.Interior.Color = RGB(255, 0, 0)
End With
GoTo a:
End If
End If
MsgBox x.Address & " " & y.Address
Else: GoTo a:
End If
Else: GoTo a:
End If
a: Next
Workbooks("xxx.xlsm")is not correct.... a workbook name is not the same as a filename.Workbooks("xxx.xlsm").Sheets...when you have already qualified object:WBschedule. I would just useWBschedule.Sheets.... Also, is Workbooks("xxx.xlsm") already open?yis a range, but the other variables on that line are variants. Same withwbandws.Set ws=Sheets("Sheet1")- is that meant to refer to the currently activeworkbook as you've not said which book it's in.Set dates- you're setting it to the address value of n - 3 columns, why not just set it to reference n-3 columns as both refs are on the same sheet:Set dates = n.Offset(, -3).Set x = Range...- does sheetxxxreally have a space after it?