Results 1 to 10 of 18

Thread: 10$ For VBA Code Split Data To Multiple Workbook Based On Unique Values In A Column

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    I get 43 xlsb files as output
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Member
    Join Date
    Dec 2012
    Posts
    43
    Rep Power
    0
    Capture.JPG
    i restarted and tried again. still get the same error. also the runtime error


    do i need to add any reference from the object library?



    Code:
    Sub SplitWorkbook()
    
        Dim colLetter As String, SavePath As String
        Dim lastValue As String
        Dim wb As Workbook
        Dim lng As Long
        Dim currentRow As Long
        colLetter = "P"
        SavePath = "" 'Indicate the path to save
        If SavePath = "" Then SavePath = ThisWorkbook.Path
        'Sort the workbook.
        With ThisWorkbook.Worksheets(1)
            .Sort.SortFields.Add Key:=.Range(colLetter & ":" & colLetter), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With .Sort
                .SetRange Cells
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            For lng = 2 To .Range(colLetter & .Rows.Count).End(xlUp).Row
                If .Cells(lng, colLetter).Value = "" Then Exit For
                lastValue = .Cells(lng, colLetter).Value
                .Cells.AutoFilter field:=.Cells(lng, colLetter).Column, Criteria1:=lastValue
                lng = .Cells(.Rows.Count, colLetter).End(xlUp).Row
                Set wb = Application.Workbooks.Add(xlWorksheet)
                .Rows(1 & ":" & lng).Copy wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp)
                wb.SaveAs SavePath & "\" & lastValue, 50
                wb.Close
            Next
            .AutoFilterMode = False
        End With
        
    End Sub
    Quote Originally Posted by Excel Fox View Post
    I get 43 xlsb files as output

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    43
    Rep Power
    0
    i found the problem. it is becuase the source workbook file format is .xlsb and once i changed it to .xls then it worked.

    all of my source workbooks are originally xlsb is there anyway that the code will work in xlsb source workbook?

  4. #4
    Member
    Join Date
    Dec 2012
    Posts
    43
    Rep Power
    0
    i also checked with other file formats it does not work in xlsm as well. it only works in .xls seems like the code only functions in old version of excel only.

  5. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    It's more to do with your available system resources, than anything else I think. Can you try the same macro on a more powerful computer?
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  6. #6
    Member
    Join Date
    Dec 2012
    Posts
    43
    Rep Power
    0
    the modified code you provided, worked perfectly.

    now only one issue that all the generated workbooks have the default worksheet name as "sheet1" i want the worksheet name to be the same as the source worksheet which is "GL_Details" for all those generated workbooks the worksheet name should be "GL_Details"

    can you please help on this part of the code?

    Quote Originally Posted by Excel Fox View Post
    It's more to do with your available system resources, than anything else I think. Can you try the same macro on a more powerful computer?

  7. #7
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    IT's working fine for me in the binary format (xlsb).

    I've made a small modification. try this

    Code:
    Sub SplitWorkbook()
    
        Dim colLetter As String, SavePath As String
        Dim lastValue As String
        Dim wb As Workbook
        Dim lng As Long
        Dim currentRow As Long
        colLetter = "P"
        SavePath = "" 'Indicate the path to save
        If SavePath = "" Then SavePath = ThisWorkbook.Path
        'Sort the workbook.
        With ThisWorkbook.Worksheets(1)
            .Sort.SortFields.Add Key:=.Range(colLetter & ":" & colLetter), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With .Sort
                .SetRange .Parent.UsedRange.Cells
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            For lng = 2 To .Range(colLetter & .Rows.Count).End(xlUp).Row
                If .Cells(lng, colLetter).Value = "" Then Exit For
                lastValue = .Cells(lng, colLetter).Value
                .Cells.AutoFilter field:=.Cells(lng, colLetter).Column, Criteria1:=lastValue
                lng = .Cells(.Rows.Count, colLetter).End(xlUp).Row
                Set wb = Application.Workbooks.Add(xlWorksheet)
                .Rows(1 & ":" & lng).Copy wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp)
                wb.SaveAs SavePath & "\" & lastValue, 50
                wb.Close
            Next
            .AutoFilterMode = False
        End With
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Replies: 10
    Last Post: 05-23-2013, 12:30 PM
  2. Replies: 4
    Last Post: 05-01-2013, 09:49 PM
  3. Replies: 2
    Last Post: 04-14-2013, 09:15 PM
  4. Replies: 2
    Last Post: 03-05-2013, 07:34 AM
  5. Group Pivot Data Based On Row Values In One Column
    By mrmmickle1 in forum Excel Help
    Replies: 10
    Last Post: 10-09-2012, 11:46 PM

Posting Permissions

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