insomniac53
Board Regular
- Joined
- Sep 3, 2013
- Messages
- 104
When opening more than one workbook in Excel (latest Office 365 version) VBA code does not know which workbook it belongs to. For example, suppose you have two workbooks open and you switch from one to the other, perhaps using ALT-TAB. Any procedures or functions referring to a particular sheet will crash because it's looking in the active workbook, not the one it belongs to.
This is fine until another workbook has been opened at which point the code fails, even if this is the active one on screen. I understand that you could specify a workbook and sheet within a single function, but this isn't feasible when you have many functions using the simple Sheets command above - you would need to alter each one individually.
Could someone tell me if there is a way around this on a global level so that when switching from workbook to workbook, the vba code knows which one it belongs to? Thanks!</code>
Code:
Private Function MyFunction() As integer
Dim w1 As Worksheet: Set w1 = Sheets("name of sheet")
<code>*** code ***
End Function
This is fine until another workbook has been opened at which point the code fails, even if this is the active one on screen. I understand that you could specify a workbook and sheet within a single function, but this isn't feasible when you have many functions using the simple Sheets command above - you would need to alter each one individually.
Could someone tell me if there is a way around this on a global level so that when switching from workbook to workbook, the vba code knows which one it belongs to? Thanks!</code>