macro for formatting columns if values are greater than or equal to reference cells

traxdmb34

New Member
Joined
May 2, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi all, I'm still in the beginning stages of learning all the fun stuff that can be done with macros. I'm working on some formatting stuff to make my life easier and the last bit I need to figure out is how to bold and/or highlight cells in a column if they are great than or equal to values in two different rows in the same column. My reference values will always be in rows 4 and 5 and start in column J, but the number of columns will vary based on how many results I need to include on a given table. The screenshot shows an example of formatting I did manually in column J, just to show that values over 1 need to be bold and values over 23 need the cell shaded (color doesn't matter). Nothing needs to be done if the cell in rows 4 or 5 is blank. Formatting will need to be repeated every other column to the end of the table.

Can someone point me in the right direction, please? Thanks in advance!

1691611713931.png
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
If your values that need to be formatted are always in Column J, then I think simple Conditional Formatting would be easier than a Macro...?
 
Upvote 0
If your values that need to be formatted are always in Column J, then I think simple Conditional Formatting would be easier than a Macro...?
Perhaps you missed these couple of things?
Formatting will need to be repeated every other column to the end of the table
the number of columns will vary

@traxdmb34
This does use conditional formatting but allows for the variations you described. It assumes no conditional formatting already in the relevant columns.

VBA Code:
Sub Test()
  Dim myCols As Range
  Dim lastcol As Long, lastrow As Long, c As Long
  
  lastcol = Cells(4, Columns.Count).End(xlToLeft).Column
  lastrow = Columns("J").Resize(, lastcol - 9).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  Set myCols = Range("J6:J" & lastrow)
  For c = 12 To lastcol Step 2
    Set myCols = Union(myCols, Intersect(myCols.EntireRow, Columns(c)))
  Next c
  With myCols
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(J$4<>"""",J6>J$4)"
    .FormatConditions(1).Font.Bold = True
    .FormatConditions(1).StopIfTrue = False
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(J$5<>"""",J6>J$5)"
    .FormatConditions(2).SetFirstPriority
    .FormatConditions(1).Interior.Color = vbGreen
    .FormatConditions(1).StopIfTrue = False
  End With
End Sub

My sample data and results:

traxdmb34.xlsm
JKLMNOPQRSTUVWXY
41600700100070100500
52333300007007800690
62383066004290
757.1214
812.28006000
9172
101772
117.993
Sheet3
 
Upvote 1
Perhaps you missed these couple of things?



@traxdmb34
This does use conditional formatting but allows for the variations you described. It assumes no conditional formatting already in the relevant columns.

VBA Code:
Sub Test()
  Dim myCols As Range
  Dim lastcol As Long, lastrow As Long, c As Long
 
  lastcol = Cells(4, Columns.Count).End(xlToLeft).Column
  lastrow = Columns("J").Resize(, lastcol - 9).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  Set myCols = Range("J6:J" & lastrow)
  For c = 12 To lastcol Step 2
    Set myCols = Union(myCols, Intersect(myCols.EntireRow, Columns(c)))
  Next c
  With myCols
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(J$4<>"""",J6>J$4)"
    .FormatConditions(1).Font.Bold = True
    .FormatConditions(1).StopIfTrue = False
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(J$5<>"""",J6>J$5)"
    .FormatConditions(2).SetFirstPriority
    .FormatConditions(1).Interior.Color = vbGreen
    .FormatConditions(1).StopIfTrue = False
  End With
End Sub

My sample data and results:

traxdmb34.xlsm
JKLMNOPQRSTUVWXY
41600700100070100500
52333300007007800690
62383066004290
757.1214
812.28006000
9172
101772
117.993
Sheet3
Thank you so much! I tested this on my table and it's a great help! I have cells in the table that are not numeric values (ND (non-detect) and -- if no result to report); it's bolding/highlighting those, so I'll have to adjust it a bit, but I really appreciate your help!! Thanks again!
 
Upvote 0
I have cells in the table that are not numeric values
Yes, I had seen that but then forgot to account for it, sorry! :oops:
For the future, if you can provide your sample data with XL2BB then it is less likely things like that will happen since we can test with your actual sample data rather than some briefly-typed (& bad) samples of our own. :)

Rich (BB code):
Sub Test_v2()
  Dim myCols As Range
  Dim lastcol As Long, lastrow As Long, c As Long
 
  lastcol = Cells(4, Columns.Count).End(xlToLeft).Column
  lastrow = Columns("J").Resize(, lastcol - 9).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  Set myCols = Range("J6:J" & lastrow)
  For c = 12 To lastcol Step 2
    Set myCols = Union(myCols, Intersect(myCols.EntireRow, Columns(c)))
  Next c
  With myCols
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(J$4<>"""",J6>J$4,ISNUMBER(J6))"
    .FormatConditions(1).Font.Bold = True
    .FormatConditions(1).StopIfTrue = False
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(J$5<>"""",J6>J$5,ISNUMBER(J6))"
    .FormatConditions(2).SetFirstPriority
    .FormatConditions(1).Interior.Color = vbGreen
    .FormatConditions(1).StopIfTrue = False
  End With
End Sub
 
Upvote 0
Solution
Yes, I had seen that but then forgot to account for it, sorry! :oops:
For the future, if you can provide your sample data with XL2BB then it is less likely things like that will happen since we can test with your actual sample data rather than some briefly-typed (& bad) samples of our own. :)

Rich (BB code):
Sub Test_v2()
  Dim myCols As Range
  Dim lastcol As Long, lastrow As Long, c As Long
 
  lastcol = Cells(4, Columns.Count).End(xlToLeft).Column
  lastrow = Columns("J").Resize(, lastcol - 9).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  Set myCols = Range("J6:J" & lastrow)
  For c = 12 To lastcol Step 2
    Set myCols = Union(myCols, Intersect(myCols.EntireRow, Columns(c)))
  Next c
  With myCols
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(J$4<>"""",J6>J$4,ISNUMBER(J6))"
    .FormatConditions(1).Font.Bold = True
    .FormatConditions(1).StopIfTrue = False
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(J$5<>"""",J6>J$5,ISNUMBER(J6))"
    .FormatConditions(2).SetFirstPriority
    .FormatConditions(1).Interior.Color = vbGreen
    .FormatConditions(1).StopIfTrue = False
  End With
End Sub

Amazing! Having zero background with macros until very recently, this all seems like some sort of crazy magic to me. The technical understanding is coming...slowly.

Thanks again for your help!
 
Upvote 0
You're welcome. Thanks for the confirmation. (y)

Having zero background with macros until very recently, this all seems like some sort of crazy magic to me. The technical understanding is coming...slowly.
Stick with it & it will come. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top