Macros to consolidate cells with duplicate values/texts and sumif values that match with consolidated cells.

Rant

New Member
Joined
Jul 3, 2021
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
Hi guys,

My Excel workbook consists of 4 worksheets;
Sheet 1 has date, item, location/address and number of deliveries,
Sheet 2 has date, item, total cost of the item.
Ref sheet consists of lookup values

Can someone please help me with the macros for the 4th worksheet (Sheet 3)
I want to assign macros to a command button in sheet 3; which will extract values from sheet 1 and 2, consolidate the cells with duplicate values from sheet 1(date, item, loc/address), sumif (number of items delivered) and provide me Invoice value for those units based on sheet2 values.

Thank you

Invoice.xlsx
ABCDEFGHIJKLMNOPQR
1Sheet 1Sheet 2Ref sheet
2DateItemLocationStreetUnitNo. of items deliveredDateItemxxTotal price
31/01/2022AALoc11725341/01/2022AAww100AA
41/01/2022AALoc11725341/01/2022BBxx101BB
51/01/2022AALoc11725341/01/2022CCyy102CC
61/01/2022AALoc11626611/01/2022DDzz50
71/01/2022AALoc11626612/01/2022AAww113
81/01/2022CCLoc11626922/01/2022BBxx114
91/01/2022CCLoc11626372/01/2022CCyy115
101/01/2022BBLoc11626522/01/2022DDzz50
111/01/2022BBLoc1162686
121/01/2022BBLoc1162695
132/01/2022BBLoc1162661Sheet 3
142/01/2022CCLoc1162667
152/01/2022CCLoc1162665DateItemLocationStreetUnitTotal no. of items deliveredInvoice per unit
162/01/2022CCLoc21626691/01/2022AALoc1172510152.48
172/01/2022CCLoc21626651/01/2022AALoc1162612363.45
181/01/2022BBLoc11626233120.52
191/01/2022CCLoc1162612966.55
202/01/2022BBLoc116266164.15
212/01/2022CCLoc11626132137.71
222/01/2022CCLoc21626134140.15
23
24
25
26
Sheet1
Cell Formulas
RangeFormula
O16:O22O16=SUMIFS($F$3:$F$24,$A$3:$A$24,$J16,$B$3:$B$24,$K16,$C$3:$C$24,$L16,$D$3:$D$24,$M16,$E$3:$E$24,$N16)
P16:P22P16=(SUM(SUMIFS($M$3:$M$10,$J$3:$J$10,$J16,$K$3:$K$10,$Q$3:$Q$5)))*($O16/SUMIF($A$3:$A$17,$J16,$F$3:$F$17))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K7:L10Cell Value<0textNO
M7:M10Cell Value<0textNO
K3:L6Cell Value<0textNO
M3:M6Cell Value<0textNO
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Assuming data starts in cell A2 on all sheets.
Check the name of the sheets in the macro.


Try this:
VBA Code:
Sub ConsolidateCells()
  Dim dic1 As Object, dic2 As Object, dicR As Object, dicD As Object
  Dim a As Variant, b As Variant, c As Variant, d As Variant
  Dim i As Long, j As Long, k As Long
  Dim cad As String
  
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  Set dicR = CreateObject("Scripting.Dictionary")
  Set dicD = CreateObject("Scripting.Dictionary")
  a = Sheets("Sheet 1").Range("A2", Sheets("Sheet 1").Range("F" & Rows.Count).End(3)).Value
  b = Sheets("Sheet 2").Range("A2", Sheets("Sheet 2").Range("D" & Rows.Count).End(3)).Value
  ReDim c(1 To UBound(a, 1), 1 To 7)
  d = Sheets("Ref").Range("A2", Sheets("Ref").Range("A" & Rows.Count).End(3)).Value
  
  For i = 1 To UBound(a, 1)
    dicD(a(i, 1)) = dicD(a(i, 1)) + a(i, 6)   'sum by date
    
    cad = a(i, 1) & "|" & a(i, 2) & "|" & a(i, 3) & "|" & a(i, 4) & "|" & a(i, 5)
    If Not dic1.exists(cad) Then
      k = k + 1
      dic1(cad) = k
      For j = 1 To 5
        c(k, j) = a(i, j)
      Next
    End If
    k = dic1(cad)
    c(k, 6) = c(k, 6) + a(i, 6)   'sum by date, item, location, street, unit
  Next
  
  For i = 1 To UBound(d, 1)     'item from ref sheet
    dicR(d(i, 1)) = Empty
  Next
  
  For i = 1 To UBound(b, 1)     'sum by date from sheet 2
    If dicR.exists(b(i, 2)) Then dic2(b(i, 1)) = dic2(b(i, 1)) + b(i, 4)
  Next
  
  For i = 1 To k
    c(i, 7) = dic2(c(i, 1)) * (c(i, 6) / dicD(c(i, 1)))
  Next
  
  Sheets("Sheet 3").Range("A2").Resize(k, UBound(c, 2)).Value = c
End Sub
 
Upvote 0
Solution
The values in the last column seems incorrect, the code return zeros under invoice per unit column.
 
Upvote 0
You can put the data with which you are testing and the correct result.
 
Upvote 0

Forum statistics

Threads
1,223,276
Messages
6,171,140
Members
452,381
Latest member
Nova88

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