The VBA code below has two parts. The first part refrences: If Not Intersect(Target,…
- User
The VBA code below has two parts. The first part refrences: If Not Intersect(Target, wsBooking.Range("A2")) Is Nothing Then, which works well The second part refrences: If Not Intersect(Target, wsBooking.Range("A4")) Is Nothing Then, which is ot working an needs to be corrected.
Can you correct the VBA code below to do the following: In my excel sheet 'Booking' I have a calender in columns C to J showing the current month and the four other months after. In column C is the month and year formatted as mmm yy. Each month, is represented by the rows cell in column C of sheet 'Booking'. In column D to J are the days of the month in column C of the same row. However, some of the rows in columns D to J that represent the day of the month are blank.
In my excel sheet 'Booking', I have a drop down list box in A4. The value in sheet 'Booking' A4 will correspond to a header value in sheet 'WF Assets' A1:AN1 When I choose a value in sheet 'Booking' A4, I want to find the column with the equal value in sheet 'WF Assets' A1:AN1 With the column found, for each date in row 2 downwards in the specific column of sheet 'WF Assets' A1:AN1 find the day value in the rows of column D to J for the month value in column C in sheet 'Booking' and give the cell a red background in sheet 'Booking'.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range) Dim wsAssets As Worksheet, wsWFAssets As Worksheet, wsBooking As Worksheet Dim selectedValue1 As String, selectedValue2 As String, monthYear As String Dim headerRange As Range Dim headerCell As Range Dim foundColumn As Range Dim cell As Range Dim dayCell As Range Dim monthCol As Range Dim targetColumn As Long Dim lastRow1 As Long, lastRow2 As Long Dim colIndex As Long Dim startRow2 As Long Dim dayValue As Variant Dim i As Long
' Set references to the worksheets Set wsWFAssets = ThisWorkbook.Sheets("WF Assets") Set wsAssets = ThisWorkbook.Sheets("Assets") Set wsBooking = ThisWorkbook.Sheets("Booking") ' Check if the changed cell is A2 If Not Intersect(Target, wsBooking.Range("A2")) Is Nothing Then selectedValue1 = wsBooking.Range("A2").Value ' Find the header row in Assets sheet (B1:H1) Set headerRange = wsAssets.Range("B1:H1") targetColumn = 0 ' Reset target column index ' Find the matching header For Each headerCell In headerRange If headerCell.Value = selectedValue1 Then targetColumn = headerCell.Column Exit For End If Next headerCell ' If a matching header was found If targetColumn > 0 Then ' Clear existing values in L2 onward wsBooking.Range("L2:L60").ClearContents ' Adjust this range as needed ' Get the last row of data in the corresponding column lastRow1 = wsAssets.Cells(wsAssets.Rows.Count, targetColumn).End(xlUp).Row ' Populate L2 downwards with values from the corresponding column For i = 2 To lastRow1 ' Assuming data starts from row 2 'wsBooking.Cells(i + 2, 1).Value = wsAssets.Cells(i, targetColumn).Value ' + 2 to account for row offset wsBooking.Cells(i, 12).Value = wsAssets.Cells(i, targetColumn).Value ' + 2 to account for row offset Next i End If End If ' Check if the changed cell is A4 If Not Intersect(Target, wsBooking.Range("A4")) Is Nothing Then ' Clear previous formatting wsBooking.Range("D2:J" & wsBooking.Cells(wsBooking.Rows.Count, "D").End(xlUp).Row).Interior.ColorIndex = xlNone ' Get the selected value from A4 selectedValue2 = wsBooking.Range("A4").Value ' Find the column in ‘WF Assets' corresponding to the selected value Set foundColumn = wsWFAssets.Rows(1).Find(What:=selectedValue2, LookIn:=xlValues, LookAt:=xlWhole) If Not foundColumn Is Nothing Then colIndex = foundColumn.Column ' Fetch the month and year from column C in ‘Booking' For Each cell In wsBooking.Range("C2:C" & wsBooking.Cells(wsBooking.Rows.Count, "C").End(xlUp).Row) monthYear = cell.Value ' This will be like "mmm yy" ' Determine the cell range for the days of the month in the corresponding month (from D to J) Set monthCol = wsBooking.Range(wsBooking.Cells(cell.Row, 4), wsBooking.Cells(cell.Row, 10)) ' D to J ' Loop through each day value in the month range D to J - Assistant
- User
The second part of the VBA is still not changing the background colours in sheet 'Booking'
- Assistant
- User
the values in column D to J of sheet 'Booking' are date vales formatted as dd
- Assistant
- User
I am now getting highlighted cells in sheet 'Booking' But it is highlighting cells in other months. As you go down column C in sheet 'Booking' the date values change to represent the month.
- Assistant
- User
error: Variable not defined - monthvalue
- Assistant
- User
Almost perfect. It missed the first date in row 2 of sheet 'WF Assets'
- Assistant
- User
The code is almost correct. What I have noticed is that it is not highlighting cells that have a date that is in column D of sheet 'Booking'
- Assistant
- User
It is still not changing the background cell of matches found in column D of sheet 'Booking'
- Assistant
- User
If the matching date found is not in column D, the background colour is cahnged to red. But if the matching date found is in column D, the background colour is not applied.
- Assistant