We help IT Professionals succeed at work.

Macro for conditional formating in Excel

Hello experts,

I have in a excel file:

-      A target value in C1
-      A table A4:D406
-      values for each row in column D. These values are sorted from lowest to greatest (so D4<D5<D6…)

I would like a macro that formats all the data in the range ‘An:Dn’ in ‘bold red’, with n being the row number where Dn<=C1

Thanks
Comment
Watch Question

Roy CoxGroup Finance Manager

Commented:
Why use VBA? Conditional Formatting should do that without VBA

Author

Commented:

It will help me learn VBScript and I can copy and paste in other files

Commented:
I believe you can do this with Conditional Formatting.  Selecting Column D and using conditional formatting to look for cells <= C1.  The only disadvantage is the highlighting is only column D.  So if it is important to highlight A:D maybe this won't work.

Commented:
Sorry my comment didn't see your responses.
Roy CoxGroup Finance Manager

Commented:
VBScript is not the same as VBA. I see no point in VBA for this, you can copy and paste formats to another workbook.

By all means learn VBA, but don't use it pointlessly.

Author

Commented:

If you don't want to respect my request, your choice... Always amazed that people spend more time challenging customers requests than addressing them. I know exactly what I want and I don't need to further justify. And yes, maybe VBA is the correct term

Commented:
Sure.  Think most of the experts comments are to help clarify the request.  Sometimes a question is not completely clear, or perhaps the author is not aware of other alternatives.  In your case you clear about what you want.
"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
You can use this macro. Change the Const value in line 3 to match your table's name

Sub CheckAgainstC1()

Const TABLE_NAME = "Table1"
Dim lngLastRow As Long
Dim lngRow As Long
Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects(TABLE_NAME)
lngLastRow = tbl.Range.Rows.Count + 1

For lngRow = 5 To lngLastRow
    If Cells(lngRow, "D") <= Range("C1") Then
        Cells(lngRow, "D").Font.Bold = True
        Cells(lngRow, "D").Font.Color = vbRed
    End If
Next

End Sub

Open in new window

Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I’m glad I was able to help.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2019
              Experts Exchange Top Expert VBA 2018, 2019
              Experts Exchange Distinguished Expert in Excel 2018