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.Add Type:=xlExpression, Formula1:="=$E$23 = 0"
With Selection.FormatConditions(1).Interior
.Color = rgbRed
End With
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$E$23= 15"
With Selection.FormatConditions(1).Interior
.Color = rgbGold
End With
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$E$23 = 25"
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.Add Type:=xlExpression, _
Formula1:="=AND($D1=""Highlight""," & Cells(1, j * 4 + 1).Address(False, True) & " = 0)"
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)"
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)"
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?


Visual Studio Development
Microsoft Sync Framework
Smart Device Development
Samsung PC
Open Specifications
Microsoft Atlas API: D...
Other Forums
Archived Forums C-D
Archived Forums T-U
Archived Forums Visual...
Microsoft Partner Center
Microsoft R
Forums Redesign - Q&A ...
MSDN and TechNet Profi...
C++ Standards, Extensi...
Visual Studio General ...
Visual Studio Tools fo...
AdSDK Integration & Ad...
Windows Desktop Develo...
DirectShow Development
Message Analyzer
Access for Developers
Developing Apps for Sh...
SharePoint 2010 - Gene...
SharePoint Legacy Vers...
Cloud Services (Web an...
Pricing & Billing
Azure Backup
Azure Data Factory
Azure Data Lake Analyt...
Azure Analysis Services
Windows Workflow Found...
Team Foundation Server...
Visual FoxPro General
Debugger Canvas
Microsoft Translator U...
Small Basic
.NET Framework inside ...
Microsoft Drivers for ...
SQL Server Data Wareho...
Standard 7: Tools
SQL Server 2012 Analys...
Live Connect (Archived)
MSDN and TechNet Searc...
WebsitePanel - Enterpr...
Windows Protocols
Getting Started with S...
Programming Silverligh...
PivotViewer Control wi...
Game Development with ...
Silverlight for Window...
Expression Encoder
Expression Web and Sup...
Expression Media
Retired SharePoint Col...
.NET Framework 4 Relea...
.NET Framework 4: Wind...
.NET Platform Architec...
Building Windows Store...
Deep Zoom Composer
Game Technologies: Aud...
General Troubleshootin...
HD DVD Interactivity A...
Hobbyist Zone
Installing and Registe...
JScript for the .NET F...
Microsoft adCenter: De...
Microsoft Security Dev...
Microsoft Unified Comm...
Microsoft "Data Explor...
Office Live Small Busi...
Performance Counters f...
Popfly Bugs and Sugges...
Popfly Game Creator
Search Server: Report ...
Server Core Developers
SQL Server in Azure VMs
Team Foundation Server...
Team Foundation Server...
Tailoring your Windows...
Visual Basic 2008 (Pre...
Visual Basic Language
Visual C# IDE
Visual Studio Async CTP
Visual Studio Database...
Web Sandbox - Exploits
Web Sandbox - General
Windows Live Alerts De...
Building Windows Store...
Windows IoT


Encrypt Message