PDA

View Full Version : Variables from other subs



BENatUSGS
10-15-2010, 08:24 AM
Hello All,

Is it possible to pull variable names from previous private subs?
Like,

Private sub ABC ()
Dim ABC As Integer
ABC = InputBox(“Enter Value”)
End sub

Private Sub CheckABC ()
MsgBox ABC
End sub

I have a userform with quite a lot of variables throughout. I would like to have one private sub that will post all the cumulated variables from the all private subs within the userform code.

Let me know,
Ben

Kenneth Hobs
10-15-2010, 09:59 AM
It would be best to write those variable values to cells as they are used. You could pass variables to some subs as parameters but there is a limit.

The most inefficient method is to declare your variables as public or private. e.g.
Private ABC As Integer

Private Sub ABCSet()
ABC = Application.InputBox("Enter Value", "Numbers Truncated to Integer", , , , , , 1)
End Sub

Private Sub CheckABC()
MsgBox ABC
End Sub

The better way:
Private Sub ABCSet()
CheckABC Application.InputBox("Enter Value", "Numbers Truncated to Integer", , , , , , 1)
End Sub

Private Sub CheckABC(ABC As Integer)
MsgBox ABC
End Sub

Zack Barresse
10-16-2010, 11:56 AM
I don't know about inefficiency with public/private variables when it comes to VBA. You're not going to see any real speed differences, so I'm not sure where the inefficiency is going to come from. You're still declaring a variable in your routine, it's just as an argument. Plus setting variables allow more control over the data and debugging process. Ben, can you post more information about what it is you are trying to do, and all of your code?

BENatUSGS
10-18-2010, 08:48 AM
Zack and Kenneth,
Here is some more information on what it is I am trying to do exactly.
My goal/task is to create a user form for data input (See attached I attempted to post and image of what the userform looks like). The userform holds a number of different ways to input data and each input is assigned a variable. When the user has completed their input, I would like there to be some sort of check or summation of the inputted values. I tried to find a way to have this “check” show up somewhere on the userform but could not find a way to do that. So, I decided that I would create a msgbox that would compile all the variables and show them back to the user. The purpose of doing this is to make the user perform one last check before their inputted data gets sent to a spread sheet.

Here is the code I wrote for the msgbox. The variables; sn, Msg, ssint, rsint, rval, are from the code in other parts of the userform.
Private Sub CheckButton_Click()

Dim gsm As String
Dim config As Integer
Dim sna As Integer

gsm = "Here is your inputed data. Make sure this is correct"
gsm = gsm & vbNewLine
gsm = gsm & sn & Msg & ssint & rsint & rval
config = vbYesNo + vbQuestion
sna = MsgBox(gsm, config)
If sna = vbNo Then GoTo tt
tt:
End Sub

Kenneth Hobs
10-18-2010, 09:13 AM
If you want to use variables, then use the Private or Public method.

Another method is to store the values into controls in a userform.

e.g.
Private Sub ABC()
UserForm1.TextBox1.Value = InputBox("Enter Value")
End Sub

Private Sub CheckABC()
MsgBox UserForm1.TextBox1.Value
End Sub