Consulting

Results 1 to 2 of 2

Thread: Splitting data into other worksheets based on data in ROW

  1. #1
    VBAX Newbie
    Joined
    May 2018
    Posts
    1
    Location

    Splitting data into other worksheets based on data in ROW

    I've seen lots out there about how to do this based on data in column but not as much on row. I found one example but I don't know enough about VBA to know what to modify - unfortunately, the creator did not include comments.

    My data looks like this:

    Record ID 1 2 3
    First Name John Jane Bob
    Last Name Doe Smith Hope
    Title VP CEO CIO
    Company Doe and Sons XYZ Inc Hope Corp


    i want to have the code iterate through the record ID's and place each one in a separate spreadsheet.

    Below is the code I found, but it seems to me that it's transferring rows not columns. I only understand VBA well enough to get myself in trouble but not enough to get out of it


    Sub parse_data()
    Dim lr As Long
    Dim ws As Worksheet
    Dim vcol, i As Integer
    Dim icol As Long
    Dim myarr As Variant
    Dim title As String
    Dim titlerow As Integer
    vcol = 1
    Set ws = Sheets("Sheet1")
    lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
    title = "A1:C1"
    titlerow = ws.Range(title).Cells(1).Row
    icol = ws.Columns.Count
    ws.Cells(1, icol) = "Unique"
    For i = 2 To lr
    On Error Resume Next
    If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
    ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
    End If
    Next
    myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
    ws.Columns(icol).Clear
    For i = 2 To UBound(myarr)
    ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
    If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
    Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
    Else
    Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
    End If
    ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
    Sheets(myarr(i) & "").Columns.AutoFit
    Next
    ws.AutoFilterMode = False
    ws.Activate
    End Sub
    If it makes any difference... the original data is in a horizontal format (record ID for each row and name/title/company as columns) but I created a new transposed file (as in the example above) because I need the final output to be in this vertical format.

    Thanks in advance for the help!

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test()
        Dim tbl As Range
        Dim ws As Worksheet
        Dim rngF As Range, rngC As Range
        
        Application.ScreenUpdating = False
        
        Set tbl = Worksheets("Sheet1").Cells(1).CurrentRegion
        
        Set ws = Workbooks.Add(xlWBATWorksheet).Sheets(1)
        tbl.Copy
        ws.Cells(1).PasteSpecial Transpose:=True
        Application.CutCopyMode = False
        
        Set rngF = ws.Cells(1).CurrentRegion
        Set rngC = rngF(1).Offset(, rngF.Columns.Count + 2)
    
        rngF.Columns(1).AdvancedFilter xlFilterCopy, , rngC, True
    
        Do While rngC(2).Value <> ""
            rngF.AdvancedFilter xlFilterInPlace, rngC.Resize(2)
            rngF.Copy
            Worksheets.Add(ws).Cells(1).PasteSpecial Transpose:=True
            ActiveSheet.Name = rngC(2).Value
            rngC(2).Delete xlShiftUp
        Loop
    
        Application.DisplayAlerts = False
        ws.Delete
        Application.DisplayAlerts = True
        
    End Sub

Posting Permissions

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