VBA: Looping through textboxes.

dbmathis

Well-known Member
Joined
Sep 22, 2002
Messages
1,064
Hi all,

I know this is a dumb question, but how do I loop through textboxs rather than do this?

Code:
Private Sub Clear_Click()

TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""

End Sub

These textboxes exist on a worksheet and were added with the VBA toolbar.

Thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try this.
Code:
Dim ws As Worksheet
Dim t
    Set ws = Worksheets(1)
    For Each t In ws.OLEObjects
        If Left(t.Name, 7) = "TextBox" Then
            t.Object.Text = ""
        End If
    Next t
 
Upvote 0
Like this?

Code:
Private Sub Clear_Click()
    Dim x As Integer
    For x = 1 To 4
        ActiveSheet.OLEObjects("TextBox" & x).Object.Text = ""
    Next x
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,900
Members
449,194
Latest member
JayEggleton

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