robhillier
New Member
- Joined
- May 5, 2010
- Messages
- 23
Greetings!
I'm trying to find a way to programmatically traverse the object model at run-time, and I'm curious if anyone knows how to accomplish this.
Perhaps some context would help. I have a 2007 workbook (a series of workbooks, actually) that have 'Styles' applied to cells. I need to find a way to get references to all cells that have had the 'Input' Style applied to them. (I actually have to loop through a series of custom Styles, but I'll use the 'Input' Style throughout this post so I'm speaking on common ground)
In Word, this is easy. The Find object has a Style property that can be set to a specific style name. One can then use Find.Execute to get instances of text that have that Style applied. Excel's Find object has no such property.
One solution would be to loop through each cell and check its Style property with something like:
but some worksheets are quite large (over 100,000 cells in one case, with a range of A1:EK788), so looping through each cell would be time-prohibitive for ONE Style, let alone many Styles, which I have to do (8 Styles, at the moment).
Excel's Application.Find is quite fast, and I would prefer to use this. To accomplish this, I would have to read the properties of the Style object, see if they have been set, and if so apply them to Application.FindFormat, and then execute the Find. The problem here is that I would have to hard code these checks and applications, like:
and then do the same for the Alignment, Font, Number, Patterns, and Protection properties, and each property of their sub-objects (Style.Interior.ColorIndex, for example). Labourious and not at all future-proof, as you can see.
This is why I would like to programmatically traverse the object model at run-time. It would allow me to recursively loop through the Style object's properties and sub-properties, and apply them to the FindFormat object's properties and sub-properties.
Does anyone know of a way? (And thanks for reading this far, by the way!!!)
Rob
I'm trying to find a way to programmatically traverse the object model at run-time, and I'm curious if anyone knows how to accomplish this.
Perhaps some context would help. I have a 2007 workbook (a series of workbooks, actually) that have 'Styles' applied to cells. I need to find a way to get references to all cells that have had the 'Input' Style applied to them. (I actually have to loop through a series of custom Styles, but I'll use the 'Input' Style throughout this post so I'm speaking on common ground)
In Word, this is easy. The Find object has a Style property that can be set to a specific style name. One can then use Find.Execute to get instances of text that have that Style applied. Excel's Find object has no such property.
One solution would be to loop through each cell and check its Style property with something like:
Code:
For Each rngLoop In ActiveSheet.UsedRange
If rngLoop.Style = "Input" Then
'... My logic ...
End If
Next rngLoop
but some worksheets are quite large (over 100,000 cells in one case, with a range of A1:EK788), so looping through each cell would be time-prohibitive for ONE Style, let alone many Styles, which I have to do (8 Styles, at the moment).
Excel's Application.Find is quite fast, and I would prefer to use this. To accomplish this, I would have to read the properties of the Style object, see if they have been set, and if so apply them to Application.FindFormat, and then execute the Find. The problem here is that I would have to hard code these checks and applications, like:
Code:
Application.FindFormat.Clear
With ThisWorkbook.Styles("Input")
' Does the Style include Borders properties?
If .IncludeBorder Then
' Border colours
Application.FindFormat.Borders(xlDiagonalDown).Color = .Borders(xlDiagonalDown).Color
Application.FindFormat.Borders(xlDiagonalUp).Color = .Borders(xlDiagonalUp).Color
Application.FindFormat.Borders(xlEdgeBottom).Color = .Borders(xlEdgeBottom).Color
'...
' Border line Styles
Application.FindFormat.Borders(xlDiagonalDown).LineStyle = .Borders(xlDiagonalDown).LineStyle
Application.FindFormat.Borders(xlDiagonalUp).LineStyle = .Borders(xlDiagonalUp).LineStyle
Application.FindFormat.Borders(xlEdgeBottom).LineStyle = .Borders(xlEdgeBottom).LineStyle
'...
' Border line weights
Application.FindFormat.Borders(xlDiagonalDown).Weight = .Borders(xlDiagonalDown).Weight
Application.FindFormat.Borders(xlDiagonalUp).Weight = .Borders(xlDiagonalUp).Weight
Application.FindFormat.Borders(xlEdgeBottom).Weight = .Borders(xlEdgeBottom).Weight
'...
' And all other border properties...
End If
End With
This is why I would like to programmatically traverse the object model at run-time. It would allow me to recursively loop through the Style object's properties and sub-properties, and apply them to the FindFormat object's properties and sub-properties.
Does anyone know of a way? (And thanks for reading this far, by the way!!!)
Rob
Last edited: