JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
I am writing a macro to do some highlighting. The main macro calls several Subs to do some of the work. It needs to pass a list of cell addresses to these subs. I am wondering about the best way to do that.
My current solution is to pass them as separate arguments:
As I work on the sheet, the cells move around, new cells get added, and old ones get deleted. All of the cells are named, so the macro can access them using the names. That solves the problem of the cells moving around. But with the current implementation, if I add a cell, I have to edit both the call and the sub in addition to defining a new address variable.
I'd like to be able to pass a list of cell addresses ("D3, G3, M3, D10") so I only have to edit one variable and not have to change any other code. And I'd like to replace the repeated code in the Fillem sub with a loop.
Should I pass a comma-delimited string and use the Split function to break it up? Is there a better way?
Thanks
My current solution is to pass them as separate arguments:
Code:
Sub Highlight()
. . .
' Define the target addresses
Dim strTSTop As String
strTSTop = Range("TSTop").Address
Dim strFLTop As String
strFLTop = Range("FLTop").Address
Dim strAve As String
strAve = Range("Ave").Address
. . .
Call Fillem(StrTSTop, StrFLTop, strAve, FillColor)
. . .
End Sub
Code:
Sub Fillem(Addr1 as string, Addr2 as string, Addr3 as string, FillColor as Long)
. . .
Range(Addr1).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = FillColor
.color = colorFill
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range(Addr2).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = FillColor
.color = colorFill
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range(Addr2).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = FillColor
.color = colorFill
.TintAndShade = 0
.PatternTintAndShade = 0
End With
As I work on the sheet, the cells move around, new cells get added, and old ones get deleted. All of the cells are named, so the macro can access them using the names. That solves the problem of the cells moving around. But with the current implementation, if I add a cell, I have to edit both the call and the sub in addition to defining a new address variable.
I'd like to be able to pass a list of cell addresses ("D3, G3, M3, D10") so I only have to edit one variable and not have to change any other code. And I'd like to replace the repeated code in the Fillem sub with a loop.
Should I pass a comma-delimited string and use the Split function to break it up? Is there a better way?
Thanks