'VBA -- Using a Userbox checkbox as show below, how do I set the value of a variable using a checkbox and then use that variable in a Module?
So currently, I have the following in a Userbox in VBA; a checkbox and a close window button.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then vb1 = -1 Else vb1 = 1
End Sub
Private Sub CommandButton1_Click()
UserForm1.Hide
End Sub
And I have this in my Module.
Option Explicit
Sub GetRangeData_inputbox()
Dim vb1, vb2, vb3, vb4, vb5, vb6, vb7 As Integer
Range("c3") = vb1 * 1000
The primary purpose of the userbox is to check a box. I opted for a userbox since there are many of these boxes to be checked, but I only put one in for simplistic purposes.
The Goal of this is for a box to be checked, change vb1 to -1, or vb1 to 1 if the box is checked or unchecked, then, in the module, flow into a formula that does 1,000 (for example)*VB1.
Let me know,
Thanks!
Solution 1:[1]
To use a variable in a sub you must declare it like this Sub GetRangeData_inputbox(vb1 As Integer) and then you must call the sub in the CheckBox1_Click() event.
Here is a proof of concept:
Private Sub CheckBox1_Click()
Dim vb1 As Integer
If CheckBox1.Value = True Then vb1 = -1 Else vb1 = 1
Call GetRangeData_inputbox(vb1)
End Sub
And in the module:
Sub GetRangeData_inputbox(vb1 As Integer)
Range("c3") = vb1 * 1000
End Sub
Also you can replace UserForm1.Hide with UserForm1.Unload, unless you have a reason for hiding it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Matr1x |
