I have two excel files, a patcher.xlsm and a file.xlsm. There is a button on file.xlsm, when it clicks it executes a procedure on Sheet 1 with a message box: The magic number is: 5.
Now, I have a patcher.xlsm, that would re-import a patched Sheet 1 and a Standard module with a global variable and run a macro on file.xlsm to set this number to 7. The expected result is such that, users clicking on file.xlsm would now see The magic number is: 7.
For some reason, whenever I executed patcher.xlsm, the magic number is always reset to 0, as though the memory has been cleared or project reset.
Is there a way to assign a global variable from another workbook?
In the past, I have looked at several stackoverflow threads, example, changing the scope of the variable, private, public, global, etc, activating the workbook, looking at Excel's hidden name space at the moment.
Sub Proc()
MsgBox "The magic number is: 5"
End Sub
Sheet1 in file.xlsm
Call CopyModule("patcher_b_progress_bar.xlsm", "file_b.xlsm", "Sheet1", True)
Call CopyModule("patcher_b_progress_bar.xlsm", "file_b.xlsm", "GlobalModule", True)
Application.Run ("'file_b.xlsm'!GlobalModule.setDefaultMagicNumber")
patcher.xlsm "patching" the sheet 1 and setting a magic number via a module
CopyModule
' export "patched GlobalModule" from patcher.xlsm as temp.bas
' remove GlobalModule from file.xlsm
' re-import "patched GlobalModule" to file.xlsm
(too lengthy to post here, referenced from http://www.cpearson.com/excel/vbe.aspx)
CopyModule of patcher.xlsm
Option Explicit
Private MAGIC_NUMBER As Integer
Public Function getMagicNumber() As Integer
getMagicNumber = MAGIC_NUMBER
End Function
Public Function setDefaultMagicNumber() As Integer
MAGIC_NUMBER = 7
setDefaultMagicNumber= getMagicNumber()
End Function
GlobalModule of patcher.xlsm
Sub Proc()
If GlobalModule.getMagicNumber= 7 Then
MsgBox "Magic number is correct: " & GlobalModule.getMagicNumber
Else
MsgBox "Magic number is wrong: " & GlobalModule.getMagicNumber
End If
End Sub
Sheet1 of patcher.xlsm (to be overwritten to file.xlsm)
EDIT: I have modified the issue. It seems like the variable MAGIC_NUMBER is re-initialise to "0" even though the line "GlobalModule.setDefaultMagicNumber" is executed successfully.