By: Shamima Sultana

In reply to JaneM.

Hello JaneM,

“Subscript Out of Range Error” occurs when trying to reference a worksheet that does not exist. In your case, ensure that the names of the sheets you are trying to access match exactly with what is in the workbook. To solve this issue you can use the following VBA code.

The code will dynamically checks for the existence of the sheets Sheet1(2) to Sheet1(11) using a loop. It will copy the entire rows based on the last used row in each sheet instead of using UsedRange, which may cause issues if you have formatting or other non-data cells.
The code creates a single new worksheet named MergedData and does not insert any blank sheets.

Copy-Paste the VBA code:

Sub CombineSheets()
Dim ws As Worksheet
Dim MergedSheet As Worksheet
Dim LastRow As Long
Dim MergedRow As Long
Dim i As Integer

' Create a new worksheet for the merged data
Set MergedSheet = ThisWorkbook.Worksheets.Add
MergedSheet.Name = "MergedData"

' Start merging from the first row
MergedRow = 1

' Loop through each worksheet
For i = 1 To 11 ' Assuming Sheet1, Sheet1(2), ... , Sheet1(11)
On Error Resume Next ' Ignore errors temporarily
Set ws = ThisWorkbook.Worksheets("Sheet1(" & i & ")")
On Error GoTo 0 ' Turn error handling back on

If Not ws Is Nothing Then ' Check if the worksheet exists
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' Find the last used row in the current sheet

' Copy the used range to the merged sheet
ws.Range("A1:A" & LastRow).EntireRow.Copy Destination:=MergedSheet.Range("A" & MergedRow)

' Update the merged row for the next paste
MergedRow = MergedRow + LastRow
End If

' Reset ws for the next iteration
Set ws = Nothing
Next i

MsgBox "Sheets combined successfully!"
End Sub

Regards
ExcelDemy