Best way to detect changes in one workbook and make changes in another workbook

ellak123

New Member
Joined
Mar 23, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
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.
DateStart TimeFinish TimeWFH
15 Mar 22
16 Mar 228:3017: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.
Capture1.PNG


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

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi ellak123,

This looks like a good one to try. If you can copy and paste the calendar like you did the table above it, I will try it.

 
Upvote 0
Hi ellak123,

This looks like a good one to try. If you can copy and paste the calendar like you did the table above it, I will try it.

HolidayPlanner.xlsx
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAK
63910111213March
64EMPLOYEE12345678910111213141516171819202122232425262728293031
65
66
67
68
69
70
71
72
73
74
75
76
77
78
791/2
80
81
82
83
84
85
86
87
88
89
2022
Cell Formulas
RangeFormula
D64:AG64D64=C64+1
 
Upvote 0
Hi ellak123,

Ok, I was able to create what you described, I think. I couldn't test it on a network with multiple people, but Excel will notify you when a file is in use or opened by another.

The code below goes into Employee Sheet.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cellclr 'Interior.ColorIndex = x, more colors at https://docs.microsoft.com/en-us/office/vba/api/excel.colorindex
    Dim rowName As Range, nmatch As Boolean
    Dim dayNum As String, yearNum As String, monthWord As String
    Dim wbk As Workbook, hws As Worksheet, monthNum As Integer
    Dim lastRow As Long, i As Long, monthStart As Long, monthEnd As Long
    Dim halfday As Integer, x As Integer, ddate, hoursworked
    
    halfday = 5
    
    If Target.Column < 5 Or Target.Column > 8 Then
        GoTo exitPlanner
    End If
    
    If Target.Column = 5 Then                   'Column E or 5 = WFH
        cellclr = 33                            'Blue(Work From Home)
        If Cells(Target.Row, 5) > 0 Then
            Cells(Target.Row, 6).Clear: Cells(Target.Row, 7).Clear: Cells(Target.Row, 8).Clear
        End If
        GoTo locateName
    End If
    
    If Target.Column = 6 Then                   'Column F or 6 = WFO
        cellclr = 3                             'Red(Work From Office)
        If Cells(Target.Row, 6) > 0 Then
            Cells(Target.Row, 5).Clear: Cells(Target.Row, 7).Clear: Cells(Target.Row, 8).Clear
        End If
        GoTo locateName
    End If
    
    If Target.Column = 7 Then                   'Column G or 7 = Vacation
        cellclr = 4                             'Green(Vacation)
        If Cells(Target.Row, 7) > 0 Then
            Cells(Target.Row, 5).Clear: Cells(Target.Row, 6).Clear: Cells(Target.Row, 8).Clear
        End If
        GoTo locateName
    End If
    
    If Target.Column = 8 Then                   'Column H or 8 = Sick Day
        cellclr = 15                            'Grey(Sick Day
        If Cells(Target.Row, 8) > 0 Then
            Cells(Target.Row, 5).Clear: Cells(Target.Row, 6).Clear: Cells(Target.Row, 7).Clear
        End If
        GoTo locateName
    End If
    
    
locateName:

        nmatch = False 'Set's everyone Sheet Name to false, later checks to see if the Sheet Name exists
        
        On Error Resume Next
        'Calculate Hours Worked
        Cells(Target.Row, 4) = Format(((Cells(Target.Row, 3).Value) - (Cells(Target.Row, 2).Value)), "hh:mm")
        hoursworked = Cells(Target.Row, 4).Text 'Column D or 4 = Hours Worked
        On Error Resume Next
            
        ddate = Cells(Target.Row, 1).Value      'example: 10-Mar-22
        monthNum = Month(ddate)                 '=3
        monthWord = MonthName(monthNum)         '= March
        dayNum = Day(ddate)                     '= 10
        yearNum = year(ddate)                   '= 2022
        'Call updatePlanner '- This will open the Planner so that
                            'it's undetected. Uncomment when ready to use.
        
        Set wbk = Workbooks("HolidayPlanner.xlsx")

        On Error Resume Next
        Set hws = wbk.Worksheets(yearNum)
        On Error GoTo noSheet                   'if sheet Year doesn't exist, exit with message
        
        lastRow = hws.Range("B" & Rows.Count).End(xlUp).Row
       
        'find the Month in word start and ending row in Column AH
        For i = 1 To lastRow
            If hws.Range("AH" & i).Value = monthWord Then
                monthStart = hws.Range("AH" & i).Row
                monthEnd = monthStart + hws.Range("AH" & i).MergeArea.Rows.Count - 1
            End If
        Next i
        
        'check if employee name(Sheet Name) exist on the planner for that year and month
        For Each rowName In hws.Range("B" & monthStart & ":B" & monthEnd)
            If rowName = ActiveSheet.name Then
                nmatch = True
                GoTo updateRow
            End If
        Next rowName
        GoTo updateRow
    
    
updateRow:
    If nmatch = False Then      'If the Sheet Name is not on Planner, msgbox
        MsgBox ("The name " & ActiveSheet.name & " is not on the Planner for " _
         & monthWord & " " & hws.name & ".")
        GoTo exitPlanner
    End If
    
    Dim dayCol As Long
    dayCol = hws.Cells(monthStart + 1, 1).EntireRow.Find(What:=dayNum, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
    hws.Cells(rowName.Row, dayCol).Interior.ColorIndex = cellclr

    x = Split(hoursworked, ":")(0)
    If x < halfday Then
        hws.Cells(rowName.Row, dayCol).Value = "'1/2"
    Else
        hws.Cells(rowName.Row, dayCol).Value = "'"
    End If
    GoTo exitPlanner
        
        
noPlanner:
    MsgBox ("The planner " & wbk & " cannot be found.")
    GoTo exitPlanner


noSheet:
    MsgBox ("The sheet " & yearNum & " has not been created in the Planner or the Date is invalid.")
    GoTo exitPlanner
    
    
exitPlanner:
'wbk.Close True '- This will close the Planner so that
                'it's undetected. Uncomment when ready to use.

End Sub

The code below opens the Holiday Planner undetected for updating:

VBA Code:
Option Explicit

Public Sub updatePlanner()
Dim Path
    Path = "C:\sage\HolidayPlanner.xlsx"  'the workbook path you want to refresh

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .EnableEvents = False
        .AskToUpdateLinks = False
    End With

    Workbooks.Open Path
    ActiveWorkbook.UpdateLink name:=ActiveWorkbook.LinkSources
    'ActiveWorkbook.Close True

    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
        .EnableEvents = True
        .AskToUpdateLinks = True
    End With
End Sub

The picture shows 2 errors you may see if the Year doesn't exist or the Employed Name doesn't exist on the sheet because of spelling, etc.
 

Attachments

  • forHolidayPlanner.jpg
    forHolidayPlanner.jpg
    191.8 KB · Views: 13
  • forHolidayPlanner2.jpg
    forHolidayPlanner2.jpg
    99.8 KB · Views: 12
Upvote 0
Hi ellak123,

Ok, I was able to create what you described, I think. I couldn't test it on a network with multiple people, but Excel will notify you when a file is in use or opened by another.

The code below goes into Employee Sheet.
Hi DacEasy,

Thank you so much, I really appreciate your help and the answer is really clear. I will add it to my worksheet today
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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