dear Seemanto,
thank you for the explanation, for your information the error always refer to Set sh1 = Worksheets ("DATABASE"), is there any miss type in my code?
regards,
bigMe
Hello
bigme,
Thanks for your feedback. From the screenshots you provided in the first post, I understand that you want to add new sheets and add data to specific sheets in a Master.xlsx file from a .xlsm file. You wrote the necessary code in a Private Subroutine and then called it within another Subroutine.
I wrote a similar code in a
.xlsm file.
Code:
Private Sub test1()
Dim sheetname As String
sheetname = InputBox("Enter sheet name")
With Workbooks("Masterfile.xlsx")
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = sheetname
Set sh1 = Worksheets("Test 1")
Set sh2 = Worksheets("New Code")
sh1.Range("A1") = "Movie"
sh2.Range("A1") = "Budget"
End With
sh1.Range("A2") = "Actor"
sh2.Range("A2") = "Director"
End Sub
Sub newcode()
Call test1
End Sub
After Running this code from Excel’s View tab, it showed the “Subscript out of range” error in the following line.
Code:
Set sh1 = Worksheets("Test 1")
But when I Ran this code from the Visual Basic Editor window, sometimes it worked properly and sometimes showed an error in the same line. These are the same things you encountered. However, I noticed another thing. When I Ran this code from the .xlsx file’s View tab, it worked properly.
The reason behind encountering this error is related to the way Excel handles the scope and accessibility of worksheets between different workbooks when you're running macros from different workbooks.
When you Run a macro from the View tab in Excel, it considers the active workbook as the context for executing the code. That means the .xlsm file where you wrote the code, becomes the active workbook, and it doesn't have direct access to worksheets in another workbook like "Master.xlsx" without some specific qualifications. It tries to find the DATABASE sheet within the .xlsm file (whereas the code should search the sheet within the Master.xlsx file) and returns a Subscript out of range error as the DATABASE sheet is not available in the .xlsm file.
Note: if you had a sheet named DATABASE in your .xlsm file, then the code would show an error in the next line (Set sh2 = Worksheets(“Memo Gudang”)) as the Memo Gudang sheet is not available within the .xlsm file.
But if I Run this code from the
Master.xlsx file's
View tab, it works properly as it is now the active workbook.
To fix this issue and ensure that your code can Run successfully, you can modify your code to explicitly reference the worksheets within "Master.xlsx" by using the Worksheets collection of the specific workbook.
Here is the modified code for my previous code.
Code:
Private Sub test1()
Dim sheetname As String
sheetname = InputBox("Enter sheet name")
Dim masterfile As Workbook
Set masterfile = Workbooks("Masterfile.xlsx")
With masterfile
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = sheetname
Set sh1 = .Worksheets("Test 1")
Set sh2 = .Worksheets("New Code")
sh1.Range("A1") = "Movie"
sh2.Range("A1") = "Budget"
End With
sh1.Range("A2") = "Actor"
sh2.Range("A2") = "Director"
End Sub
Sub newcode()
Call test1
End Sub
In this modified code, I explicitly set a reference to the "Masterfile.xlsx" workbook as masterfile, and then we use this reference to access worksheets and ranges within the Masterfile.xlsx workbook. This way, the code will work regardless of which workbook is currently active.
Similar to this, you should also set the reference of the
Master.xlsx workbook explicitly.
Hopefully, I was able to provide you with a clear idea of why the error occurred and how you can fix it. Let us know your feedback.
Regards,
Seemanto Saha
ExcelDemy