Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variable 2

Status
Not open for further replies.

casg

IS-IT--Management
Jan 27, 2006
13
ES
I know it may sound very newbie... but
how can I create a global variable usable for serveral macros?

I mean some macros update that variable while others use its value.
 
Are you trying to pass a variable through several macro's or several sub's in one macro?

several macro's: quite a few options you could use a .ini or .txt to store the variable and use/modify it with your macro's

several sub's: Just Dim your variable at the top of your code prior to any subs.

 
Again, storing in files is a bad idea (TM) because of legal issues.
 
Give a general idea of what one macor is doing and needs to pass to next macro.

sounds more like you need to go with Subs or Functions within one macro to acomplish what you want to do.
 
casg,

If this involves the use of info from excel question you posted yesterday, post both your macro's and I'll show you how to roll them into one.
 
If you want your variable to be used throughout your entire EB, then you have to declare it outside a subroutine or function.
Code:
Dim MyVariable

Sub Main

End Sub

If you want your variable to actually be global so that it's set on the computer so another script can take it over.
Code:
Global MyVariable

Sub Main

End Sub
 
Ini file I reffered to in case anyone following this thread is interested in using it to pass variables from one macro to another. EB to EB or EB to anything else you care to call an INI with.
Code:
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
    ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
    ByVal lpFileName As String) As Long

Function ReadIniValue(strSection As String, strElement As String, strPath As String) As String
    Dim iret As Long            'Return Code
    Dim retStr As String * 30   'Return String -> Answer is in it
    Dim strdisplay As String
    iret = GetPrivateProfileString(strSection, strElement, "Default", retStr, 30, strPath)
    ReadIniValue = retStr
End Function

Function WriteIniValue(strSection As String, strElement As String, strValue As String, strPath As String) As String
Dim iret As Long          'return code
    iret = WritePrivateProfileString(strSection, strElement, strValue, strPath)
    WriteIniValue = "Done"
End Function

Sub Main
[COLOR=red]
RC = WriteIniValue("UserInfo","UserName","John Doe","c:\YourNewIni.ini")
RC = WriteIniValue("UserInfo","UserPhone","1-800-000-0000","c:\YourNewIni.ini")

RC = ReadIniValue("UserInfo","UserName","c:\YourNewIni.ini")
msgbox "User Name is "+RC
RC = ReadIniValue("UserInfo","UserPhone","c:\YourNewIni.ini")
msgbox "User Phone is "+RC
[/color]
End Sub

open c:\YourNewIni.ini in notepad and you'll see


[UserInfo]
UserName=John Doe
UserPhone=1-800-000-0000


Each ini file can have multiple [Sections] with multiple Elements
very usful to store prefrences for individual users if you use something like.

[SystemPrefs]
KeyboardMap = SomeKey.ekm
SomeCustomSetting = Color Red

[EmployeeInfo]
EmployeeNum = 87654321
Note1 = Modified per client request
Note2 = ect. ect.


 
Again, thanks, global seem what I was looking for. The other one is promising in general. I'll test both on Monday.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top