Combine records & add values

Arjun.Singh

Board Regular
Joined
Apr 5, 2012
Messages
62
Hello,

Finding it a bit tough to crack, I have the following records (multiple) which need to be combined so that each state has only one record (making it a unique record) with the corresponding fund values added..
Excel Workbook
ABCDEFGH
1StateYearQuarterDateRel. FundsACA Rel. FundsUtilized FundsACA Utilized Funds
2Texas2008Q322-Sep-0815164536025523525
3Arizona2008Q110-Mar-08251443401899569
4Missouri2010Q38-Aug-101172986030149772
5Florida2010Q328-Oct-105110578319602182
6California2010Q311-Sep-10737512623849751
7Ohio2011Q221-Apr-11485382134955205
8Wyoming2012Q130-Jan-1214053537209961
9Florida2012Q317-Oct-121516619595158980
10Texas2012Q331-Oct-122514390865413380
11California2014Q215-Jul-141172131811205483
12Missouri2014Q224-Jul-145110892720304010
13Texas2014Q28-Jul-147375679928156003
14Arizona2014Q319-Aug-144853466953227344
15California2014Q317-Nov-14140354471885911
Sheet1


Desired outcome is (the sum of all the funds, year info remove as it is assumed that the user only need totals) :

Sheet1

<table style="font-family:Arial,Arial; font-size:10pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; " border="1" cellpadding="0" cellspacing="0"> <colgroup><col style="font-weight:bold; width:30px; "><col style="width:149.6px;"><col style="width:80px;"><col style="width:64px;"><col style="width:148px;"><col style="width:116.8px;"><col style="width:115.2px;"><col style="width:126.4px;"><col style="width:145.6px;"></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td>
</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td><td>G</td><td>H</td></tr><tr style="height:42px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">16</td><td style="background-color:#333399; color:#ffffff; font-weight:bold; font-family:Verdana; text-align:center; ">State</td><td style="color:#ffffff; background-color:#333399; font-weight:bold; font-family:Verdana; text-align:center; ">Year</td><td style="background-color:#333399; color:#ffffff; font-weight:bold; font-family:Verdana; text-align:center; ">Quarter</td><td style="background-color:#333399; color:#ffffff; font-weight:bold; font-family:Verdana; text-align:center; ">Date</td><td style="background-color:#333399; color:#ffffff; font-weight:bold; font-family:Verdana; text-align:center; "> Rel. Funds</td><td style="background-color:#333399; color:#ffffff; font-weight:bold; font-family:Verdana; text-align:center; ">ACA Rel. Funds</td><td style="background-color:#333399; color:#ffffff; font-weight:bold; font-family:Verdana; text-align:center; "> Utilized Funds</td><td style="background-color:#333399; color:#ffffff; font-weight:bold; font-family:Verdana; text-align:center; ">ACA Utilized Funds</td></tr><tr style="height:22px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">17</td><td style="font-family:Verdana; text-align:left; ">Texas</td><td style="font-family:Verdana; ">
</td><td style="font-family:Verdana; ">
</td><td style="font-family:Verdana; text-align:right; ">8-Jul-14</td><td style="font-family:Verdana; text-align:right; ">11405</td><td style="font-family:Verdana; text-align:right; ">56067</td><td style="font-family:Verdana; text-align:right; ">11908</td><td style="font-family:Verdana; text-align:right; ">12908</td></tr><tr style="height:22px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">18</td><td style="font-family:Verdana; text-align:left; ">California</td><td style="font-family:Verdana; ">
</td><td style="font-family:Verdana; ">
</td><td style="font-family:Verdana; text-align:right; ">17-Nov-14</td><td style="font-family:Verdana; text-align:right; ">8687</td><td style="font-family:Verdana; text-align:right; ">6124</td><td style="font-family:Verdana; text-align:right; ">12157</td><td style="font-family:Verdana; text-align:right; ">12145</td></tr></table>

The elaborate data will be replaced by the data shown above on the same sheet.

Many thanks in advance!



 
Hi,

Here's an updated version that will retain the most recent year (if of interest to you):

Code:
Option Explicit
Option Base 1

Sub CalcStates()

Dim arrData, arrResults
Dim x As Integer, y As Long, z As Integer, c As Integer, FoundIt As Boolean
Dim sh1 As Worksheet, sh2 As Worksheet

'On Error Resume Next

  ' Define sheets
    Set sh1 = Sheets(1)
    Set sh2 = Sheets(2)

  ' Convert range to first array
    arrData = sh1.Range("A2:H" & sh1.Range("A" & Rows.Count).End(xlUp).Row)

  ' Provide dimensions for second array
    ReDim arrResults(8, 1)

  ' Make sure Boolean is set correctly
    FoundIt = False

  ' Compare the two arrays and sum the results
    For x = LBound(arrData, 1) To UBound(arrData, 1)
        For y = LBound(arrResults, 2) To UBound(arrResults, 2)
            If arrData(x, 1) = arrResults(1, y) Then
                For c = 2 To 4
                    If c <> 3 Then arrResults(c, y) = Application.Max(arrResults(c, y), arrData(x, c))
                Next
                For c = 5 To 8
                    arrResults(c, y) = arrResults(c, y) + arrData(x, c)
                Next
                FoundIt = True ' found the correct state
                Exit For
            End If
        Next
      ' If the state could not be found in the second array
        If FoundIt = False Then
            z = z + 1
            ReDim Preserve arrResults(8, z)
            arrResults(1, z) = arrData(x, 1)
                For c = 2 To 4
                    If c <> 3 Then arrResults(c, z) = Application.Max(arrResults(c, z), arrData(x, c))
                Next
            For c = 5 To 8
                arrResults(c, z) = arrResults(c, z) + arrData(x, c)
            Next
        End If
    Next

  ' Print results to second sheet in workbook
    With sh2
        .Range("A1:H1") = Array("State", "Year", "Quarter", "Date", "Rel. Funds", "ACA Rel. Funds", "Utilized Funds", "ACA Utilized Funds")
        .Range("A2:H" & UBound(arrResults, 2) + 1) = Application.Transpose(arrResults)
        .Columns("A:H").AutoFit
    End With

  ' Add sort options to sort by State
    With sh2.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("A2:A8")
        .SetRange Range("A1:H" & sh2.Range("A" & Rows.Count).End(xlUp).Row)
        .Header = xlYes
        .Apply
    End With

End Sub
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top