Public variable value not being shared by other modules

ecchahine

New Member
Joined
Mar 31, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi guys. I'm fairly new to VBA and I'm having a small problem here. On a certain project I'm working on I have declared a couple of public Boolean variables. The value of this variable is consistent inside the same module, but if for example I change the value of Public x as Boolean to True in module1 macro1 and try to print its value in module2 macro2 the message will be empty. Any ideas?

Here's a small example:

Public macro As Boolean
__________________________

Private Sub Workbook_Open()

macro = True
.
.
End Sub

' In a different Module

Sub Test()
MsgBox macro
End Sub

The msgbox will be empty. (I used the msgbox just to illustrate the fact that the value isn't being passed on)
 
If your code is running OK, the values in temp_array_lbr will persist once it is populated.

But if the code halts due to an unhandled error, the values will not persist. So if you are still developing your code and making errors, you'll lose what's in temp_array_lbr when the code breaks.

Strip it back to basics - populate temp_array_lbr in Module 1 and test in Module 2. Something like this should work as expected?

VBA Code:
Public temp_array_lbr() As Variant
Sub Test1()

    Dim i As Long, j As Long, k As Long
    ReDim temp_array_lbr(1 To 10, 1 To 15, 1 To 20)
  
    For i = LBound(temp_array_lbr, 1) To UBound(temp_array_lbr, 1)
        For j = LBound(temp_array_lbr, 2) To UBound(temp_array_lbr, 2)
            For k = LBound(temp_array_lbr, 3) To UBound(temp_array_lbr, 3)
                temp_array_lbr(i, j, k) = i + 2 * j + 3 * k
            Next k
        Next j
    Next i

End Sub
'Module 2
Sub Test2()

    MsgBox temp_array_lbr(1, 1, 1)  'answer 6
    MsgBox temp_array_lbr(1, 10, 5)  'answer 36
  
End Sub
Big help here...it is working. Thank you very much!
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top