I have a workbook that is programmed upon opening to open a new copy of a second workbook and save the value of the second book's name in a variable that can be accessed by a private sub routine for an ActiveX control.
In a separate module I have the value:
'variable holding the name of the new workbook
Public sarname As String
In the 'ThisWorkbook' object I have the code:
Private Sub Workbook_Open()
'creates a new workbook and saves name in public variable
Dim SARWorkbook As Workbook
Set SARWorkbook = Workbooks.Add("C:\Users\...xlsm")
Module1.sarname = SARWorkbk.Name
End Sub
Finally, I am using this value in the code:
Private Sub FilBox1_Click()
'If box is checked, a "Yes" string is entered into range object of new workbook with name saved in Module1.sarname
If FilBox1.Value = True Then
Workbooks(Module1.sarname).Worksheets("COC Form").Range("b46").Value = "Yes"
Else
Workbooks(Module1.sarname).Worksheets("COC Form").Range("b46").Value = "No"
End If
End Sub
The ActiveX control after responding to its status, is supposed place a "yes" or "no" answer into a cell in the new workbook using the name stored in "sarname". After doing some investigating with MsgBox
, I know that the value is stored in the variable but the private sub routine only sees it as "", and I get a run-time error 13.
I don't have this issue with other private subs that reference public variables, so I'm not sure where this is going wrong.
Taking advice from an answer to a similar question, I took different approach and I wrote in my module:
Public Property Get Somesarname() As String
Somesarname = sarname
End Property
I then used 'somesarname' in my FilBox1 sub instead. That gets me a 'subscript out of range' error and 'somesarname' still showing up as " " when I hover the cursor over it.
I'm totally stumped now. What basic thing am I missing here?