I have a .xlsm that has data feeds pushed into a specific workbook/sheet and uses Worksheet_Change to perform various actions as the data changes.
So my VBA has to be very specific about the workbook/sheet references as the active worksheet may be another in the workbook or even a different workbook entirely.
The following is a test piece that demonstrates the problem (it clears the texts of some dummy form buttons).
Rather than protecting the worksheet, I assumed that by setting the '(Name)' property (aka CodeName) it would protect the code from changes to the display worksheet name 'Name' (which the user can change from the tab):
In VBA Project>Modules>Module1 I have these declarations:
In VBA Project>Microsoft Excel Objects>ThisWorkbook I have this code:
On opening the workbook I get a 'Run-time error '9' Subscript out of range' on the Set ws line; if I change it to:
.... then when I open the workbook again, all is well.
Any ideas?
So my VBA has to be very specific about the workbook/sheet references as the active worksheet may be another in the workbook or even a different workbook entirely.
The following is a test piece that demonstrates the problem (it clears the texts of some dummy form buttons).
Rather than protecting the worksheet, I assumed that by setting the '(Name)' property (aka CodeName) it would protect the code from changes to the display worksheet name 'Name' (which the user can change from the tab):
In VBA Project>Modules>Module1 I have these declarations:
VBA Code:
Option Explicit
Public wb As Workbook
Public ws As Worksheet
In VBA Project>Microsoft Excel Objects>ThisWorkbook I have this code:
VBA Code:
Private Sub Workbook_Open()
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Interface")
ws.Buttons.Text = ""
End Sub
On opening the workbook I get a 'Run-time error '9' Subscript out of range' on the Set ws line; if I change it to:
VBA Code:
Set ws = wb.Worksheets("Sheet1")
Any ideas?