Macro Execution Error

Nithya

New Member
Joined
Sep 20, 2011
Messages
7
Hi ,

I am using the below macro for deleting all the worksheets except two worksheets(i.e sheet1 and sheet2) from a folder which has a dump of xls files.

When I am executing this macro.., the macro is prompting to select the files manually, which i don't want to do the macro has to automatically pick all the xls from the folder and delete the unnecessary tabs.

Can any one please tell me where is the error in the below code.

Code:
Sub Delete_Trans()
    
    Dim strFldrPath As String
    
    strFldrPath = "D:\Trans"

    Dim CurrentFile As String
    Dim wb As Workbook, ws As Worksheet
    Application.ScreenUpdating = False

    CurrentFile = Dir(strFldrPath & "\" & "*.xls")

    While CurrentFile <> vbNullString
        Set wb = Workbooks.Open(strFldrPath & "\" & CurrentFile)
        If SheetExists("sheet1", wb) And SheetExists("sheet2", wb) Then
              Application.DisplayAlerts = False
          For Each ws In wb.Sheets
              If Not (ws.Name = "sheet1" Or ws.Name = "sheet2") Then
                ws.Delete
              End If
          Next ws
           Application.DisplayAlerts = True
          End If
        wb.Close True
        CurrentFile = Dir
    Wend

    Application.ScreenUpdating = True
    
End Sub


Private Function SheetExists(SheetName As String, wb As Workbook) As Boolean
    
    Dim wsCheck As Worksheet
    On Error GoTo NotFound
    Set wsCheck = wb.Sheets(SheetName)
    SheetExists = True
    Exit Function
    
NotFound:
    SheetExists = False
    
End Function
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi. Try this to open the xls files.
Code:
Sub OpenAllXls()
Dim path As Variant
Dim excelfile As Variant
        path = "H:\folder\"
            excelfile = Dir("*.xls")
        ChDir "F:\folder\"
    Do While excelfile <> ""
    Workbooks.Open Filename:=path & excelfile 'w/o password or
    'Workbooks.Open Filename:=path & excelfile, Password:="password" 'all files with same pwd
    excelfile = Dir
'your code to delete sheets
    Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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