How to make a value available from one sub to other sub

H

hiryu

Hello,

I'm a newbie to Word Macro, I'm writing a macro using a custom dialog
box. I want to set some value to a variable during initialize, and to
make that variable and it's value to other Sub, eg after clicking a
button the process require to access the that variable and it's value.

Here is my simplfied code to demonstrate my idea:

Sub Button_Click()
MsgBox (x)
End Sub

Sub UserForm_Initialize()
x = "hello"
End Sub


First I want to assign a value to "x" during initalizing the userform,
then when I click a button to popup a message box and show the "x"
value. I've tried using public, static in declaration but no luck, the
value couldn't be transferred to other Sub. Could someone tell me to
solve this please?

Thanks
 
M

Mick Hardy

Hi hiryu,

You create a private form level variable above your two procedures.
If you need the variable in other modules or classes, create a public
property to expose your variable. Here's the code.

Option Explicit

'Form level property variable
Private fstrNameMe As String

Private Sub CommandButton1_Click()
MsgBox fstrNameMe
End Sub

Private Sub CommandButton2_Click()
MsgBox Me.NameMe
End Sub

Private Sub UserForm_Initialize()
fstrNameMe = "hello"
End Sub

'Expose the property to other modules
'and classes using this form
Public Property Get NameMe() As String
NameMe = fstrNameMe
End Property

Public Property Let NameMe(ByVal vstrNewValue As String)
fstrNameMe = vstrNewValue
End Property

Mick
 
H

hiryu

(e-mail address removed) (Mick Hardy) wrote in
'Form level property variable
Private fstrNameMe As String

Thanks,
I found that just adding the one above only would work
 
M

Mark Mulvany

hiryu said:
Hello,

I'm a newbie to Word Macro, I'm writing a macro using a custom dialog
box. I want to set some value to a variable during initialize, and to
make that variable and it's value to other Sub, eg after clicking a
button the process require to access the that variable and it's value.

Here is my simplfied code to demonstrate my idea:

Sub Button_Click()
MsgBox (x)
End Sub

Sub UserForm_Initialize()
x = "hello"
End Sub


First I want to assign a value to "x" during initalizing the userform,
then when I click a button to popup a message box and show the "x"
value. I've tried using public, static in declaration but no luck, the
value couldn't be transferred to other Sub. Could someone tell me to
solve this please?

Thanks


There is another similar way of doing this. You can plant a value in a
function:

Private Function GetX() As String 'Syntax might be wrong but it gives the
idea
GetX = "hello" 'The return value of the function is the word hello
End Function

Coding...

MsgBox(GetX)

would display the text - because it is the return value of the function.
It does'nt matter to the user where the text comes from. The appearance
is the same!

Mark Mulvany
 

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