Is it possible to write an excel VBA code that can do the following: In column B…
- User
Is it possible to write an excel VBA code that can do the following: In column B starting from B3, go down the column and find all cells that are highlighted Yellow. For each cell that is highligted Yellow; If the cell value is Monday, then in the previous cell insert the value Sunday and Highlight the cell with the new value Red. If the cell value is Tuesday, then in the previous cell insert the value Monday and Highlight the cell with the new value Red. If the cell value is Wednesday, then in the previous cell insert the value Tuesday and Highlight the cell with the new value Red. If the cell value is Thursday, then in the previous cell insert the value Wednesday and Highlight the cell with the new value Red. If the cell value is Friday, then in the previous cell insert the value Thursday and Highlight the cell with the new value Red. If the cell value is Saturday, then in the previous cell insert the value Friday and Highlight the cell with the new value Red. If the cell value is Sunday, then in the previous cell insert the value Saturday and Highlight the cell with the new value Red.
- Assistant
Yes, it is possible to write an Excel VBA code that can perform the mentioned tasks. Here is an example code that can achieve this:
Sub UpdatePreviousCell() Dim lastRow As Long Dim i As Long ' Determine the last used row in Column B lastRow = Cells(Rows.Count, 2).End(xlUp).Row ' Loop through each cell in Column B starting from B3 For i = 3 To lastRow ' Check if the cell is highlighted yellow If Range("B" & i).Interior.Color = RGB(255, 255, 0) Then ' Check the cell value and update the previous cell accordingly Select Case Range("B" & i).Value Case "Monday" Range("B" & i - 1).Value = "Sunday" Case "Tuesday" Range("B" & i - 1).Value = "Monday" Case "Wednesday" Range("B" & i - 1).Value = "Tuesday" Case "Thursday" Range("B" & i - 1).Value = "Wednesday" Case "Friday" Range("B" & i - 1).Value = "Thursday" Case "Saturday" Range("B" & i - 1).Value = "Friday" Case "Sunday" Range("B" & i - 1).Value = "Saturday" End Select ' Highlight the updated cell in red Range("B" & i - 1).Interior.Color = RGB(255, 0, 0) End If Next i End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.