Visual Basic for Applications (VBA)
Conditionally formatting a looped range of cells based on value in other cell in VBA
I am trying to conditionally format a range of cells based on the number in the column to each cell groupings' left. Basically, if in row 13, the gray column to the left of each cell grouping = 0, then I want the whole cell grouping to its right to turn green, if = 15, turn yellow, if = 25 turn red. Row 12 is what is happening with my code right now and row 13 is what I want it to look like. I can't seem to get the loop correct. Sub Highlight3() For i = 1 To ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row If Cells(i, 4) = "Highlight" Then For j = 1 To 15 Range(Cells(i, j * 4 + 2), Cells(i + 1, j * 4 + 4)).Select Selection.FormatConditions.Delete Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$E$23 = 0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Color = rgbRed End With Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$E$23= 15" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Color = rgbGold End With Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$E$23 = 25" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Color = rgbGreen End With Next j End If Next i End Sub
For each of the CF formulas, like this Formula1:="=$E$23 = 0" You need to update the cell reference dynamically: Formula1:="=" & Cells(i, j*4+1).Address & " = 0" But you will end up with a lot of CF formulas. You could do it column by column (this assumes that you have some values in row 1): Sub Highlight4() Dim j As Long For j = 1 To 15 With Intersect(Columns(j * 4 + 2), ActiveSheet.UsedRange).Resize(, 3).Cells .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, _ Formula1:="=AND($D1=""Highlight""," & Cells(1, j * 4 + 1).Address(False, True) & " = 0)" .FormatConditions(.FormatConditions.Count).SetFirstPriority With .FormatConditions(1).Interior .Color = rgbGreen End With .FormatConditions.Add Type:=xlExpression, _ Formula1:="=AND($D1=""Highlight""," & Cells(1, j * 4 + 1).Address(False, True) & " = 15)" .FormatConditions(.FormatConditions.Count).SetFirstPriority With .FormatConditions(1).Interior .Color = rgbGold End With .FormatConditions.Add Type:=xlExpression, _ Formula1:="=AND($D1=""Highlight""," & Cells(1, j * 4 + 1).Address(False, True) & " = 25)" .FormatConditions(.FormatConditions.Count).SetFirstPriority With .FormatConditions(1).Interior .Color = rgbRed End With End With Next j End Sub
Related Links
How to fill in blank cells with correct data without overwriting the data above it?
rch for date and return other fields
How to paste clipboard content to a folder (as a file)
Copy Outlook Message body content into MS Excel
extract a CAB file with VBA
OMG my project has crashed
Assigning Range of Cells to Array
Create Code to Update Value in Record From Previous Data Record
MSComm -Having problem receiving data from modem
Exporting Microsoft Word Review Comments to Microsoft Excel - Error 91 ??
error 1004 application defined or object defined error
Change File Names Using SQL Query to get new UID
Listbox question (prevent refresh)
Listview.listitem.key Intermitent Failure
varEntryIDs = Split(EntryIDCollection, ",")
Is it possible to use code in VBA to manipulate files in Windows Explorer?