Syntax for For each ctrl in

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,822
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Having trouble applying a name to the line

For Each ctrl In Form1.Controls

The name of my Form is "frmLayout"

How can I change Form1.Controls to "frmLayout".Controls ?

ROFL please don't tell me just to type in it without the quotes, I mean when the name is in a string variable. Thanks.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Use something like:

Code:
VBA.UserForms.Add(strVariable).Controls

Or if you're inside the UserForm with this code you could use

Code:
Me.Controls
 
Upvote 0
Thanks, that does work, but it runs each Forms Initialize event.
Can that be avoided anyhow?
 
Upvote 0
Probably not with the code as it is.

Only thing I can think of is to switch from using Initialize to using a Display sub instead. Here's an example.

Normally you might use 'UserForm1.Show' and have the following code in that UserForm:
Code:
Option Explicit


Private Sub UserForm_Initialize()
    Label1.Caption = "Hello World"
    TextBox1.Value = "Type your name"
End Sub


Private Sub CommandButton1_Click()
    MsgBox "Hello " & TextBox1.Value
End Sub

As you say the Initialize event would fire.
If we remove the Initialize event and instead do all the initial set up with a Public Sub inside the UserForm we still get the same functionality but the Initialize event can't fire because it is not there.

So you would show this UserForm using UserForm1.Display with the code below:
Code:
Option Explicit


Public Sub Display()
    Label1.Caption = "Hello World"
    TextBox1.Value = "Type your name"
    
    Me.Show
End Sub


Private Sub CommandButton1_Click()
    MsgBox "Hello " & TextBox1.Value
End Sub

As you can see the Display sub incorporates the .Show command after it is finished initialising.
Tested this and it also works with 'VBA.UserForms.Add(strVariable).Display'

Hopefully you can apply this logic to your example and therefore bypass the Initialize event.
HTH!
 
Upvote 0
Hi,

That's great! I did find another way to get it done without Initialise running, but your suggestion
is very welcome as I can see other uses. One especially - I've found under some (unknown) situations
the Initialise (or Activate) events don't fire consistently and this would solve that problem.

Many thanks :)
 
Upvote 0

Forum statistics

Threads
1,203,203
Messages
6,054,110
Members
444,702
Latest member
patrickmg17

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