Forum Discussion

Excel's avatar
Excel
Iron Contributor
Aug 26, 2021
Solved

Question related highlight cell with the help of VBA code

Hello Everyone,

I want to highlight cells(Red colour) which is more than 4 in SPACE COLUMN with the help of VBA code.

So, what should i write in VBA code?

 

Please help..??

 

Here is a attached..

  • Excel 

    Change the line

                Range("A" & r).Resize(1, 4).Interior.Color = vbRed

    to

                Range("D" & r).Interior.Color = vbRed

    Remark: if you want to learn to write VBA, you should really try to make such changes yourself. Even if you make mistakes, you will learn much more from experimenting than from asking us for every small detail.

     

6 Replies

  • JKPieterse's avatar
    JKPieterse
    Silver Contributor
    Why would you want to use VBA for this? A simple conditional format would suffice.
    • Excel's avatar
      Excel
      Iron Contributor
      Hello Sir,
      Actually i am learning VBA program.
  • Excel 

    You can easily use conditional formatting for this. You wouldn't need VBA, and the formatting would be updated automatically when the values in column D change.

    But if you really want to use VBA:

    Sub ColorCells()
        Dim r As Long
        Dim m As Long
        Application.ScreenUpdating = False
        m = Range("D" & Rows.Count).End(xlUp).Row
        Range("A2:D" & m).Interior.ColorIndex = xlColorIndexNone
        For r = 2 To m
            If Range("D" & r).Value > 4 Then
                Range("A" & r).Resize(1, 4).Interior.Color = vbRed
            End If
        Next r
        Application.ScreenUpdating = True
    End Sub
    • Excel's avatar
      Excel
      Iron Contributor
      Sir, if we highlight only SPACE column, then what should i write in VBA?
      • Excel 

        Change the line

                    Range("A" & r).Resize(1, 4).Interior.Color = vbRed

        to

                    Range("D" & r).Interior.Color = vbRed

        Remark: if you want to learn to write VBA, you should really try to make such changes yourself. Even if you make mistakes, you will learn much more from experimenting than from asking us for every small detail.

         

Resources