Strange: "For Each Ctrl In UserForm1.Controls" does not work

Xenova

New Member
Joined
Mar 31, 2012
Messages
3
Hi all,

I have encountered the following weird situation. The following codes run well in the IDE (when we click the Green Play button). But the codes won't execute when actually using the excel file - after open the file, click the the "START" button, then a userform will be shown. There are few textboxes, enter anything into the textbox, then click the commandbutton at the bottom suppose will set all the textbox to null (as it did in the IDE). But it won't work here.

I attached a copy of the file to ease you to look at the problem.
Download it from my dropbox: http://dl.dropbox.com/u/68179510/Sample%201.xls
The following codes is part of the codes in the UserForm1


Sub ButtonReset_Click()

Dim Ctrl As Control
For Each Ctrl In UserForm1.Controls
If TypeName(Ctrl) = "TextBox" Then
Ctrl.Value = vbNullString
End If
Next Ctrl

End Sub


Thank you very much for all the attention and helps :biggrin:
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
try changing:

Code:
    Dim Ctrl As MSForms.Control
 
Upvote 0
Change this line:
Code:
For Each Ctrl In UserForm1.Controls


To this:
Code:
For Each Ctrl In Me.Controls

Or, you can omit UserForm1 entirely:
Code:
For Each Ctrl In Controls
 
Upvote 0
try changing:

Code:
    Dim Ctrl As MSForms.Control


Thanks for the fast reply.
I tried to declare as MSForms.Control, but it giving me the same result.
The codes run well in the IDE, but not when i run from the excel file.;)
 
Upvote 0
Change this line:
Code:
For Each Ctrl In UserForm1.Controls


To this:
Code:
For Each Ctrl In Me.Controls

Or, you can omit UserForm1 entirely:
Code:
For Each Ctrl In Controls


Really thanks. "Me.Controls" works!!!
I still wondering why "UserForm1" does not work as expected :confused:
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,641
Members
449,461
Latest member
kokoanutt

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