Can I have a VBA code that will do the following: Change the background colour of all…
- User
Can I have a VBA code that will do the following:
Change the background colour of all cells in the column of range B14:CP21, to RGB 166.166.166 if the date value in range B14:CP14 is within the current month.
- Assistant
Certainly! You can use the following VBA code to change the background color of cells in the range
B14:CP21to the RGB value(166, 166, 166)if the date value in the corresponding column of rangeB14:CP14is within the current month.Here's the VBA code you can use:
Sub ChangeCellColorBasedOnDate() Dim ws As Worksheet Dim cell As Range Dim currentMonth As Long Dim currentYear As Long Dim checkDate As Date ' Set the worksheet Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name ' Get current month and year currentMonth = Month(Date) currentYear = Year(Date) ' Loop through the range B14:CP14 to check dates For Each cell In ws.Range("B14:CP14") If IsDate(cell.Value) Then checkDate = cell.Value ' Check if the date is within the current month and year If Month(checkDate) = currentMonth And Year(checkDate) = currentYear Then ' Change the background color of the corresponding cells in B15:CP21 ws.Range(ws.Cells(15, cell.Column), ws.Cells(21, cell.Column)).Interior.Color = RGB(166, 166, 166) End If End If Next cell End Sub - User
- Assistant
Here’s the
DeleteArchiveDatesubroutine that you can include in your VBA code. This subroutine will perform the operations you've specified: copying values from the defined cells to specific locations, clearing comments and values in a range, and displaying a message box. - User
- Assistant
- User
In the code below, I no loger need this function: ' Add 'x' to column V for selected rows For Each cell In userRange.Rows cell.Offset(0, 21).Resize(1, 1).Value = "x"
How can I remove it without affecting other loop in the code.
' Find last row in column B (excluding headers) lastRow = Cells(Rows.Count, "B").End(xlUp).row ' Find next empty row after row 24 Set pasteCell = Cells(lastRow + 1, 1) Do While Not IsEmpty(pasteCell.Offset(0, 20)) ' Check if next row is empty up to column U Set pasteCell = pasteCell.Offset(1, 0) ' Move down until empty row found Loop ' Copy values only from user selection to the found empty row pasteCell.Resize(userRange.Rows.Count, userRange.Columns.Count).Value = userRange.Value
Set headerRange = Me.Range("A1:U1") ' Find last row in column B (excluding headers) lastRow = Cells(Rows.Count, "B").End(xlUp).row ' Find next empty row after row 24 Set pasteCell = Cells(lastRow + 1, 1) Do While Not IsEmpty(pasteCell.Offset(0, 20)) ' Check if next row is empty up to column U Set pasteCell = pasteCell.Offset(1, 0) ' Move down until empty row found Loop ' Copy values only from headerRange to the found empty row pasteCell.Resize(headerRange.Rows.Count, headerRange.Columns.Count).Value = headerRange.Value ' Add 'x' to column V for selected rows For Each cell In userRange.Rows cell.Offset(0, 21).Resize(1, 1).Value = "x" Next cell - Assistant
To remove the functionality that adds an "x" to column V for the selected rows without affecting the other loops or parts of your code, you can simply delete the loop that writes the "x" to those cells.
Here’s your code snippet with the section that adds "x" in column V removed. I've highlighted the changes.
' Find last row in column B (excluding headers) lastRow = Cells(Rows.Count, "B").End(xlUp).row ' Find next empty row after row 24 Set pasteCell = Cells(lastRow + 1, 1) Do While Not IsEmpty(pasteCell.Offset(0, 20)) ' Check if next row is empty up to column U Set pasteCell = pasteCell.Offset(1, 0) ' Move down until empty row found Loop ' Copy values only from user selection to the found empty row pasteCell.Resize(userRange.Rows.Count, userRange.Columns.Count).Value = userRange.Value Set headerRange = Me.Range("A1:U1") ' Find last row in column B (excluding headers) lastRow = Cells(Rows.Count, "B").End(xlUp).row ' Find next empty row after row 24 Set pasteCell = Cells(lastRow + 1, 1) Do While Not IsEmpty(pasteCell.Offset(0, 20)) ' Check if next row is empty up to column U Set pasteCell = pasteCell.Offset(1, 0) ' Move down until empty row found Loop ' Copy values only from headerRange to the found empty row pasteCell.Resize(headerRange.Rows.Count, headerRange.Columns.Count).Value = headerRange.Value ' The following section has been removed ' Add 'x' to column V for selected rows ' For Each cell In userRange.Rows ' cell.Offset(0, 21).Resize(1, 1).Value = "x" ' Next cell