Loop thru files in directory and delete worksheet

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
I cannot resove what is wrong with this code. I get a subscript out of range run time error 9.

I would like to set the worksheet (in red), but this is when I get the r/t error 9.

I removed the part in red and replaced later on in the code with the sheet name in red combined with the delete.

I have three sheets in the directory and it works on the first workbook, well at least it deletes the sheet, but I get a privacy warning, "The document contains macros, ActiveX controls, etc."

In the directory I want to exclude workbook 1040.xlsm. Any thoughts?

Code:
Sub Test()
    Dim strPath As String
    Dim strFile As String
    Dim wkb As Workbook
    Dim wks As Worksheet
    On Error Resume Next
    [COLOR="red"]Set wks = Sheets("1040")[/COLOR]    
    On Error GoTo 0

    strPath = ThisWorkbook.Path & Application.PathSeparator
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    strFile = Dir(strPath & "*.xls*")

    Do While Len(strFile) > 0
        If strFile <> "1040.xlsx" Then
            Set wkb = Workbooks.Open(strPath & strFile)
            Application.DisplayAlerts = False
            [COLOR="red"]Sheets("1040")[/COLOR].Delete
            Application.DisplayAlerts = True
            wkb.Close savechanges:=True
            strFile = Dir
        End If
    Loop

    MsgBox "Completed...", vbInformation
    Set wks = Nothing
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You are trying to Set wks = Sheets("1040") before you open the workbooks in the directory. Shouldn't you Set wks after you open each workbook?

Or just try (On error resume next) to delete sheet 1040 after the workbook is open.

Code:
Sub Test()
    Dim strPath As String
    Dim strFile As String
    Dim wkb As Workbook
    Dim wks As Worksheet

    strPath = ThisWorkbook.Path & Application.PathSeparator
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    strFile = Dir(strPath & "*.xls*")

    Do While Len(strFile) > 0
        If strFile <> "1040.xlsx" Then
            Set wkb = Workbooks.Open(strPath & strFile)
            Application.DisplayAlerts = False
[COLOR="Red"]            On Error Resume Next
                wkb.Sheets("1040").Delete
            On Error GoTo 0[/COLOR]
            Application.DisplayAlerts = True
            wkb.Close savechanges:=True
            strFile = Dir
        End If
    Loop

    MsgBox "Completed...", vbInformation
    Set wks = Nothing
End Sub
 
Upvote 0
Ok tried this and it again delete's Sheets("1040") out of the first workbook, but after the first workbook has been opened, sheet deleted, and closed, it now finds workbook 1040.xlsx but does not skip it.

In the folder there are 4 files: 1.xlsx, 2.xlsx, 3.xlsx, 1040.xlsx

I would like to delete Sheets("1040") out of each workbook except for 1040.xlsx which will be the workbook where I run the code from.

I've stepped through the code and can see the first workbook selected is 1.xlsx and after it is finished the code selects 1040.xlsx but just hangs on that workbook and never progresses.
 
Upvote 0
Move strFile = Dir outside of the IF statement.

Code:
Sub Test()
    Dim strPath As String
    Dim strFile As String
    Dim wkb As Workbook

    strPath = ThisWorkbook.Path & Application.PathSeparator
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    strFile = Dir(strPath & "*.xls*")

    Do While Len(strFile) > 0
        If strFile <> "1040.xlsx" Then
            Set wkb = Workbooks.Open(strPath & strFile)
            Application.DisplayAlerts = False
            On Error Resume Next
                wkb.Sheets("1040").Delete
            On Error GoTo 0
            Application.DisplayAlerts = True
            wkb.Close savechanges:=True
        End If
        [COLOR="Red"]strFile = Dir[/COLOR]
    Loop

    MsgBox "Completed...", vbInformation
    Set wks = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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