Consulting

Results 1 to 9 of 9

Thread: Transpose Data In Columns

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    Transpose Data In Columns

    Hi , I have VBA that do the Data on Column A that they Separate by | and Transport to next column with the same data in B Column . and it is working fine now just need also repeat till F column. Please check the below image you will understand better .

    Question.jpg

    Sub blah()
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    SceVals = Range(Cells(1), Cells(lr, 2)).Value
    For i = 1 To lr
      ExtraRowsNeeded = ExtraRowsNeeded + (Len(SceVals(i, 1)) - Len(Replace(SceVals(i, 1), "|", "", 1, , vbTextCompare)))
    Next i
    ReDim myresults(1 To lr + ExtraRowsNeeded, 1 To 2)
    DestRow = 0
    For i = 1 To lr
      x = Split(SceVals(i, 1), "|")
      For j = 0 To UBound(x)
        DestRow = DestRow + 1
        myresults(DestRow, 1) = x(j)
        myresults(DestRow, 2) = SceVals(i, 2)
      Next j
    Next i
    Sheets("Sheet2").Cells(1).Resize(UBound(myresults), 2).Value = myresults
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Sub M_snb()
      sn=sheet1.cells(4,1).currentregion
    
      with createobject("scripting.dictionary")
        for j=2 to ubound(sn)
          st=split(sn(j,1),"|")
          for jj=0 to ubound(jj)
            .item(.count)=array(st(jj),sn(j,2),sn(j,3),sn(j,4))
          next
    
          sheet1.cells(1,10).resize(.count)=application.index(.items,0,0)
       next
      end with
    End Sub
    Last edited by snb; 02-19-2021 at 03:55 AM.

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Dead easy in Power Query:
    let
        Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
        SplitColumnbyDelimiter = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Hdr1", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Hdr1")
    in
        SplitColumnbyDelimiter
    2021-02-19_103954.jpg

    …but in the same vein as before:
    Sub blah2()
    myWidth = 4 'adjust
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    SceVals = Range(Cells(1), Cells(lr, myWidth)).Value
    For i = 1 To lr
      ExtraRowsNeeded = ExtraRowsNeeded + (Len(SceVals(i, 1)) - Len(Replace(SceVals(i, 1), "|", "", 1, , vbTextCompare)))
    Next i
    ReDim myresults(1 To lr + ExtraRowsNeeded, 1 To myWidth)
    DestRow = 0
    For i = 1 To lr
      x = Split(SceVals(i, 1), "|")
      For j = 0 To UBound(x)
        DestRow = DestRow + 1
        myresults(DestRow, 1) = x(j)
        For k = 2 To myWidth
          myresults(DestRow, k) = SceVals(i, k)
        Next k
      Next j
    Next i
    Sheets("Sheet2").Cells(1).Resize(UBound(myresults), myWidth).Value = myresults
    End Sub
    but note that your picture shows data to column D but your narrative suggests to column F; adjust the line myWidth = 4 to accommodate.
    Last edited by p45cal; 02-19-2021 at 04:03 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thanks for your help by i need result in sheet 2 i attached the Excel File . could you please check it .
    Attached Files Attached Files

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Where did you get the first Code from ?

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Button on Sheet1 at cell F1 uses the macro blah2 to populate Sheet2
    Right click on the table in Sheet3 and choose Refresh to update the query which uses the data from Sheet1
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Really Good Job Man , You saved me !

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    ...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Quote Originally Posted by snb View Post
    Where did you get the first Code from ?
    http://www.vbaexpress.com/forum/show...ata-In-Columns

    OP's another thread.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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