TotallyConfused
Board Regular
- Joined
- May 4, 2017
- Messages
- 247
- Office Version
- 365
- Platform
- Windows
Hello everyone.
It has been my understanding that if we referred to a sheet by its CodeName rather than its SheetTab name, then if someone renamed the sheet, it would not cause VBA to fail. With that thought in mind, I have the following code. The purpose of this macro is to open a second workbook and define the sheets in this workbook and also the second workbook. Ultimately data from sheet1 of this workbook will be copied into sheet1 of the second workbook, called 'MasterDataFile'. As this macro is written, it works great. My problem is that if I rename Sheet1 of either workbook, I then get the error message of 'Error 9, subscript out of range'. I have highlighted in red the section of code that seems to be the problem. (Note: it also includes the colon) I have tried removing the word 'ThisWorkbook' and the following period, but I still get the same error message if Sheet1 is renamed.
As a passing thought, I'd love to know if it is possible to copy a sheet from one workbook into a second workbook without opening workbook #2, and if so how, but that's a problem for another day.
If anyone can show me what I've done wrong to cause that error message, I'd sure appreciate it. THANK YOU in advance for any help or suggestions.
TotallyConfused
It has been my understanding that if we referred to a sheet by its CodeName rather than its SheetTab name, then if someone renamed the sheet, it would not cause VBA to fail. With that thought in mind, I have the following code. The purpose of this macro is to open a second workbook and define the sheets in this workbook and also the second workbook. Ultimately data from sheet1 of this workbook will be copied into sheet1 of the second workbook, called 'MasterDataFile'. As this macro is written, it works great. My problem is that if I rename Sheet1 of either workbook, I then get the error message of 'Error 9, subscript out of range'. I have highlighted in red the section of code that seems to be the problem. (Note: it also includes the colon) I have tried removing the word 'ThisWorkbook' and the following period, but I still get the same error message if Sheet1 is renamed.
As a passing thought, I'd love to know if it is possible to copy a sheet from one workbook into a second workbook without opening workbook #2, and if so how, but that's a problem for another day.
Rich (BB code):
Sub Open2WorkbookAndDefineSheets()
Application.ScreenUpdating = False
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
Dim Wb2 As Workbook
Dim Wb2ws1 As Worksheet
Workbooks.Open Filename:="MasterDataFile.xlsm", UpdateLinks:=0 ' Do not update links
Set Wb2 = Workbooks("MasterDataFile.xlsm")
Set Wb2ws1 = Wb2.Sheets("Sheet1")
Application.ScreenUpdating = True
End Sub
If anyone can show me what I've done wrong to cause that error message, I'd sure appreciate it. THANK YOU in advance for any help or suggestions.
TotallyConfused