This is what I am trying:
- In module, declare module-scope variables for worksheet object;
- In
ThisWorkbookobject, set the variables when workbook opens.
The code for each:
Module 1:
Option Explicit
Public ws As Worksheet
Function func()
dim rng as Range
set rng = ws.Range("namedRange")
'rest of code ...
end function
ThisWorkbook:
Private Sub Workbook_Open()
Set ws = Worksheets("Sales")
End Sub
When I open the workbook, the first run throws an error:
Method 'Range' of object '_Worksheet' failed
and all times after that, the error is:
Object variable or With block variable not set
setrng = ws.Range...- KekuSemauWorkbook_Openevent? If yes, I see no reason for it not to work. - L42