Code to loop through textboxes on a userform without specifying the name

ktorres

New Member
Joined
Jan 12, 2016
Messages
4
I have several userforms and each one has several textboxes for which some are named textbox and others are named "HC", "CC" etc. with numbers ranging from 1 to 400. (I should have set up my forms in a more consistent way but we're past that now I think).

Is there code that would loop through all the textboxes in the userform and insert a "0" if that textbox.value = ""?

Thanks!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
ktorres,

You might try the following...

Code:
Private Sub CommandButton1_Click()
Dim tb As Control

For Each tb In Me.Controls
    If TypeName(tb) = "TextBox" Then
        If tb.Value = "?" Then tb.Value = "0"
    End If
Next tb

End Sub

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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