Run time error 9 received in running a macro

dellennon

New Member
Joined
May 16, 2011
Messages
20
Hi all, Please help me to troubleshoot to run a macro that is having an error message of "run time error 9, subscription out of range".

Debug is error on this line..

For x = 1 To 3

Application.DisplayAlerts = False
Blank_DBoR_Workbook.Sheets("Sheet" & x).Delete
Application.DisplayAlerts = True

Next



Thanks.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi dellennon,

Welcome to the forum!!

There must be no tab called "Sheet" & x to delete - perhaps because the macro has run once and already deleted the three tabs?

HTH

Robert
 
Upvote 0
Hi Robert,
Thanks for your reply. Sorry for my ignorance since this macro was only given to me by my friend, so how can i fix the problem? erase the command line that causing the problem?
 
Upvote 0
Hi there,

I dare it is working erratically because sometimes there's tabs called 'Sheet1', 'Sheet2' and 'Sheet3' to be deleted, and sometimes there's not - no doubt after the macro has run.

You can use the following that simply suppresses the error message but in no way fixes the issue:

Code:
    For x = 1 To 3
        On Error Resume Next
            Application.DisplayAlerts = False
                Blank_DBoR_Workbook.Sheets("Sheet" & x).Delete
            Application.DisplayAlerts = True
        On Error GoTo 0
    Next

HTH

Robert
 
Upvote 0
Hi there,

I dare it is working erratically because sometimes there's tabs called 'Sheet1', 'Sheet2' and 'Sheet3' to be deleted, and sometimes there's not - no doubt after the macro has run.

You can use the following that simply suppresses the error message but in no way fixes the issue:

Code:
    For x = 1 To 3
        On Error Resume Next
            Application.DisplayAlerts = False
                Blank_DBoR_Workbook.Sheets("Sheet" & x).Delete
            Application.DisplayAlerts = True
        On Error GoTo 0
    Next
HTH

Robert

Thanks again Robert. I did try your solution. The Macro runs smoothly but it did not produce the output file that it supposed to create... What might be the problem if this macro works on others pc/excel but not on mine and some others... We have the same excel version..
 
Upvote 0
What might be the problem

Good question as it worked fine for me :confused:

What exactly do you want the macro to do, and what is being assigned to the 'Blank_DBoR_Workbook' variable?
 
Upvote 0
Good question as it worked fine for me :confused:

What exactly do you want the macro to do, and what is being assigned to the 'Blank_DBoR_Workbook' variable?

The macro will compare two excel tables and the output will be the same format as the two excel but highlighted the mismatched value/s. Blank Dbor Workbook should be the resulting WB. Below is the complete module that the error is occuring..



Private Sub Create_Blank_DBoR()

Dim x As Integer

Set Blank_DBoR_Workbook = Workbooks.Add

'copy the DBoR sheets to a new workbook
For x = UBound(arraySheetSelector) To LBound(arraySheetSelector) Step -1
Comparison_Robot_Workbook.Worksheets(arraySheetSelector(x)).Copy Before:=Blank_DBoR_Workbook.Sheets(1)
Next

'delete empty sheets
For x = 1 To 3

Application.DisplayAlerts = False
Blank_DBoR_Workbook.Sheets("Sheet" & x).Delete
Application.DisplayAlerts = True


Next
 
Upvote 0
Hi dellennon,

It may be that the new workbook being created doesn't actually three blank tabs to start with as this is an Excel setting that can be easily changed per user (PC).

As such, try this which will delete any tab in the new workbook that has no data - not just 'Sheet1', 'Sheet2', or 'Sheet3':

Code:
Private Sub Create_Blank_DBoR()

    Dim x As Integer
    
    Set Blank_DBoR_Workbook = Workbooks.Add
    
    'copy the DBoR sheets to a new workbook
    For x = UBound(arraySheetSelector) To LBound(arraySheetSelector) Step -1
        Comparison_Robot_Workbook.Worksheets(arraySheetSelector(x)).Copy Before:=Blank_DBoR_Workbook.Sheets(1)
    Next
    
    'Delete empty sheets
    For Each Worksheet In Blank_DBoR_Workbook.Sheets
        If WorksheetFunction.CountA(Sheets(Worksheet.Name).Cells) = 0 Then
            Application.DisplayAlerts = False
                Blank_DBoR_Workbook.Sheets(Worksheet.Name).Delete
            Application.DisplayAlerts = True
        End If
    Next Worksheet

End Sub

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top