Forum Discussion
Vimal_Gaur
Jan 23, 2023Brass Contributor
VBA Background color
VBA - Need shortcut key to change background color of ENTIRE row of active cell
It should also change background color of ENTIRE rows of active / selected cells
Sub colorseveralwows() Dim i As Long Dim bereich As Range Set bereich = Selection For i = bereich.Row To bereich.Row + bereich.Rows.Count - 1 Rows(i).Interior.Color = vbBlue Next i End Sub
You can run this code with the shortcut ctrl+B in the attached file. The macros colors rows 7, 8 and 9 blue if you select e.g. cells F7, F8 and F9.
- OliverScheurichGold Contributor
Sub colorrows() Dim i As Long i = ActiveCell.Row Rows(i).Interior.ColorIndex = 5 End Sub
An alternative could be these lines of code. In the attached file you can select a cell and then press ctrl+k in order to color the whole selected row blue.
- Vimal_GaurBrass Contributor
Thanks for your reply. there is much simpler way
Sub rowcolor()
Rows(ActiveCell.Row).Interior.Color = vbGreen
End Sub
above works same as your code but has a issue, plz see sheet attached.
Your sheet changes the background color of entire row but on first row. Suppose if I select A1,A2 & A3, then press K as per your program, it should color entire rows 1,2 & 3. Suppose if I select D5,D6 & D7, then press K as per your program, it should color entire rows 5,6 & 7.
- OliverScheurichGold Contributor
Sub colorseveralwows() Dim i As Long Dim bereich As Range Set bereich = Selection For i = bereich.Row To bereich.Row + bereich.Rows.Count - 1 Rows(i).Interior.Color = vbBlue Next i End Sub
You can run this code with the shortcut ctrl+B in the attached file. The macros colors rows 7, 8 and 9 blue if you select e.g. cells F7, F8 and F9.
- NikolinoDEGold Contributor
I can't offer you a solution with a key combination...I don't know how :).
But with a button you can insert it very easily and quickly.
Attached is the VBA solution with a button. Example file is included.
Sub N_colour() [a1].Interior.ColorIndex = 5 End Sub
Hope I was able to help you with this info.
I know I don't know anything (Socrates)
- Vimal_GaurBrass ContributorThanks for your reply.
My Dear I said ENTIRE ROW & not a SINGLE CELL