+ Reply to Thread
Results 1 to 5 of 5

Combine Equal rows (columns C & G) and Sum (columns E & F)

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2015
    Location
    AU
    MS-Off Ver
    2019
    Posts
    61

    Combine Equal rows (columns C & G) and Sum (columns E & F)

    Hi,

    I would like to consolidate a table (see below).
    Where:
    Header : row 1
    key columns (C & G),
    sum columns (E & F) and
    keep 1st data on columns (A, B, D, H, I & J)

    I tried to follow some other codes from other post but unfortunately my excel VBA is very limited. I hope someone can share and guide me how to achieve the desired output.

    Thank you.

    excel.jpg

  2. #2
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,156

    Re: Combine Equal rows (columns C & G) and Sum (columns E & F)

    hi rxk. it would be good to upload an excel sample so that we don't have to manually key on our own to test. i have filled up just the essential information and you may try this:
    Sub test()
    
    Cells(1, 1).CurrentRegion.Sort key1:=Cells(1, "C"), order1:=xlAscending, key2:=Cells(1, "G"), order2:=xlAscending, Header:=xlYes
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    For RowX = LR To 2 Step -1
        If Cells(RowX, "C") = Cells(RowX - 1, "C") And Cells(RowX, "G") = Cells(RowX - 1, "G") Then
            If Cells(RowX, "F") <> vbNullString Then
                Cells(RowX - 1, "F") = Cells(RowX - 1, "F") + Cells(RowX, "F")
            Else
                Cells(RowX - 1, "E") = Cells(RowX - 1, "E") + Cells(RowX, "E")
            End If
            Rows(RowX).Delete
        End If
    Next
    
    End Sub
    Attached Files Attached Files

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  3. #3
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Combine Equal rows (columns C & G) and Sum (columns E & F)

    Hi,

    Here's an other suggestion:
    Results will be output to Sheet2 (must exists before running the code)

    Sub Test()
       Dim ar, Dic
       Dim i As Integer, j As Integer, n As Integer
       Dim str As String
       
       Set Dic = CreateObject("Scripting.dictionary")
       
       'Store value in aray
       ar = Cells(1).CurrentRegion.Value
       n = 2
       
       For i = 2 To UBound(ar, 1)
          str = ar(i, 3) & ar(i, 7)
       
          If Not Dic.exists(str) Then
             Dic.Add str, n
             For j = 1 To UBound(ar, 2)
                ar(n, j) = ar(i, j)
             Next j
             n = n + 1
          Else
             For j = 1 To UBound(ar, 2)
                Select Case j
                Case 5, 6:
                   ar(Dic(str), j) = ar(Dic(str), j) + ar(i, j)
                Case Else:
                   ar(Dic(str), j) = ar(i, j)
                End Select
             Next j
          End If
          
       Next i
    
       'Output results to sheet2 (must exists)
       With Sheets(2)
          .Cells(1).CurrentRegion.Clear
          .Cells(1, 1).Resize(n - 1, UBound(ar, 2)) = ar
       End With
    
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Combine Equal rows (columns C & G) and Sum (columns E & F)

    So - one row for RX-4231, one row for ZT-4217 and all in between??


    *I never clicked - this post has languished for about an hour
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  5. #5
    Registered User
    Join Date
    05-12-2015
    Location
    AU
    MS-Off Ver
    2019
    Posts
    61

    Re: Combine Equal rows (columns C & G) and Sum (columns E & F)

    Thank you ALL for your response. This will help me speed up my work.

+ 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. If columns are equal, combine rows and sum cells ?
    By katievh in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-11-2017, 01:03 PM
  2. [SOLVED] combine many columns data into half columns data by creating rows
    By killerware in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-08-2013, 07:53 AM
  3. Duplicate rows, delete columns w/same data, combine columns w/unique data, Mac Excel 2011
    By msmcoin in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 2
    Last Post: 02-03-2013, 02:10 PM
  4. Creating equal rows and columns
    By ElTurbo in forum Excel General
    Replies: 11
    Last Post: 07-11-2011, 04:35 PM
  5. Need help transposing Rows into 3 equal columns
    By Onenguyen in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-06-2010, 10:52 AM
  6. Replies: 2
    Last Post: 01-14-2009, 04:01 AM
  7. [SOLVED] Combine multiple columns into two long columns, Repeating rows in first column
    By anasab@gmail.com in forum Excel General
    Replies: 0
    Last Post: 07-31-2006, 12:13 PM

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