ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to iterate through all Text Boxes on a Form? (https://www.excelbanter.com/excel-programming/323226-how-iterate-through-all-text-boxes-form.html)

Ed

How to iterate through all Text Boxes on a Form?
 
I can iterate through all the controls on a UserForm, but I can't find a
"Type" or other property that will let me get just the Text Boxes. Any
help?

Ed



Harald Staff

How to iterate through all Text Boxes on a Form?
 
Hi Ed

Put them into your own new custom collection like this:

Option Explicit

Dim MyTextBoxes As Collection

Private Sub UserForm_Initialize()
Set MyTextBoxes = New Collection
MyTextBoxes.Add Me.TextBox1, "T1"
MyTextBoxes.Add Me.TextBox2, "T2"
MyTextBoxes.Add Me.TextBox3, "T3"
End Sub

Private Sub CommandButton1_Click()
Dim TB As MSForms.TextBox
For Each TB In MyTextBoxes
MsgBox TB.Text
Next
MsgBox MyTextBoxes("T2").Text
MsgBox MyTextBoxes(1).Text
End Sub

HTH. Best wishes Harald

"Ed" skrev i melding
...
I can iterate through all the controls on a UserForm, but I can't find a
"Type" or other property that will let me get just the Text Boxes. Any
help?

Ed





Tom Ogilvy

How to iterate through all Text Boxes on a Form?
 
for each ctrl in Userform1.controls
if typeof ctrl is MSForms.Textbox then
ctrl.Value = ""
end if
Next

--
Regards,
Tom Ogilvy

"Ed" wrote in message
...
I can iterate through all the controls on a UserForm, but I can't find a
"Type" or other property that will let me get just the Text Boxes. Any
help?

Ed





Ed

How to iterate through all Text Boxes on a Form?
 
Wow! You've helped quite a bit today alone, Tom. Hope some day I can
return the favor.

Thank you!
Ed

"Tom Ogilvy" wrote in message
...
for each ctrl in Userform1.controls
if typeof ctrl is MSForms.Textbox then
ctrl.Value = ""
end if
Next

--
Regards,
Tom Ogilvy

"Ed" wrote in message
...
I can iterate through all the controls on a UserForm, but I can't find a
"Type" or other property that will let me get just the Text Boxes. Any
help?

Ed







Harald Staff

How to iterate through all Text Boxes on a Form?
 
Hi Tom

This is probably only a theoretical problem, but code that loops all
controls might loop and test 200+ of them every time just to get something
done to 5 of them.

Best wishes Harald

"Tom Ogilvy" skrev i melding
...
for each ctrl in Userform1.controls
if typeof ctrl is MSForms.Textbox then
ctrl.Value = ""
end if
Next




Ed

How to iterate through all Text Boxes on a Form?
 
Harald - I didn't think of that. At the moment, I don't have that problem.
Later on, though (assuming I get good enough that people want more
complicated stuff), that can become a problem. I will look into the custom
collections - that has definite possibilities.

Thanks.
Ed

"Harald Staff" wrote in message
...
Hi Tom

This is probably only a theoretical problem, but code that loops all
controls might loop and test 200+ of them every time just to get something
done to 5 of them.

Best wishes Harald

"Tom Ogilvy" skrev i melding
...
for each ctrl in Userform1.controls
if typeof ctrl is MSForms.Textbox then
ctrl.Value = ""
end if
Next







All times are GMT +1. The time now is 11:02 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com