+ Reply to Thread
Results 1 to 5 of 5

Split worksheets in multiple workbooks in folder

Hybrid View

  1. #1
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Split worksheets in multiple workbooks in folder

    I have about 400 Excel workbooks in a folder, all of which have 1 sheet of data. I have been searching for the VB code which will split the worksheet by column Y into separate sheets. I have found the code which will perform this as well as code which is intended to run the same code on ALL workbooks in a folder. I've tried putting the two sets of code together, but it keeps running the code for the active sheet only. How can I get this to run on all workbooks in a folder?

    
    Sub RunCodeOnAllXLSFiles()
    Dim lCount As Long
    Dim wbResults As Workbook
    Dim wbCodeBook As Workbook
    Dim lastrow As Long, LastCol As Integer, i As Long, iStart As Long, iEnd As Long
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    
    On Error Resume Next
        Set wbCodeBook = ThisWorkbook
            With Application.FileSearch
                .NewSearch
                'Change path to suit
                .LookIn = "C:\files1\files2\Files"
                .FileType = msoFileTypeExcelWorkbooks
                'Optional filter with wildcard
                '.Filename = "Book*.xls"
                    If .Execute > 0 Then 'Workbooks in folder
                        For lCount = 1 To .FoundFiles.Count 'Loop through all
                            'Open Workbook x and Set a Workbook variable to it
                            Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
        
        'here is the code I am running on all workbooks
    
    
    Application.ScreenUpdating = False
        lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        .Range(.Cells(2, 1), Cells(lastrow, LastCol)).Sort Key1:=Range("Y2"), Order1:=xlAscending, _
            Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        iStart = 2
        For i = 2 To lastrow
            If .Range("Y" & i).Value <> .Range("Y" & i + 1).Value Then
                iEnd = i
                Sheets.Add after:=Sheets(Sheets.Count)
                Set ws = ActiveSheet
                On Error Resume Next
           'providing a sheet name with the word LIST in front of value
                ws.Name = "LIST " & .Range("Y" & iStart).Value
                On Error GoTo 0
                ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
                .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy Destination:=ws.Range("A2")
                iStart = iEnd + 1
            End If
        Next i
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
    'Deletes the original sheet and the additional 2 default sheets
    
    Application.DisplayAlerts = False
    Sheets("Sheet1").Select
    ActiveWindow.SelectedSheets.Delete
    
    Application.DisplayAlerts = False
    Sheets("Sheet2").Select
    ActiveWindow.SelectedSheets.Delete
    
    Application.DisplayAlerts = False
    Sheets("Sheet3").Select
    ActiveWindow.SelectedSheets.Delete
    
    
    
                            wbResults.Close SaveChanges:=False
                        Next lCount
                    End If
            End With
    On Error GoTo 0
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    End Sub
    Last edited by Eric09; 09-07-2012 at 10:05 AM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Split worksheets in multiple workbooks in folder

    Quote Originally Posted by Eric09 View Post
    I have about 400 Excel workbooks in a folder, all of which have 1 sheet of data. I have been searching for the VB code which will split the worksheet by column Y into separate sheets.
    Can you explain better ? do you want extract each column save as new sheet ?
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Split worksheets in multiple workbooks in folder

    I want to split all 400 workbooks by the value in column Y, 1 sheet for each value. I got this to work for one workbook with this code:

    Application.ScreenUpdating = False
        lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        .Range(.Cells(2, 1), Cells(lastrow, LastCol)).Sort Key1:=Range("Y2"), Order1:=xlAscending, _
            Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        iStart = 2
        For i = 2 To lastrow
            If .Range("Y" & i).Value <> .Range("Y" & i + 1).Value Then
                iEnd = i
                Sheets.Add after:=Sheets(Sheets.Count)
                Set ws = ActiveSheet
                On Error Resume Next
           'providing a sheet name with the word LIST in front of value
                ws.Name = "LIST " & .Range("Y" & iStart).Value
                On Error GoTo 0
                ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
                .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy Destination:=ws.Range("A2")
                iStart = iEnd + 1
            End If
        Next i
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    I was able to delete the original sheet and the other two default sheets, so that I would end up with only the sheets produced with the code above. Again, I want to loop this function through all 400 as well.

    Application.DisplayAlerts = False
    Sheets("Sheet1").Select
    ActiveWindow.SelectedSheets.Delete
    
    Application.DisplayAlerts = False
    Sheets("Sheet2").Select
    ActiveWindow.SelectedSheets.Delete
    
    Application.DisplayAlerts = False
    Sheets("Sheet3").Select
    ActiveWindow.SelectedSheets.Delete
    Now I want to it to loop through 400 workbooks in a folder, and perform these two functions. All workbooks have the same columns and structure, just different names and data.

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Split worksheets in multiple workbooks in folder

    to open all files in a folder and change them
    Sub OpenfileUpdate()
    Dim strFile As String
    mFolder = "D:\DATA\test\"
    strFile = Dir(mFolder & "*.xls*")
    Do While strFile <> ""
        Workbooks.Open mFolder & strFile
    ' <<<<<<<<<< insert your code 
        ActiveWorkbook.Close True
        strFile = Dir
    Loop
    End Sub

  5. #5
    Registered User
    Join Date
    09-07-2012
    Location
    VA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Split worksheets in multiple workbooks in folder

    That did it. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1