I want a VBA code that will do the following. When I click on a cell in the range…
- User
I want a VBA code that will do the following. When I click on a cell in the range B2:L101, the entrie row within the range will highlight Yellow
- Assistant
Sure! You can use the following VBA code to achieve this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Target, Range("B2:L101")) Is Nothing Then Range("B" & Target.Row & ":L" & Target.Row).Interior.Color = RGB(255, 255, 0) ' Yellow Else Range("B2:L101").Interior.Color = RGB(255, 255, 255) ' Clear previous highlighting End If End SubHere's how to add this code to your worksheet:
- Press
ALT + F11to open the Visual Basic for Applications editor. - In the Project Explorer window, find your worksheet name and double-click it.
- In the code window, paste the above code.
- Close the VBA editor.
Now, whenever you click on a cell within the range B2:L101, the entire row within that range will highlight yellow.
- Press
- User
When I click on a cell the row highlights, but if I click on another cell, the previous highlight does not clear
- Assistant
Apologies for the oversight. Here's an updated version of the code that clears the previous highlighting before applying the new highlight:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Range("B2:L101").Interior.Color = RGB(255, 255, 255) ' Clear previous highlighting If Not Intersect(Target, Range("B2:L101")) Is Nothing Then Range("B" & Target.Row & ":L" & Target.Row).Interior.Color = RGB(255, 255, 0) ' Yellow End If End SubNow whenever you click on a cell within the range B2:L101, the entire row within that range will highlight yellow, and the previous highlight will be cleared.
- User
Can you please write me a line of VBA code that copies B101:L101 to B1:L1
- Assistant
Certainly! The following VBA code will copy the values from the range B101:L101 to the range B1:L1:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Range("B2:L101").Interior.Color = RGB(255, 255, 255) ' Clear previous highlighting If Not Intersect(Target, Range("B2:L101")) Is Nothing Then Range("B" & Target.Row & ":L" & Target.Row).Interior.Color = RGB(255, 255, 0) ' Yellow If Target.Row = 101 Then ' If the selected row is 101 Range("B101:L101").Copy Range("B1:L1") ' Copy B101:L101 to B1:L1 End If End If End SubNow, whenever you click on a cell within the range B2:L101, the entire row within that range will highlight yellow. Additionally, if you click on any cell within row 101, the values from B101:L101 will be copied to B1:L1.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.