Pass value from userform textbox to vba procedure

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a userform where the user will input a number into a textbox and that value should be passed to a vba procedure for use as a variable.

I have declared the variable in vba as long, but when stepping through the procedure it show this line as ""

Code:
i = Val(frmUserForm.txtnumber.Text)
 
Okay now I got stuck again. I have declard the variable global in a standard module, but not how do I empty that variable.

If the user opens the form and adds a number to the textbox and the procedure performs its function, the next time the user opens the form and hits cancel but has not entered anything in the textbox, the public variable is still stored.

How can I empty out the variable after the procedure has run?
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
try this


Code:
Sub Test()
    Sheets("Sheet1").Range("A1").Copy Sheets("Sheet").Range("A1").Resize(i)
    TextBox1.Text = vbNullString
End Sub
 
Private Sub TextBox1_Change()
    i = Val(TextBox1.Text)
End Sub
 
Upvote 0
Rasm,

Thanks again for the help, but I couldn't seem to get it to work with your method.

I have found another method which works. On form Initialize I've made the value of the textbox = to 0 so if they click the button I added if i = 0 then exit sub.

Works grand.
 
Upvote 0
Gail

You shouldn't need to use a global/public variable for this.

In fact you could do it without a variable.

Just pass the value from the form to the sub in the other module before you unload the form.

Module1:
Code:
Option Explicit
 
Sub OpenUserForm1()

    UserForm1.Show

End Sub
 
Sub Test(X As Long)

    Sheets("Sheet1").Range("A1").Copy Sheets("Sheet2").Range("A1").Resize(X)

End Sub

Userform Module:
Code:
Option Explicit
 
Private Sub CommandButton1_Click()
    Test CLng(TextBox1.Value)
    Unload Me
End Sub
This worked fine for me, though I'm sure it's not perfect and there's probably a bit more to what you want to do.:)
 
Upvote 0
So for the delay Norie, this worked great for my particular process.
 
Upvote 0
I have a userform where the user will input a number into a textbox and that value should be passed to a vba procedure for use as a variable.

I have declared the variable in vba as long, but when stepping through the procedure it show this line as ""

Code:
i = Val(frmUserForm.txtnumber.Text)
I used your code to do something that's probably easy, BUT it does not work. SCREENSHOT:
vbafix.PNG

Help me make this work! Download Link to Exported File: CLICK :p
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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