Consulting

Results 1 to 15 of 15

Thread: Need help with restructuring Macro Columns to rows, Please!!!

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location

    Need help with restructuring Macro Columns to rows, Please!!!

    I have data that is in column C that is numeric with dashes. I need the macro Columns to rows to be able to take this data that is found in the cell and move it to the Row.
    Currently the macro on works with just numeric data, but not numeric data with "-" in between the numerica data.
    So I need the macro to be able to do what it suppossed to as it does now but be able to work with the data in column C you ll find on the excell sheet that I attach.

    Thank you very much!!! in advance

    Sincerely Dennis


    Sub movetocolumns()Dim i As Integer, iRow As Integer
    Dim arrSource As Variant
    
    
    'Set the first row
    iRow = 1
    
    
    With ActiveWorkbook.Worksheets("Sheet1")
        'get the data into an array from the first column
        arrSource = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    
    
        'parse every value of the array and add the data to the next column
        For i = 1 To (UBound(arrSource) - UBound(arrSource) Mod 3) Step 3
            .Cells(iRow, 2) = arrSource(i, 1)
            .Cells(iRow, 3) = arrSource(i + 1, 1)
            .Cells(iRow, 4) = arrSource(i + 2, 1)
            iRow = iRow + 1
        Next i
        'add the remaining values
        Select Case UBound(arrSource) Mod 3
            Case 1  'one item to add
                .Cells(iRow, 2) = arrSource(i, 1)
            Case 2  'still two items to add
                .Cells(iRow, 2) = arrSource(i, 1)
                .Cells(iRow, 3) = arrSource(i + 1, 1)
            Case Else   'nothing to add
        End Select
    End With
    End Sub
    Attached Files Attached Files

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you not do this with Data/Text to columns?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    I Prefer the macro to do this as it does work great and fast, please
    Thank you
    Attached Files Attached Files
    Last edited by estatefinds; 07-14-2017 at 07:46 AM.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
      Range("C1").CurrentRegion.TextToColumns Destination:=Range("C1"), DataType:=xlDelimited, OtherChar:="-"
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    i ran what you had posted and nothing happends?

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try the unedited version
    Sub Macro4()
        Range("C1").CurrentRegion.TextToColumns Destination:=Range("C1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
            :="-", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
            1)), TrailingMinusNumbers:=True
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    One problem is that Column A has no data, so the array is empty

      arrSource = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    Another is that it is not very clear what you'd like the result to be formatted as

    Please add an 'After' worksheet to your attachment
    ---------------------------------------------------------------------------------------------------------------------

    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

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Paul
    He asks for the code to be applied to Column C
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    that unmerges,
    All i need is the data in the column C moved to rows. In post 3 the example shows what it looks like disregrd the hight lighted red.
    so the data needs to be moved from column to rows with out altering the data that is in column C

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Just change the destination to E1. I can't follow what the Post 3 example is intended to show; there seems no relation between column C and thae other data.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    sorry i forgot to add that data to worksheet, here it is Thankyou
    Attached Files Attached Files

  12. #12
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    ok im sending file of what is suppossed to look like. the data will be moved from column C to rows starting a E1 to AL1 then resume placing data to E2 to AL2 and continue until all data is placed.


    the data in column C is in order from top to bottom, so the data will be placed in rows starting at E1.
    Attached Files Attached Files

  13. #13
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    review post 12 and attachment. thank you

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
    Set r = Range("C1").CurrentRegion
    rw = r.Rows.Count
    Set t = Cells(1, 5).Resize(1 + rw \ 34, 34)
    For i = 1 To r.Rows.Count
    t.Cells(i) = r(i)
    Next
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    it worked great thank you very much!!!!

Posting Permissions

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