Two ways to reference, which is smart design?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
488
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have two simple sample pieces of code. They both do the same thing and they both work. My question is: which is correct? The second example is less text. Does that make it run any faster? Is there a downside?

VBA Code:
Sub TestOpen()

        SearchEdit.Show
        SearchEdit.SE_MS_select.Visible = False
        SearchEdit.MS_warning.Visible = False
        SearchEdit.SE_ModelIndication.Visible = False
        SearchEdit.SE_DatePending.Visible = False
        SearchEdit.SE_DateConfirmed.Visible = False
        SearchEdit.SE_DateUnConfirmed.Visible = False
        SearchEdit.SE_Confirm.Visible = False
        SearchEdit.SE_Unconfirm.Visible = False
        SearchEdit.cmdNotPending.Visible = False

       SearchEdit.SE_Notes_11.Value = Sheets("Calendar").Range("FA" & ActiveCell.Row).Value

End Sub

VBA Code:
Sub TestOpen2()

With SearchEdit
   
        .Show
        .SE_MS_select.Visible = False
        .MS_warning.Visible = False
        .SE_ModelIndication.Visible = False
        .SE_DatePending.Visible = False
        .SE_DateConfirmed.Visible = False
        .SE_DateUnConfirmed.Visible = False
        .SE_Confirm.Visible = False
        .SE_Unconfirm.Visible = False
        .cmdNotPending.Visible = False
       
End With

End Sub

Also related, I've noticed that this code works:

VBA Code:
SearchEdit.SE_Notes_11.Value = Sheets("Calendar").Range("FA" & ActiveCell.Row).Value

...and so does this:


VBA Code:
SE_Notes_11 = Sheets("Calendar").Range("FA" & ActiveCell.Row)

The second example is "cleaner," but is it any more efficient (speed-wise)?
 
Upvote 0
Also related, I've noticed that this code works:
SearchEdit.SE_Notes_11.Value = Sheets("Calendar").Range("FA" & ActiveCell.Row).Value
...and so does this:
SE_Notes_11 = Sheets("Calendar").Range("FA" & ActiveCell.Row)
The second example is "cleaner," but is it any more efficient (speed-wise)?

Those two snippets work and some others as verbose as the following two should also work:
VBA Code:
SearchEdit.Controls.Item("SE_Notes_11").Value = Application.ThisWorkbook.Sheets.Item("Calendar").Range("FA" & Application.ActiveCell.Row).Value
SearchEdit.Controls!SE_Notes_11 = Sheets!Calendar.Range("FA" & Application.ActiveCell.Row).Value
VBA offers flexibility with both implicit and explicit references. While the performance impact of implicit references is typically negligible, it can become significant when dealing with thousands of iterations. To avoid ambiguity and unnecessary resource usage, it’s generally better to write explicit code. For instance, SearchEdit.SE_Notes_11 could refer to either the control or its value, depending on the context; since you're trying to get a value in your example, VBA uses its resources to determine that you actually meant SearchEdit.SE_Notes_11.Value. But if you had something like this, it would return an object:
Code:
Dim myControl As Object
Set myControl = SearchEdit.SE_Notes_11
Therefore, you could do this:
Msgbox myControl.Width

How can you know what it will return? take a look at the object browser, when a member of an object is the default one, it has a cyan icon next to it and the description tells you it is the default member. Intellisense also hints that.
1739255822738.png
 
Last edited:
Upvote 0
Also related, I've noticed that this code works:

VBA Code:
SearchEdit.SE_Notes_11.Value = Sheets("Calendar").Range("FA" & ActiveCell.Row).Value

...and so does this:


VBA Code:
SE_Notes_11 = Sheets("Calendar").Range("FA" & ActiveCell.Row)

The second example is "cleaner," but is it any more efficient (speed-wise)?
The second one will only work if the code is in that form. If it is, you should really be using that second version, not the prior one, since the userform is effectively a class and the second one is really short for Me.SE_Notes_11 = ... which will always refer to the current instance of the class.
 
Upvote 0

Forum statistics

Threads
1,226,771
Messages
6,192,919
Members
453,767
Latest member
922aloose

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