textboxes within userforms

Kmsanton

Board Regular
Joined
Jul 7, 2006
Messages
91
I created a userform and within that userform I placed a textbox. When you click on my command button the userform appears and you can type into the textbox. Is there a code that will clear the textbox everytime you open the excel document. In addition is there another code you can put in so that when you press a different command button it will clear the textbox as well.

Would they be the same code??
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
You shouldn't need any code to have the textbox appear blank when the form is opened, unless you've linked it to a cell of course.

You can easily clear a textbox like this.
Code:
Private Sub CommandButton1_Click()
    TextBox1.Text = vbNullString
End Sub
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Like this?

Code:
Private Sub UserForm_Initialize()
    TextBox1.Text = ""
End Sub

Private Sub CommandButton1_Click()
    TextBox1.Text = ""
End Sub
 

Kmsanton

Board Regular
Joined
Jul 7, 2006
Messages
91
not working

neither of those are working. They work if I create a textbox that is not in a userform but they dont work for the textbox that is featured on my userform. My userform isn't always visible, does that matter. I've named the text box : textbox_lineal

This is the code I used

Private Sub UserForm_Initialize()
TextBox_lineal.Text = ""
End Sub

Private Sub CommandButton1_Click()
TextBox_lineal.Text = ""
End Sub


I also tried


Private Sub CommandButton1_Click()
TextBox_lineal.Text = vbNullString
End Sub




Nothing worked....any ideas
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Where is this code located?

Both Andrew and my code should only work with a textbox on a userform.

The code should be in the userform's own module.
 

Kmsanton

Board Regular
Joined
Jul 7, 2006
Messages
91
I put the initialize code into my textbox and that code works but I tried to put the command button code into my userform and that doesn't work. My command button is featured on my worksheet not my userform. When I press this command button I want the textbox on my userform to be blank. Where should I put the code to do this??
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
Sorry I'm a little bit confused.:eek:

Why do you have the command button on the worksheet?

Why not have it on the userform?

Anyway, any code for a command button on a worksheet should go in the worksheet module of the worksheet it's on.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,119
Messages
5,640,219
Members
417,131
Latest member
Seanr19871

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
Top