+ Reply to Thread
Results 1 to 15 of 15

Take Multiple Instances from a Row and Move to a Column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Take Multiple Instances from a Row and Move to a Column

    I have a sheet with the following sample layout (sample attached). The actual sheets have around 8,000 rows each and can have varying numbers of column entries per code, i.e. there could be three as per the sample attached, Country, Animal, Cost or there could be say six Country, Region, Colour, Animal, Cost, Ship.

    Code.........Country1.......Animal1.......Cost1...............Country2........Animal2......Cost2...........Country3.................Animal3.........Cost3

    BZ1234.......Africa............Buffalo.........£10,000.00..........China..............Jaguar.........£752.00.........The Netherlands.........Otter.............£568,346.00
    BZ3658.......America.........Cat..............£3,456.00...........Canada...........Tiger............£34,658.00
    BZ7637.......England.........Dingo............£7,634.00...........Austria............Hog............£34,678.00
    BZ5034.......Fance...........Rabbit...........£890.00


    I need to be able to run a macro that looks across each row in the sheet and extracts the data onto a new sheet putting all the countries into a single column, all the animals into a single column and all the costs into a single column and duplicates the code for each itteration within the row, resulting in:

    Code.........Country..............Animal......Cost
    BZ1234......Africa..................Buffalo.......10,000.00
    BZ1234......China..................Jaguar........752.00
    BZ1234......The Netherlands....Otter.........568,346.00
    BZ3658......America...............Cat............3,456.00
    BZ3658......Canada...............Tiger..........34,658.00
    BZ7637......England...............Dingo.........7,634.00
    BZ7637......Austria................Hog...........34,678.00
    BZ5034......Fance .................Rabbit........890.00

    The macro would need to take account of the fact that the number of columns for associated entries could vary, so it would be helpful if this could somehow be determined through a user entry box. So in this example there are three associated columns for each entry, but this could be any number, so it would need that flexibility. A User entry box would allow the user to enter say, 3 or 6 based on the number of columns per code.

    Is this possible to achieve?

    Many thanks
    Attached Files Attached Files

  2. #2
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi HangMan,

    Forgive me, but it may be worth you pointing out to readers of this post in what way it differs from this previous post of yours, and perhaps also the reasons it can't be continued therein:

    http://www.excelforum.com/excel-prog...readsheet.html

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  3. #3
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi XOR LX,

    I did ask the question as to whether I should continue this in the previous post, (see post 30 in the thread). Jaslake (post 31) suggested making a new post, hence the reason for this post.

    Basically the difference here is that I need to be able to do the whole process in reverse, the original post was taking multiple instances from 4 columns and putting them into rows, now I need to be able to take multiple instances from single rows and put them back into columns. Hopefully the 'Source' and 'Outcome' tabs on the attchment will show what I am trying to achieve.

    Many thanks
    Last edited by HangMan; 08-28-2013 at 06:44 AM.

  4. #4
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Take Multiple Instances from a Row and Move to a Column

    Ok, understood. Thanks for the clarification and I trust that someones with expertise in VBA will answer your request shortly.

    Regards

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi HangMan

    Try this
    Option Explicit
    
    Sub Reverse_Engineer()
        Dim ws As Worksheet, ws1 As Worksheet
        Dim rng As Range, szCell As Range
        Dim i As Long, j As Long, LR As Long, LC As Long, LC1 As Long, cnt As Long
        Dim szLastValue As String
    
        Set ws = Sheets("Source Data")
        Set ws1 = Sheets("Outcome")
        
        Application.ScreenUpdating = False
        With ws1
            .Cells.Clear
            ws.Cells.Copy Destination:=.Range("A1")
            .Activate
            LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            LC = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious).Column
    
            Set rng = .Range("A2:A" & LR)
            
            For i = LR To 2 Step -1
                LC1 = .Range(.Cells(i, 1), (.Cells(i, LC))).Find(What:="*", LookIn:=xlValues, _
                        SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
                If LC1 > 4 Then
                    cnt = 1
                    .Cells(i, 1).Offset(1, 0).Resize((((LC1 - 1) / 3) - 1), 1).EntireRow.Insert
                    For j = 5 To LC1 Step 3
                        .Cells(i, j).Resize(1, 3).Copy
                        .Range(.Cells(i + cnt, "B"), (.Cells(i + cnt, "B"))).PasteSpecial
                        cnt = cnt + 1
                    Next j
                End If
            Next i
    
            LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            .Range("A2:A" & LR).Select
    
            szLastValue = ""
            For Each szCell In Selection.Cells
                If Trim(szCell.Value) <> "" Then
                    szLastValue = szCell.Value
                Else
                    If szLastValue <> "" Then szCell.Value = szLastValue
                End If
            Next szCell
            .Range(.Cells(1, 5), .Cells(1, LC)).EntireColumn.Delete
            .Range("B1").Value = "Country"
            .Range("C1").Value = "Animal"
        End With
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi jaslake,

    Wow, that is amazing, it works perfectly, thank you so much...

    How would I need to adjust the code if there were say five or six columns per 'entry'? I tried a test and with more columns per entry and it errors on this line:

    .Cells(i, 1).Offset(1, 0).Resize((((LC1 - 1) / 3) - 1), 1).EntireRow.Insert
    I tried tweaking various elements of it, but I still get the error...

    Many thanks

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi HangMan

    No clue what's going on. The Code finds the last Column...whether that's 1 entry or 100 entries. I'd need to see the File. Either the Code is broke or the File is different. Attach the offending File and I'll look at it.

  8. #8
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi jaslake,

    Okay, I simply added one extra column per 'set', colour, so there are effectively now 4 entries per set, which would then result in the code column and four additional (rather than three) columns, but that's where the error comes up.

    I've attached the updated sample.

    Many thanks
    Attached Files Attached Files

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi HangMan

    So your File Structure is different. You SIMPLY can't do this
    I simply added one extra column per 'set', colour, so there are effectively now 4 entries per set
    Code is written to deal with your File Structure.

    When you've developed your Final File Structure let us know...perhaps one of us can help.

    Please attach a sample file that represents what you have. The structure of your attachment should be the same structure as your actual data. Any proprietary information should be changed.

    Include in the attachment any code you're currently using (whether it works or not) and an "After" worksheet that demonstrates what you wish the output to be.

    To Attach a File:

    1. Click on Go Advanced
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the "Add Files"... button to locate your file for uploading.
    6. This will open a new window File Upload...Click "Select Files"
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the "Upload Files" button and wait until the file has uploaded.
    10. Click the "Done" Button.

  10. #10
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi Jaslake,

    I'm not sure I understand what you mean by 'the file structure is different? The only thing I changed was to have four columns of data rather than three... Can you explain what you mean by this?

    Many thanks

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi HangMan

    I think you've explained it to yourself...
    The only thing I changed was to have four columns of data rather than three
    Four columns of data rather than three is a change in the File Structure. The Code is looking for three columns of data...not four.

  12. #12
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi Jaslake,

    But I assume it is possible to adapt for however many columns there are, would that be the case? I'm just trying to understand as my knowledge of VBA is very limited...

    Many thanks again

  13. #13
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi HangMan

    The Code adapts itself to as many Columns you wish to throw at it. It does not adapt to the number of Columns in a Data Set. The Code is set for 3.

    If you have different Files with different Data Sets this may be able to be accommodated.

  14. #14
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Take Multiple Instances from a Row and Move to a Column

    Hi Jaslake,

    Okay, that makes sense. I played around with the code this morning and figured out how to adapt it for additional columns in a Data Set, so all is working as hoped now.

    Many thanks for your help, this will save many hours of copying and pasting, very much appreciated.

  15. #15
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Take Multiple Instances from a Row and Move to a Column

    You're welcome...glad I could help. Thanks for the Rep.

+ 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. [SOLVED] Complex VLOOKUP, multiple instances vertical, return subsequent instances horizontally
    By Miles_2804 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-12-2013, 11:54 AM
  2. Replies: 8
    Last Post: 01-30-2013, 02:58 PM
  3. Replies: 4
    Last Post: 10-06-2012, 08:25 AM
  4. Move Multiple-Column Data to One Column (not Transpose)
    By CriticalEric in forum Excel General
    Replies: 5
    Last Post: 04-22-2012, 07:17 PM
  5. Replies: 1
    Last Post: 09-14-2011, 09:56 AM

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