Add values in rows based on matching values in several columns

Nanashi7562

New Member
Joined
Aug 29, 2022
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
Platform
  1. Windows
Hello and Good Day! This is my first post so please be gentle with me.
I have a data set where I have several rows of data and I need to copy the data to another sheet. Please refer to the sample sheet below.
1661780584271.png


I need to transfer the following data to another workbook using VBA. However, when I transfer it to the other workbook, if the columns for ID, NAME, CATEGORY and LOCATION should match,
the values in the columns Q1 to Q4 should be added. Please refer below.
1661780792651.png


Do you guys have any idea or suggestion on how to do this ? Any tips will be appreciated. Thank you.
 

Attachments

  • 1661780405206.png
    1661780405206.png
    15.1 KB · Views: 2

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Very quickly done using a pivot table
Book1
ABCDEFGH
2IDNameCatLocQ1Q2Q3Q4
31111AZNANYC10021
42234BBnBLA52012
51234ARTCDC3333
61111AZNANYC0226
72234BBNBLA1602
84456RATDCAN8888
92234BBNCLA1602
10
11
12Values
13IDNameCatLocSum of Q1Sum of Q2Sum of Q3Sum of Q4
141111AZNANYC10247
151234ARTCDC3333
162234BBnBLA68014
17CLA1602
184456RATDCAN8888
Sheet1
 
Upvote 0
Assuming your data starts in row 3 of each sheet, try:
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, i As Long, dic As Object, val As String
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    v1 = srcWS.Range("A3").CurrentRegion.Value
    v2 = desWS.Range("A3").CurrentRegion.Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        val = v1(i, 1) & "|" & v1(i, 2) & "|" & v1(i, 3) & "|" & v1(i, 4)
        If Not dic.exists(val) Then
            dic.Add val, v1(i, 5) & "|" & v1(i, 6) & "|" & v1(i, 7) & "|" & v1(i, 8)
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        val = v2(i, 1) & "|" & v2(i, 2) & "|" & v2(i, 3) & "|" & v2(i, 4)
        If dic.exists(val) Then
            desWS.Range("E" & i + 2).Resize(, 4).Value = Array(Split(dic(val), "|")(0), Split(dic(val), "|")(1), Split(dic(val), "|")(2), Split(dic(val), "|")(3))
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
Change the sheet names (in red) to suit your needs.
 
Upvote 0
Very quickly done using a pivot table
Book1
ABCDEFGH
2IDNameCatLocQ1Q2Q3Q4
31111AZNANYC10021
42234BBnBLA52012
51234ARTCDC3333
61111AZNANYC0226
72234BBNBLA1602
84456RATDCAN8888
92234BBNCLA1602
10
11
12Values
13IDNameCatLocSum of Q1Sum of Q2Sum of Q3Sum of Q4
141111AZNANYC10247
151234ARTCDC3333
162234BBnBLA68014
17CLA1602
184456RATDCAN8888
Sheet1
Thank you for the suggestion but unfortunately this needs to be implemented via VBA. We did originally suggest to do this approach but my boss declined :)
 
Upvote 0
Assuming your data starts in row 3 of each sheet, try:
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, i As Long, dic As Object, val As String
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    v1 = srcWS.Range("A3").CurrentRegion.Value
    v2 = desWS.Range("A3").CurrentRegion.Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        val = v1(i, 1) & "|" & v1(i, 2) & "|" & v1(i, 3) & "|" & v1(i, 4)
        If Not dic.exists(val) Then
            dic.Add val, v1(i, 5) & "|" & v1(i, 6) & "|" & v1(i, 7) & "|" & v1(i, 8)
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        val = v2(i, 1) & "|" & v2(i, 2) & "|" & v2(i, 3) & "|" & v2(i, 4)
        If dic.exists(val) Then
            desWS.Range("E" & i + 2).Resize(, 4).Value = Array(Split(dic(val), "|")(0), Split(dic(val), "|")(1), Split(dic(val), "|")(2), Split(dic(val), "|")(3))
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
Change the sheet names (in red) to suit your needs.
Thankyou very much for this... let me test and get back to you
 
Upvote 0
An alternative is Power Query
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"ID", "Name", "Cat", "Loc"}, {{"QT1", each List.Sum([Q1]), type number}, {"QT2", each List.Sum([Q2]), type number}, {"QT3", each List.Sum([Q3]), type number}, {"QT4", each List.Sum([Q4]), type number}})
in
    #"Grouped Rows"
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,693
Members
449,331
Latest member
smckenzie2016

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