VBA Clear/.value = ""

MFish

Board Regular
Joined
May 9, 2019
Messages
76
I'm having an issue with VBA Clear Command Button.

I have a textbox named, txtLoc (Location of my depot) and many more textboxes/comboboxes. I also have my clear command button named, cmdClear. I'm just trying to clear the data out of all my userform criteria but this code doesn't work...

Sub cmdClear_Click()

txtLoc.clear
txtName.clear
setfocus.txtLoc

End Sub

But, why does this one work...

Sub cmdClear_Click()

txtLoc.value = ""
txtName.value = ""
setfocus.txtLoc

End Sub

? I am the type to try to understand "Why" this or "Why" that way it must be written... Can someone send me to a link or just quickly explain why?
Also, on another note, can someone tell me how to make a box that shows my code I've written? I see it in other messages. Thanks.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The .Clear property does not exist for a Texbox.
The property .Clear in a combobox is to remove the data added to the combo list; and also the data of the combobox.


To clean the textbox data can be:


Code:
    TextBox1.Value = ""
    TextBox1.Value = Null

To enter the code you must press the # icon (Wrap Code tags around selected text)
 
Upvote 0
To quickly clear all your UserForm Controls you can use a script like this
Code:
Private Sub CommandButton1_Click()
'Modified  5/14/2019  8:11:44 PM  EDT
For Each xControl In Me.Controls
        If TypeName(xControl) = "TextBox" Then xControl.Value = vbNullString
        If TypeName(xControl) = "ComboBox" Then xControl.Clear
        If TypeName(xControl) = "ListBox" Then xControl.Clear
        If TypeName(xControl) = "CheckBox" Then xControl.Value = False
        
Next xControl
End Sub
 
Upvote 0
You can try, if there's no problem unloading the userform:

Code:
Private Sub CommandButton1_Click()
Unload Me
UserForm1.Show
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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