Rufus Clupea
Board Regular
- Joined
- Feb 11, 2019
- Messages
- 85
In reviewing/rewriting some code, I've come across a bit of a (for me) conundrum.
I realize this is probably another, "It depends..." situation, so if anyone could talk about (or point me to an article on) the pros & cons...
Here are the 2 situations...
The first example (WITH/END WITH inside the FOR/NEXT loop) seems briefer, and more readable, but the WITH/END WITH is executed i times. (Is that good or bad practice?) Granted, there aren't (usually) many OptionButtons in a group, but if the Sub were manipulating data in a large array or something...?
The second example (WITH/END WITH outside the FOR/NEXT loop) is... 'wordier', but the WITH/END WITH only need be executed once. Again, I spoze one criterion is the size of i; are there any others?
FWIW, I did try the search facility--and google; neither seems to like (combination of) search terms.
Once again, TYA
I realize this is probably another, "It depends..." situation, so if anyone could talk about (or point me to an article on) the pros & cons...
Here are the 2 situations...
Code:
Public Sub Sample1(Low As Long, High As Long)
Dim i As Long
For i = Low To High
With UserForm1.Controls("OptionButton" & i)
If .Value = True Then
.Font.Bold = True:
.ForeColor = &H80000012:
.TabStop = True
Else
.Font.Bold = False:
.ForeColor = &H80000011:
.TabStop = False
End If
End With
Next i
End Sub
Public Sub Sample2(Low As Long, High As Long)
Dim i As Long
With UserForm1
For i = Low To High
If Controls("OptionButton" & i).Value = True Then
Controls("OptionButton" & i).Font.Bold = True:
Controls("OptionButton" & i).ForeColor = &H80000012:
Controls("OptionButton" & i).TabStop = True
Else
Controls("OptionButton" & i).Font.Bold = False:
Controls("OptionButton" & i).ForeColor = &H80000011:
Controls("OptionButton" & i).TabStop = False
End If
Next i
End With
End Sub
The first example (WITH/END WITH inside the FOR/NEXT loop) seems briefer, and more readable, but the WITH/END WITH is executed i times. (Is that good or bad practice?) Granted, there aren't (usually) many OptionButtons in a group, but if the Sub were manipulating data in a large array or something...?
The second example (WITH/END WITH outside the FOR/NEXT loop) is... 'wordier', but the WITH/END WITH only need be executed once. Again, I spoze one criterion is the size of i; are there any others?
FWIW, I did try the search facility--and google; neither seems to like (combination of) search terms.
Once again, TYA