Hi there, I am trying to create a workbook that allows employees to enter working from home hours in one excel workbook - each employee has their own tab and they fill in the days when they were at home. Column D fills colour blue on a day the work from home.
There is a separate workbook as Holiday Planner(created years ago) which is used to keep track of employees on leave, offsite etc. with different colours. So if John Doe edits his sheet(above) the corresponding date below will change to blue.
I am trying to find the correct cell using the below code but I think I am mixing up the workbooks when I open holidayPlanner before running the code - giving the error "Method 'Intersect' of _Application' failed" . If you have a better/different approach to the code I'd love to see it.
Any help appreciated!
Date | Start Time | Finish Time | WFH |
---|---|---|---|
15 Mar 22 | |||
16 Mar 22 | 8:30 | 17:00 | *Blue* |
17 Mar 22 | |||
18 Mar 22 |
There is a separate workbook as Holiday Planner(created years ago) which is used to keep track of employees on leave, offsite etc. with different colours. So if John Doe edits his sheet(above) the corresponding date below will change to blue.
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
With Sh
Dim clrCol, cl As Range
Dim wbk As Workbook
Dim hws As Worksheet
Dim ddate, name As String
Dim dayNum, monthNum As Integer
Set wbk = Workbooks.Open("C:\Users\myName\Desktop\HolidayPlanner.xlsx")
Set hws = wbk.Worksheets("2022")
Set clrCol = Range("G" & Target.Row)
'Detects changes made to column E(Finish Time)
If Not Application.Intersect(Target, Range("E:E")) _
Is Nothing Then
If Target.Value = "" Then
'Revert blue cell to clear
clrCol.Interior.ColorIndex = 0 'No fill
'Name to search in col B of holiday planner
name = ActiveSheet.name
ddate = Cells(Target.Row, 2).Value
monthNum = month(ddate)
dayNum = day(ddate)
MsgBox name & " " & dayNum & " " & monthNum
Else
'Change colour to blue for WFH
clrCol.Interior.Color = RGB(0, 0, 255) 'blue(wfh)
'Name to search in col B of holiday planner
name = ActiveSheet.name
ddate = Cells(Target.Row, 2).Value
monthNum = month(ddate)
dayNum = day(ddate)
With hws
'Get the first row of March in Holiday Planner
'Then will search that row for the dayNum
Set FindRow = .Range("H:H").Find(What:=MonthName(monthNum), LookIn:=xlValues)
MsgBox FindRow
End With
End If
End If
End With
bm_Safe_Exit:
Application.EnableEvents = True
End Sub
Any help appreciated!