Pros/Cons of For..Next with With...End With Combinations?

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. :confused:

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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The first should be more efficient since you are creating a variable once and referencing it. The latter requires finding the correct control multiple times per iteration. The use of With (and the loop) is kind of irrelevant here (aside from the fact that you aren't actually using the with in the second sub), the question is really which is more efficient:
Code:
Dim a as Object
Set a = SomeControl
a.Value1 = "abc"
a.Value2 = 234
Or
Code:
SomeControl.Value1 = "abc"
SomeControl.Value2 = 234

The first should be more efficient, the second depends on the costliness of returning SomeControl. Specifically in your example:
Code:
Controls("OptionButton" & i).TabStop = False

You have to look up the control object from a collection multiple times, this is clearly less efficient than returning it once and using that pointer to reference its properties.

The effect is simply compounded when using a loop.
 
Last edited:
Upvote 0
The first should be more efficient since you are creating a variable once and referencing it. The latter requires finding the correct control multiple times per iteration. The use of With (and the loop) is kind of irrelevant here (aside from the fact that you aren't actually using the with in the second sub),

Actually, I am--I (hurriedly) wrote the example incorrectly; those "Controls" should have been ".Controls" (My bad. :oops:)

the question is really which is more efficient:

Sorry, that's what I thought I was asking.

You have to look up the control object from a collection multiple times, this is clearly less efficient than returning it once and using that pointer to reference its properties.

The effect is simply compounded when using a loop.

I'm sure that's "clear" to some/many; I did not know/understand that. Unfortunately, I number among those who are trying to learn these subtleties; that's why I ask. ;)
 
Upvote 0
I may be wrong, but I feel that having read my post back, it comes across as condescending. That certainly wasn't the intention, so I apologise if it was taken that way. I was attempting to clarify and reframe the question.

99/100 it probably doesn't matter whichever way you do it. People have a tendency to prematurely optimise when it comes to Excel on these forums, this would probably be one of the lower priority things when looking how to optimise code - the only exception being if you knew it was particularly expensive to get a handle to the object. I can't think of any specific examples built into Excel right now, but I have been in this situation where I have written objects that would be expensive to requery.
 
Upvote 0
99/100 it probably doesn't matter whichever way you do it. People have a tendency to prematurely optimise when it comes to Excel on these forums, this would probably be one of the lower priority things when looking how to optimise code - the only exception being if you knew it was particularly expensive to get a handle to the object. I can't think of any specific examples built into Excel right now, but I have been in this situation where I have written objects that would be expensive to requery.

IC. The greatest number of OptionButtons I'll be checking at a time is 14--no biggie. However, I'll have similar handling Subs for CheckBoxes (~75) and SpinButtons (~300), so I can see the possibility of things bogging down (or not) depending on which way I go. (Breaking those down into smaller groups will affect functionality, so I'd prefer to optimize from the get-go.)

You've given me some things to think about/explore.

TA
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top