An implementation of Visual Basic that is built into Microsoft products.
Hi @sachin kumar ,
Run-time error 9, “Subscript out of range,” means that the macro is trying to access an item that does not exist, commonly:
- An incorrect workbook name:
Workbooks("Report.xlsx") - An incorrect worksheet name:
ThisWorkbook.Worksheets("Data") - A worksheet index greater than the number of available sheets:
ThisWorkbook.Worksheets(5) - An array index outside its valid range.
When the error appears, click Debug and check the highlighted line. Verify workbook and worksheet names carefully, including spaces and file extensions.
For arrays, check the valid indexes with:
Debug.Print LBound(MyArray), UBound(MyArray)
You can also display all worksheet names with:
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Debug.Print "[" & ws.Name & "]"
Next ws
The screenshot mentioned in your question does not appear to be attached. Please provide:
- The screenshot of the error.
- The complete macro code.
- The line highlighted after clicking Debug.
Please remove any confidential information before posting. The highlighted line is needed to identify the exact invalid workbook, worksheet, collection, or array index.
If this explanation and the diagnostic steps were helpful, I would appreciate it if you could follow the instructions here, so that others experiencing similar behavior can benefit from the answer as well.