I'm having a difficult time trying to use global variables across modules in VBA.
I have a two-dimensional array that I am using to store values from a sheet in Excel. I want to create an undo button, where I retrieve the last stored values, what I'm looking at is using a global variable, that is edited in one module and referenced in another.
The issue is, I don't think that the program is allowing me to carry over the changes from one module into another. Is there anything I can do to facilitate this? Below is a summary of what I'd like to accomplish:
Public empsTot As Variant
Sub update()
ReDim Preserve empsTot(5, numOfEmp, numOfFields)
'emps(,,) would be range extracted from excel
For i = 0 To numOfFields
For j = 0 To numOfEmp
empsTot(0, j, i) = emps(j, i)
Next
Next
'repeat the above process for each new entry, changing empsTot(++,j,i)
End sub
Sub retrieveValues()
For i = 0 To 6
For j = 0 To 3
'add values to sheet using something like
Range = empsTot([last value],j,i)
Next
Next
End Sub
Public week As Integer
Public empsTot As Variant
Sub update()
week = week + 1
If week > 5 Then
week = 5
End If
Worksheets("Data").Activate
Dim numOfEmp
Let numOfEmp = ThisWorkbook.Sheets("Accessory Sheet").Range("I2")
numOfEmp = numOfEmp
ReDim emps(numOfEmp, 6)
Dim numOfFields
Let numOfFields = 5
ReDim Preserve empsTot(5, numOfEmp, numOfFields)
For i = 0 To numOfEmp
Dim offset As Integer
offset = i + 3
emps(i, 0) = Range("E" & (offset))
emps(i, 1) = Range("F" & (offset))
emps(i, 2) = Range("I" & (offset))
emps(i, 3) = Range("J" & (offset))
emps(i, 4) = Range("K" & (offset))
emps(i, 5) = Range("B" & (offset))
Debug.Print ("^" & emps(i, 5))
Next
Debug.Print ("Week is " & week)
For i = 0 To numOfFields
For j = 0 To numOfEmp
empsTot(1, j, i) = emps(j, i)
Debug.Print (">" & empsTot(1, j, i))
Debug.Print ("i" & CStr(i))
Debug.Print ("j" & CStr(j))
Next
Next
Worksheets("Accessory Sheet").Activate
Dim ColNo
Dim empIndA
empIndA = 0
Dim empIndB
empIndB = 0
For a = 2 To numOfFields + 1
For b = 2 To (numOfEmp + 2)
ColNo = a
CurCol = Split(Cells(, ColNo).Address, "$")(1)
ThisWorkbook.Sheets("Accessory Sheet").Range((CurCol) & CStr(b + 10)).Value = emps(empIndB, empIndA) - ThisWorkbook.Sheets("Accessory Sheet").Range((CurCol) & CStr(b)).Value
empIndB = empIndB + 1
Next
empIndB = 0
empIndA = empIndA + 1
Next
ColNo = 0
For a = 0 To numOfFields
For i = 0 To numOfEmp
ColNo = a + 2
CurCol = Split(Cells(, ColNo).Address, "$")(1)
CurCol = CStr(CurCol)
ThisWorkbook.Sheets("Accessory Sheet").Range((CurCol) & CStr(i + 2)).Value = emps(i, a)
Next
Next
End Sub
Sub retrieveValues()
For i = 0 To 2
For j = 0 To 2
Debug.Print (empsTot(1, 1, 1))
Next
Next
End Sub