+ Reply to Thread
Results 1 to 3 of 3

VBA code to rearrange data into multiple columns

Hybrid View

  1. #1
    Registered User
    Join Date
    11-10-2011
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    28

    VBA code to rearrange data into multiple columns

    Hello there,

    I have a worksheet with data arranged in descending order based on column A value. What I'm trying to do is to arrange the data into separate columns, as in the rows with the highest value in column A stay in the same place, rows with value -1 are cut and pasted to the first blank cell to the right and so on (sample workbook attached).

    I understand that this requires some kind of loop. I'd be grateful for any help.

    Sub RearrangeData()
    
    Dim LastRow as Long
    Dim FirstEmptyColumn as Long
    Dim Maximum as Byte
    
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    FirstEmptyColumn = Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column + 1
    Maximum = Range("A1").Value
    
    'I'm not sure how to put it in a loop, the bit below is wrong, I'm just trying to grasp the logic of it
    Do Until IsEmpty (Range("A:A") 
    If Range("A:C" & LastRow).Value = Maximum - 1 Then Range("A:C" & LastRow).Cut FirstEmptyColumn.Paste
    Loop
    
    End Sub
    Thanks in advance for any suggestions.

    Regards,
    shinobi
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA code to rearrange data into multiple columns

    Sub macro_1()
    Dim count, cur_num, cur_col
    count = 1
    cur_col = 1
    cur_num = Cells(count, cur_col)
    Do Until Cells(count, cur_col) = ""
        If Not Cells(count, cur_col) = cur_num Then
            Range(Cells(count, cur_col), Cells(Rows.count, cur_col + 2)).Cut Cells(1, cur_col + 3)
            cur_col = cur_col + 3
            cur_num = Cells(1, cur_col)
            count = 0
        End If
        count = count + 1
    Loop
    End Sub
    ?

  3. #3
    Registered User
    Join Date
    11-10-2011
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: VBA code to rearrange data into multiple columns

    Thanks yudlugar!

    this is exactly what I was trying to do!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy data from worksheet into another, rearrange columns.
    By adelkam in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-03-2013, 10:11 PM
  2. [SOLVED] How can I rearrange data in several columns?
    By amisdar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-28-2012, 10:20 PM
  3. Rearrange data from 6 columns to 1 column
    By narzio in forum Excel General
    Replies: 3
    Last Post: 03-06-2012, 02:43 AM
  4. [SOLVED] Rearrange data columns in Pivot Table
    By hedrew3 in forum Excel General
    Replies: 6
    Last Post: 01-24-2011, 05:41 AM
  5. Rearrange two columns of data
    By RexAtHighSpeed in forum Excel General
    Replies: 1
    Last Post: 12-20-2005, 04:30 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1