ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to clear items on a form? (https://www.excelbanter.com/excel-programming/342589-how-clear-items-form.html)

Grd

How to clear items on a form?
 
Hi,

I have a user form with details such as first name and last name. IF the
user wishes to start again, I would like a clear or reset button to clear the
fields that they have typed.

Is there a way to loop through each textbox to clear it to blank?

Thanks

G

Jake Marx[_3_]

How to clear items on a form?
 
G,

Grd wrote:
I have a user form with details such as first name and last name. IF
the user wishes to start again, I would like a clear or reset button
to clear the fields that they have typed.

Is there a way to loop through each textbox to clear it to blank?


Here's one way:

Private Sub ClearAll()
Dim ctl As Control

For Each ctl In Controls
If TypeOf ctl Is MSForms.TextBox Then
ctl.Object.Text = ""
End If
Next ctl
End Sub

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]



dtb

How to clear items on a form?
 
It's not elegant, but assuming you name all your TextBox controls
consistently (i.e. begin with txt) then the following will work.

Dim ctrl As Control

For Each ctrl In UserForm1.Controls
If Left(ctrl.Name, 3) = "txt" Then
ctrl.Text = ""
End If
Next

Just add this to the Click event of a button on your UserForm.



"Grd" wrote:

Hi,

I have a user form with details such as first name and last name. IF the
user wishes to start again, I would like a clear or reset button to clear the
fields that they have typed.

Is there a way to loop through each textbox to clear it to blank?

Thanks

G



All times are GMT +1. The time now is 05:51 AM.

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