Pass a variable to a different sub

R

RyanH

I have a 2 command buttons on a userform. cmbCalculate and cmbAddToQuote.
cmbCalculate has code that confirms all textboxes have data before
calculating a price. cmbAddToQuote adds the price to a worksheet. How can I
pass a variable value from cmbCalculate code to cmbAddToQuote code. Here is
what I have for an example. I want to stop Sub cmbAddToQuote_Click() if the
variable StopCode = True. IS this possible?

Public Sub cmbCalculate_Click()

Dim StopCode as Boolean

If textbox1 = "" Then
MsgBox "Try Again."
StopCode = True
Exit Sub
End If
End Sub

Private Sub cmbAddToQuote_Click()

Call cmbCalculate_Click

If StopCode = True Then Exit Sub

'.....additional code
End Sub

Thanks in Advance, Ryan
 
D

Dave Peterson

Did you see the responses to your other post?
I have a 2 command buttons on a userform. cmbCalculate and cmbAddToQuote.
cmbCalculate has code that confirms all textboxes have data before
calculating a price. cmbAddToQuote adds the price to a worksheet. How can I
pass a variable value from cmbCalculate code to cmbAddToQuote code. Here is
what I have for an example. I want to stop Sub cmbAddToQuote_Click() if the
variable StopCode = True. IS this possible?

Public Sub cmbCalculate_Click()

Dim StopCode as Boolean

If textbox1 = "" Then
MsgBox "Try Again."
StopCode = True
Exit Sub
End If
End Sub

Private Sub cmbAddToQuote_Click()

Call cmbCalculate_Click

If StopCode = True Then Exit Sub

'.....additional code
End Sub

Thanks in Advance, Ryan
 
C

Chip Pearson

Ryan,

You can declare the variable StopCode at the module level (before and
outside of any Sub or Function procedure) so that its value is preserved and
it is accessible from any procedure within the module. Don't declare the
variable within a Sub or Function procedure.

See http://www.cpearson.com/Excel/Scope.aspx for a discussion of variable
and procedure "scope".


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top