Certainly! To change the color of the cell to green instead of red, you can update the line in the VBA code that sets the cell's interior color. The RGB values for green are (0, 255, 0).
Here's the updated section of the code you need to modify:
currentCell.Value = dateCounter
currentCell.Interior.Color = RGB(0, 255, 0) ' Green Color
Now, incorporating this into the full subroutine, the modified code looks like this:
Sub InsertDatesIntoMatchingColumn()
Dim bookingSheet As Worksheet
Dim targetSheet As Worksheet
Dim startDate As Date
Dim endDate As Date
Dim targetColumn As Range
Dim currentCell As Range
Dim dateCounter As Date
Dim searchValue As String
Dim columnHeader As String
Dim foundColumn As Range
' Set references to the Booking sheet
Set bookingSheet = ThisWorkbook.Worksheets("Booking")
' Read start date and end date from Booking sheet
startDate = bookingSheet.Range("A8").Value
endDate = bookingSheet.Range("A10").Value
' Read the target sheet name from cell A2
searchValue = bookingSheet.Range("A2").Value
' Attempt to find the target sheet
On Error Resume Next
Set targetSheet = ThisWorkbook.Worksheets(searchValue)
On Error GoTo 0
If targetSheet Is Nothing Then
MsgBox "Target sheet '" & searchValue & "' was not found.", vbExclamation
Exit Sub
End If
' Get the header value from A4
columnHeader = bookingSheet.Range("A4").Value
' Search for the header in row 1 of the target sheet
Set foundColumn = targetSheet.Rows(1).Find(What:=columnHeader, LookIn:=xlValues, LookAt:=xlWhole)
If foundColumn Is Nothing Then
MsgBox "Column header '" & columnHeader & "' was not found in sheet '" & searchValue & "'.", vbExclamation
Exit Sub
End If
' Get the next empty cell in the found column (which is foundColumn.Column)
Set currentCell = targetSheet.Cells(targetSheet.Rows.Count, foundColumn.Column).End(xlUp).Offset(1, 0)
' Loop through each date from start date to end date
For dateCounter = startDate To endDate
currentCell.Value = dateCounter
currentCell.Interior.Color = RGB(0, 255, 0) ' Green Color
Set currentCell = currentCell.Offset(1, 0) ' Move to the next cell down
Next dateCounter
MsgBox "Dates inserted successfully!", vbInformation
End Sub
Summary of Changes:
- Replaced the interior color line to
currentCell.Interior.Color = RGB(0, 255, 0) to set the cell background to green.
You can follow the same steps as before to run this updated code. It will now fill the cells with green color for each date inserted.