Split the worksheets in two different Floders

Nithya

New Member
Joined
Sep 20, 2011
Messages
7
Hi ,

I'm new here.
I have a questions that i could not find the answer yet.
I'm also new in macro VBA programming.

I have a xls sheet with 5 tabs, I need to delete all the tabs except the two tabs which are Event tab and Occurrence Tab.( I have a macro which is working fine till this part).i.e it is deleting the remaining three tabs.

Now What I want to do is

1)
The macro should read the source files( I have around 1000 files), the source files will be in C:\Uploaded
2) The macro should delete all the tabs except events and occurrence tab and the macro should place the event tab in c:\event and occurrence tab in c:\occurrence folder,


Below is the code which I am currently using two times in each folder.


Code:
Sub WorksheetDeletionMacro()
    
    Dim strFldrPath As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        On Error GoTo ExitSub
        strFldrPath = .SelectedItems(1)
    End With

    Dim CurrentFile As String: CurrentFile = Dir(strFldrPath & "\" & "*.xls")
    Dim SheetName As String:   SheetName = "Event"
    Dim FailedWorkbooks As String: FailedWorkbooks = vbNullString
    Dim wb As Workbook, ws As Worksheet
    Application.ScreenUpdating = False
    While CurrentFile <> vbNullString
        Set wb = Workbooks.Open(strFldrPath & "\" & CurrentFile)
        If SheetExists(SheetName, wb) Then
            Application.DisplayAlerts = False
            For Each ws In wb.Sheets
                If ws.Name <> SheetName Then ws.Delete
            Next ws
            Application.DisplayAlerts = True
        ElseIf FailedWorkbooks = vbNullString Then
            FailedWorkbooks = wb.Name
        Else
            FailedWorkbooks = FailedWorkbooks & ", " & wb.Name
        End If
        wb.Close True
        CurrentFile = Dir
    Wend
    Application.ScreenUpdating = True
    
    If FailedWorkbooks <> vbNullString Then MsgBox FailedWorkbooks & " did not have a sheet named " & SheetName & "."
    Exit Sub
    
ExitSub:
    Exit Sub
    
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


Please help me with a logic for this.
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

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