Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 24

Thread: Macro to split data based on the value from specific column

  1. #1

    Macro to split data based on the value from specific column

    Hello VBA gurus,

    I am looking for a macro to split the data based on the value from the specific column.

    I have attached my excel file and there is a tab called "Requirement" for more info what I would like to have.

    Your help is highly appreciated and thank you in advance.

    Best regards,
    Ratna
    Attached Files Attached Files

  2. #2
    i created another sheet (job) and add button to split data in "data" sheet.
    Attached Files Attached Files

  3. #3
    Thank you arnelgp, I wanted to create separate workbook (not worksheet) and save into to C:\WIP\GLAD into the folder that has created based on the value from Column H and needs to save into respective folder based on the file created.
    Folder I have is as per below (just for example)
    601
    604
    606
    610 etc

  4. #4
    I also noticed that when I copy macro into original file, it doesn't work. In original file there are few more columns not only A:T

    Also I want to lookup Date field instead column L in order to get the file name.

    Thank you and I appreciate for your help.

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    If you are on office 365 then perhaps:
    Sub test()    
        Dim unq As Variant
        Dim tbl As Object, x As Long
        Dim lc As ListColumn
        Dim wbTmp As Workbook, wsTmp As Worksheet
        
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        
        Set tbl = Sheet1.ListObjects(1)
        Set lc = tbl.ListColumns("CostCenter")
        With tbl
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=lc.DataBodyRange, Order:=xlAscending
            .Sort.Apply
        End With
        unq = Application.Unique(lc.DataBodyRange, False, False)
        
        For x = 1 To UBound(unq)
            tbl.Range.AutoFilter lc.Index, unq(x, 1)
            Set wbTmp = Workbooks.Add
            Set wsTmp = wbTmp.Sheets(1)
            tbl.Range.SpecialCells(xlVisible).Copy
            With wsTmp
                .Range("A1").PasteSpecial xlPasteValues
                .Range("A1").PasteSpecial xlPasteFormats
                .Cells.EntireColumn.AutoFit
            End With
            Application.CutCopyMode = False
            With wbTmp
                .SaveAs "C:\WIP\GLAD\" & unq(x, 1) & Format(Date, "MMMYYYY"), 51
                .Close False
            End With
        Next x
        
        With Application
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
        End With
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    Thank you georgiboy, It runs on the file I attached. But I tried the macro into my original file but got debug on below line. In my original file data contain through A to U column, I hope this won't be an issue. I wanted to get the file name from the Date column not just the current month.

    Set tbl = Sheet1.ListObjects(1)

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Maybe try the attachment:

    The folder 'C:\WIP\GLAD' should already exist
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  8. #8
    here check it again.
    Attached Files Attached Files

  9. #9
    final revision.
    Attached Files Attached Files

  10. #10
    Thank you, looks like it works but there is one problem. File name for new workbook is picking from Column A. It should be picked from "CostCenter" field.

  11. #11
    It only creates a folder but there is no file inside respective folder.

  12. #12
    here is another try.
    now looking at "CostCenter" column for the cost center.
    Attached Files Attached Files

  13. #13
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    How often does Columns A & H differ?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  14. #14
    In original data, Column H has numeric info but column A has Alfa numeric data.

  15. #15
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Quote Originally Posted by Ratna Chhetr View Post
    In original data, Column H has numeric info but column A has Alfa numeric data.
    However in your sample file of over 23,000 rows there is no Alpha Numeric data in Column A, its all Numeric. The responses provided were based on the sample file provided.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  16. #16
    In my original file there are alfa numeric value with over million rows. That's why wanted to split based on the value from Column H.

  17. #17
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Did you get errors from post 7?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  18. #18
    I tried in my original file, it only creates a folder (not all the time) and file with Row A only. I couldn't get entire data.

  19. #19
    post again your "real" data or at least have same table format at the original.

  20. #20
    Here you go,
    All the blank columns have the data in original file. Original data has over million rows. I am unable to upload due to file size limitation. I wanted to split by Column G.
    Attached Files Attached Files
    Last edited by Ratna Chhetr; 07-31-2022 at 09:56 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
  •