Forum Discussion

Vimal_Gaur's avatar
Vimal_Gaur
Brass Contributor
Jan 23, 2023
Solved

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

  • Vimal_Gaur 

    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.

  • Vimal_Gaur 

    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_Gaur's avatar
      Vimal_Gaur
      Brass 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.

      • OliverScheurich's avatar
        OliverScheurich
        Gold Contributor

        Vimal_Gaur 

        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.

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    Vimal_Gaur 

    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.

     

    NikolinoDE

    I know I don't know anything (Socrates)

    • Vimal_Gaur's avatar
      Vimal_Gaur
      Brass Contributor
      Thanks for your reply.
      My Dear I said ENTIRE ROW & not a SINGLE CELL

Resources