SOLVED

Question related highlight cell with the help of VBA code

Iron Contributor

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..

6 Replies

@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
Why would you want to use VBA for this? A simple conditional format would suffice.
Sir, if we highlight only SPACE column, then what should i write in VBA?
Hello Sir,
Actually i am learning VBA program.
best response confirmed by Excel (Iron Contributor)
Solution

@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.

 

@Hans Vogelaar 

Got it sir. 

Thank you very much sir. 

1 best response

Accepted Solutions
best response confirmed by Excel (Iron Contributor)
Solution

@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.

 

View solution in original post