samjsteffes
New Member
- Joined
- Feb 27, 2018
- Messages
- 16
Hi all,
I have a question about adding conditional formatting with VBA, and I am hoping someone can nudge me in the right direction....
Context/scenario:
I have a multidimensional table of different fields, which is used to track where physical attributes change at certain footage. As an example:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Footage[/TD]
[TD]Structure[/TD]
[TD]Material[/TD]
[TD]Clip type[/TD]
[TD]Access[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]at grade[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]Normal[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]at grade[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]250[/TD]
[TD]elevated[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]300[/TD]
[TD]elevated[/TD]
[TD]concrete[/TD]
[TD]plastic[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]375[/TD]
[TD]elevated[/TD]
[TD]concrete[/TD]
[TD]plastic[/TD]
[TD]Restricted[/TD]
[/TR]
[TR]
[TD]525[/TD]
[TD]elevated[/TD]
[TD]steel[/TD]
[TD]plastic[/TD]
[TD]Restricted[/TD]
[/TR]
[TR]
[TD]615[/TD]
[TD]at grade[/TD]
[TD]steel[/TD]
[TD]composite[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]800[/TD]
[TD]at grade[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]1000[/TD]
[TD]at grade[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]normal[/TD]
[/TR]
</tbody>[/TABLE]
I've changed the actual content from what I am working with, but the key detail is that each row entry is created because an attribute in at least one of the fields changed at that footage; I've highlighted these occurrences in the table above.
What I would like to do, is create a macro in order to toggle on/off conditional formatting to highlighted these changes, preferably by highlighting the cells, rather than changing the text color.
Some other notes: I'm using a structured table reference, and I only want to apply this formatting to a specified group of the table columns
Below is the code I've tried:
The bold line in the code is the one I can't figure out. In a 'with' procedure, how do I get it to look at the address/value of the cell above to compare? I'm trying to avoid to a "for each" type loop through all the cells, since the table could become pretty large. This code doesn't result in any errors, but it also doesn't result in any new conditional formatting rules being applied, so I assume something is off.
I'd also be curious to know if there is a way to do this without having "live" conditional formatting as the end result. Meaning, can I just review the table columns and highlight based on this condition/criteria, without having to apply conditional formatting?
Thanks in advance for any advice!
- sjs
I have a question about adding conditional formatting with VBA, and I am hoping someone can nudge me in the right direction....
Context/scenario:
I have a multidimensional table of different fields, which is used to track where physical attributes change at certain footage. As an example:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Footage[/TD]
[TD]Structure[/TD]
[TD]Material[/TD]
[TD]Clip type[/TD]
[TD]Access[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]at grade[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]Normal[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]at grade[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]250[/TD]
[TD]elevated[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]300[/TD]
[TD]elevated[/TD]
[TD]concrete[/TD]
[TD]plastic[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]375[/TD]
[TD]elevated[/TD]
[TD]concrete[/TD]
[TD]plastic[/TD]
[TD]Restricted[/TD]
[/TR]
[TR]
[TD]525[/TD]
[TD]elevated[/TD]
[TD]steel[/TD]
[TD]plastic[/TD]
[TD]Restricted[/TD]
[/TR]
[TR]
[TD]615[/TD]
[TD]at grade[/TD]
[TD]steel[/TD]
[TD]composite[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]800[/TD]
[TD]at grade[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]Narrow[/TD]
[/TR]
[TR]
[TD]1000[/TD]
[TD]at grade[/TD]
[TD]wood[/TD]
[TD]plastic[/TD]
[TD]normal[/TD]
[/TR]
</tbody>[/TABLE]
I've changed the actual content from what I am working with, but the key detail is that each row entry is created because an attribute in at least one of the fields changed at that footage; I've highlighted these occurrences in the table above.
What I would like to do, is create a macro in order to toggle on/off conditional formatting to highlighted these changes, preferably by highlighting the cells, rather than changing the text color.
Some other notes: I'm using a structured table reference, and I only want to apply this formatting to a specified group of the table columns
Below is the code I've tried:
Rich (BB code):
sub hilite_cells()
dim tbl as ListObject
dim rng as Range
dim a as Integer
dim b as Integer
dim i as Integer
Application.ScreenUpdating = False
On Error Resume Next
Set tbl = Worksheets("sheet_name").ListObjects("tbl_name")
Set rng = tbl.HeaderRowRange
If tbl Is Nothing Or rng Is Nothing Then
MsgBox "Nothing found."
Exit Sub
End If
tbl.DataBodyRange.FormatConditions.Delete
For i = a To b
With tbl.ListColumns(i).DataBodyRange.Offset(1, 0).Resize(tbl.DataBodyRange.Rows.count - 1)
.FormatConditions.Add Type:=xlExpression, Formula1:="<>" & .Offset(1, 0).Value
.FormatConditions(1).Interior.Color = vbYellow
End With
Next i
Application.ScreenUpdating = True
End Sub
The bold line in the code is the one I can't figure out. In a 'with' procedure, how do I get it to look at the address/value of the cell above to compare? I'm trying to avoid to a "for each" type loop through all the cells, since the table could become pretty large. This code doesn't result in any errors, but it also doesn't result in any new conditional formatting rules being applied, so I assume something is off.
I'd also be curious to know if there is a way to do this without having "live" conditional formatting as the end result. Meaning, can I just review the table columns and highlight based on this condition/criteria, without having to apply conditional formatting?
Thanks in advance for any advice!
- sjs