Quote:
Originally Posted by macropod
As coded, the macro requires the heading row parameter to be present, to make it optional, it needs to be the last parameter:
Parms: Col=3, RGB=215,199,244, Hdrs=0
|
I'm not sure which code you are referencing. In the code I am working on, the keyword parameters can be written in
any order. That's the whole purpose of keyword parameters, no?. If they must be in a specific order, there is little need for the keywords. They can just be coded as values:
Parms: 3, 215, 199, 244, 0
And in my code,
all of them have defaults, so
none of them are required. I loop through them one by one using a Case statement to determine which keyword it is and then process the value. I have actually come up with a couple more parameters as well, all of which can be coded in any order.
Code:
ParmStr = Trim(Selection.Tables(1).Range.Previous.Paragraphs.last.Range.Text)
ParmStr = Left(ParmStr, Len(ParmStr) - 1)
Tokens = Split(ParmStr, " ")
If UBound(Tokens) < 1 Then GoTo NoInDocParms
If UCase(Tokens(0)) <> ParmKW Then GoTo NoInDocParms
For i = 1 To UBound(Tokens)
If Tokens(i) = "" Then
GoTo Continue_i: End If
KwVal = Split(UCase(Tokens(i)), "=")
If UBound(KwVal) <> 1 Then
msg = "Invalid keyword parameter (" & Tokens(i) & ")"
MsgBox msg, vbOKOnly, MyName: GoTo ErrExit: End If
Select Case Trim(KwVal(0))
Case "COLUMN"
... process the Column value
Case "Headers" 'If it's a Headers parameter,
... process the Headers value
Case "RGB1"
... process the RGB1 values
Case "RGB2"
... process the RGB2 values
Case "MATCHCASE"
... process the MatchCase value
Case Else
msg = "Invalid keyword parameter (" & Tokens(i) & ")"
MsgBox msg, vbOKOnly, MyName: GoTo ErrExit
End Select
Continue_i:
Next i
This is working as far as I have gotten. I'll post my complete code when it's ready.
Quote:
The macro can then be recoded as:
Code:
. . .
For i = 0 To UBound(Split(StrParms, "="))
Select Case i
i = 0: C = Trim(Split(Split(StrParms, "=")(1), ",")(0))
i = 1: StrRGB = Trim(Split(Split(StrParms, "=")(2), ",")(0))
i = 2: Hdr = Trim(Split(StrParms, "=")(3))
End Select
Next
. . .
|
That's an interesting and clever way to do that. At first it looked odd to me, but I see that it avoids having to test for i > UBound at each step and then having a label to Go To or messy nested If statements.
But I've never seen a Case statement coded like that. It's always:
Code:
Select Case i
Case 0: C = Trim(Split(Split(StrParms, "=")(1), ",")(0))
Case 1: StrRGB = Trim(Split(Split(StrParms, "=")(2), ",")(0))
Case 2: Hdr = Trim(Split(StrParms, "=")(3))
End Select