Results 1 to 4 of 4

Thread: VBA Help splitting data based on a ID #

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    Something like this should get you started


    Option Explicit
    
    
    Sub SplitID()
        Dim i As Long
        Dim r As Range
        Dim C As Collection
        Dim ws As Worksheet
        Dim wb As Workbook
        Dim ID As String, W As String
        
        Application.ScreenUpdating = False
        
        Set r = ActiveSheet.Cells(1, 1).CurrentRegion
    
        Set C = New Collection
        
        'make unique list
        On Error Resume Next
        For i = 2 To r.Rows.Count
            C.Add r.Cells(i, 1).Value, CStr(r.Cells(i, 1).Value)
        Next i
        On Error GoTo 0
    
    
        For i = 1 To C.Count
        
            'get the ID
            ID = C.Item(i)
            
            'delete ws with that ID just in case
            On Error Resume Next
            Application.DisplayAlerts = False
            Worksheets(ID).Delete
            Application.DisplayAlerts = True
            On Error GoTo 0
            
            'add ws named ID
            Worksheets.Add
            Set ws = ActiveSheet
            ws.Name = ID
            
            'filter data on ID
            r.AutoFilter Field:=1, Criteria1:=ID
            
            'copy to ID ws
            r.SpecialCells(xlCellTypeVisible).Copy ws.Range("A1")
    
    
            'get wb name
            W = ThisWorkbook.Path & Application.PathSeparator & ID & ".xlsx"
            
            'delete wb with that ID just in case
            On Error Resume Next
            Application.DisplayAlerts = False
            Kill W
            Application.DisplayAlerts = True
            On Error GoTo 0
            
            'copy ws to new wb
            ws.Move
            Set wb = ActiveWorkbook
            wb.SaveAs W, xlWorkbookDefault
            wb.Close False
            
            ThisWorkbook.Activate
        Next i
    
    
        ActiveSheet.AutoFilterMode = False
    
    
        Application.ScreenUpdating = False
    
    
        
        MsgBox "Done"
    
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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