starl
Administrator
- Joined
- Aug 16, 2002
- Messages
- 6,089
- Office Version
- 365
- Platform
- Windows
I'm testing out some logic, so the coding is rather subpar.
I have 2 sheets, each with the same Private sub that just does some simple math using a module level private variable.
In a standard module, I wrote the following test and it worked properly (calling the Private subs and debug.print the results)
Next test - the workbook will have multiple sheets and the Private sub will only be on some sheets. So I change the CodeName of those sheets to start with 'Data' (eg, Data1, Data2).
I then wrote the following:
I added the .Activate because I thought that was the reason this wasn't working properly. What works: it calls and runs the code on the first Data sheet (Data1.TestVariable). But when it loop and runs the send sheet (Data2.TestVariable), I get an error: Cannot run the macro 'Data2.TestVariable'. The macro may not be available....
Obviously it's calling the correct codename and macro and it ran fine with the original codename. If I leave the codename as is and run the first test - it fails. If I change the codename back, it works fine.
I am at a loss as to why I can edit the codename on one sheet, but not the other.
Also tried this
Have confirmed the codename
just fyi, here's one of the private subs
I wanted to keep the various private variables alone in each sheet module. The other way I can do this would be a class holding the variables which is saved to a collection, but I'm afraid it could slow the program down looking things up. Ultimately, when a sheets Calculate_Event is triggered, the sheet uses its variables to do some logic. I have no idea how many sheets I may end up with.
I have 2 sheets, each with the same Private sub that just does some simple math using a module level private variable.
In a standard module, I wrote the following test and it worked properly (calling the Private subs and debug.print the results)
VBA Code:
Sub TestCallPrivateSub()
Dim Progname As String
Progname = ActiveSheet.CodeName & ".TestVariable"
Application.Run Progname
Application.Run "Sheet5.testvariable"
End Sub
Next test - the workbook will have multiple sheets and the Private sub will only be on some sheets. So I change the CodeName of those sheets to start with 'Data' (eg, Data1, Data2).
I then wrote the following:
VBA Code:
Sub TestCallPrivateSub2()
Dim Progname As String
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
If Left(wks.CodeName, 4) = "Data" Then
wks.Activate
Progname = wks.CodeName & ".TestVariable"
Application.Run Progname
End If
Next wks
End Sub
I added the .Activate because I thought that was the reason this wasn't working properly. What works: it calls and runs the code on the first Data sheet (Data1.TestVariable). But when it loop and runs the send sheet (Data2.TestVariable), I get an error: Cannot run the macro 'Data2.TestVariable'. The macro may not be available....
Obviously it's calling the correct codename and macro and it ran fine with the original codename. If I leave the codename as is and run the first test - it fails. If I change the codename back, it works fine.
I am at a loss as to why I can edit the codename on one sheet, but not the other.
Also tried this
Application.Run "data2.testvariable"
and got the same error.Have confirmed the codename
VBA Code:
?activesheet.codename
Data2
just fyi, here's one of the private subs
VBA Code:
Private p_testvariable As Long
Private Sub TestVariable()
p_testvariable = p_testvariable + 1
Debug.Print p_testvariable
End Sub
I wanted to keep the various private variables alone in each sheet module. The other way I can do this would be a class holding the variables which is saved to a collection, but I'm afraid it could slow the program down looking things up. Ultimately, when a sheets Calculate_Event is triggered, the sheet uses its variables to do some logic. I have no idea how many sheets I may end up with.