+ Reply to Thread
Results 1 to 4 of 4

Move Duplicate Vertical to Horizontal

Hybrid View

  1. #1
    Registered User
    Join Date
    07-24-2007
    Posts
    6

    Move Duplicate Vertical to Horizontal

    Hello, I would like help moving a portion of vertical data to be horizontal.

    I have a report that is in Excel that is 12 columns wide and a few thousand rows long.
    The data is sales data that if the sale record is completed with no issues then I get one row but if the order has any discounts applied against it then it repeats the record for every discount.

    I need the data to show as a single row and move the duplicates to be within the single row.

    When you look at the data you will notice Sales Number 23630 is the perfect scenario with no adjustments against revenue but orders 13060 and 13150 both have discounts.

    I am thinking that I can use the Sales Number and when it repeats grab the data and put it in the columns needed. Columns are fixed with 12 columns on the report every week. I am sure for those good at VBA this is easy but I need guidance.

    Please see condensed sample data attached. I did condense the data down to only 4 columns but if you can give me a solution that works for the 4 columns I think I can get it to work for 12 columns.

    Thanks and if this is too vague please let me know and I'll revise.
    Attached Files Attached Files
    Last edited by sampson20; 12-27-2010 at 05:26 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,927

    Re: Move Duplicate Vertical to Horizontal

    The macro below starts at the bottom of the sheet working its way up. If the bottom sale number is the same as the previous then the description and amount are appended to the previous row until the end of the row is reached, then the old lower row is deleted.

    Try it on a back up copy of your workbook, in case your actual column layout is not the same as the sample.

    Option Explicit
    Sub CountSalesIDs()
        'Ben Van Johnson, Excel Forum 12/17/2010
        
        Dim TestCell        As Range, _
            RefCell         As Range, _
            DataSheetName   As String, _
            HeaderRow       As Long, _
            HeaderColumn    As Long, _
            TestRow         As Long, _
            NextCol         As Long, _
            lastcol         As Long, _
            HeaderParm
    
        
        'find the cell with the first table header
        With Cells
            Set HeaderParm = .Find("Sales Number")
            HeaderRow = HeaderParm.Row
            HeaderColumn = HeaderParm.Column
        End With
        
        'get row of the last sale number
        SalesCount = Cells(Rows.Count, HeaderColumn).End(xlUp).Row
        
        'Start at the bottom of the sheet, testing the sale number against the one above
        'if they are the same, then append the description and amount from the lower
        'line to the upper line
        'When there are several repeats of a sale number the lowest record is alway at the end
    
        For TestRow = SalesCount To HeaderRow - 1 Step -1
                    
            'get the last column of the current line
            lastcol = Cells(TestRow, Columns.Count).End(xlToLeft).Column
            
            'get the column where data is to be appended
            NextCol = lastcol + 1
            
            'compare the sales numbers
            If Cells(TestRow, HeaderColumn).Value = Cells(TestRow - 1, HeaderColumn).Value Then
            
                'start with the description column and move right to the end if the row
                For Each RefCell In Range(Cells(TestRow, HeaderColumn + 2), Cells(TestRow, lastcol))
                
                    'increment the column to append to
                    NextCol = Cells(TestRow - 1, Columns.Count).End(xlToLeft).Column + 1
                    
                    'copy the current data to the end of the upper row
                    Cells(TestRow - 1, NextCol).Value = RefCell.Value
                    
                    'if the new append column has no header, then
                    'check if the value just copied is a text string
    
                    If Cells(HeaderRow, NextCol).Value = "" Then
                    
                        If WorksheetFunction.IsText(RefCell.Value) Then
                            Cells(HeaderRow, NextCol).Value = "Discount Reason"
                        Else
                            Cells(HeaderRow, NextCol).Value = "Discount Amount"
                        End If
                    End If
                    NextCol = NextCol + 1
                Next RefCell
            
            'delete the current row, i.e., the one just appended to the upper row
            Range(Cells(TestRow, 1).Address).EntireRow.Delete
            End If
        Next TestRow
    End Sub
    Attached Files Attached Files
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    07-24-2007
    Posts
    6

    Re: Move Duplicate Vertical to Horizontal

    Code provided above by protonLeah worked great. I had to define one of the variables and I get an error when I get to the end of the file (or top since it goes from bottom to top). But Variable was easily defined and I just ignored error.

    Comments in code made it very easy to follow.

    thanks so much...

  4. #4
    Registered User
    Join Date
    01-09-2011
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Move Duplicate Vertical to Horizontal

    I am opening the sample & when running the macro the VB edititor is opening & cursor is pointing towards Ln24, Col 17

    Pop up "Compile Error: Variable Not defined.

    Please help. Unable to use the macro

    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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