Populate data under header and combined based on the same price

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
in SH1 sheet I would match headers with headers in others sheets and populate data under header and combine duplicates Brand if contains the same price
without forgetting add new row before TOTAL row if I have data much in others sheets or delete empty row before TOTAL row if the data are small in other sheets with comparison empty rows in SH1 sheet.I will add new sheets before SH1 sheet.
فاتورة 58720.xlsm
ABCDEFG
1TOTALDATEINVOICE NOPRICEQTYCODEBRAND
27,000.0002024.10.12970000444500.000141657DROUB 70A L KOR
3TOTALDATEINVOICE NOPRICEQTYCODEBRAND
42,065.0002024.10.17970000466385.00041518KM 195/65R15 TA21 KOR
5525.00011672VIPIEMME 70A L IT
6TOTALDATEINVOICE NOPRICEQTYCODEBRAND
78,525.0002024.10.19970000471500.000101657DROUB 70A L KOR
8500.00061673DROUB 70A R KOR
9525.00011672VIPIEMME 70A L IT
10TOTALDATEINVOICE NOPRICEQTYCODEBRAND
111,860.0002024.10.28970000496465.00041386KM 225/60R16 TA21 KOR
12TOTALDATEINVOICE NOPRICEQTYCODEBRAND
135,815.0002024.10.29970000499775.00051677KM 285/60R18 KL61 VIT
14420.00021676DROUB 60A L KOR
15550.00021339XPRO 90A L KOR
16TOTALDATEINVOICE NOPRICEQTYCODEBRAND
17475.0002024.10.30970000502475.00011334XPRO 70A R KOR
Page 0




فاتورة 58720.xlsm
ABCDEFG
1TOTALDATEINVOICE NOPRICEQTYCODEBRAND
23,730.0002024.09.15970000315500.00021619VEGA 70A R KOR
3500.00021541VEGA 70A L KOR
4425.00021641VEGA 55A R KOR
5440.00021642LARGEST 60A L HIGH JAP
6TOTALDATEINVOICE NOPRICEQTYCODEBRAND
74,360.0002024.09.16970000329460.00041643KM 215/65R16 TA31 KOR
8630.00041645265/70R16 ALGERIA
9TOTALDATEINVOICE NOPRICEQTYCODEBRAND
101,780.0002024.09.16970000334445.00041646KM 205/65R16 HS63 KOR
11TOTALDATEINVOICE NOPRICEQTYCODEBRAND
123,300.0002024.09.219700003451,650.00021492GC 385/65R22.5 AT131 CHI
13TOTALDATEINVOICE NOPRICEQTYCODEBRAND
143,520.0002024.09.24970000360880.00041651KM 265/70R16 KOR
15TOTALDATEINVOICE NOPRICEQTYCODEBRAND
16920.0002024.10.02970000392460.00021656DONGA 66A L KOR
17TOTALDATEINVOICE NOPRICEQTYCODEBRAND
1810,875.0002024.10.09970000427625.00041390KM 235/65R17 HP71 KOR
19675.00041662KM 235/55R19 PS71 KOR
20575.00011661DROUB 90A L KOR
212,550.00021663KM 13R22.5 MA11 KOR
22TOTALDATEINVOICE NOPRICEQTYCODEBRAND
232,120.0002024.10.09970000430530.00031619VEGA 70A R KOR
24530.00011457BATTERY 70A KOR
25TOTALDATEINVOICE NOPRICEQTYCODEBRAND
262,140.0002024.10.10970000435535.00041665KM 225/70R16 TA21 KOR
Page 0 (2)




فاتورة 58720.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
220.00
230.00
240.00
250.00
260.00
270.00
280.00
290.00
300.00
310.00
320.00
330.00
340.00
35TOTAL0.000.00
SH1
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=D22*E22




result I want
فاتورة 58720.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR 4.00500.002,000.00
232VEGA 55A R KOR 2.00425.00850.00
243 LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
3514VEGA 70A R KOR4.00530.002,120.00
3615KM 225/70R16 TA21 KOR4.00535.002,140.00
3716KM 195/65R15 TA21 KOR4.00385.001,540.00
3817DROUB 70A L KOR30.00500.0015,000.00
3918VIPIEMME 70A L It2.00525.001,050.00
4019KM 225/60R16 TA21 KOR4.00465.001,860.00
4120KM 285/60R18 KL61 VIT5.00775.003,875.00
4221DROUB 60A L KOR 2.00420.00840.00
4322XPRO 90A L KOR2.00550.001,100.00
4423XPRO 70A R KOR1.00475.00475.00
45TOTAL97.0058,485.00
SH1
Cell Formulas
RangeFormula
D45,F45D45=SUM(D22:D44)
F22:F44F22=D22*E22

thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
combine duplicates Brand if contains the same price
Check your result, you are not combining the Brand correctly and if the price is duplicated.

You also omitted this brand: BATTERY 70A KOR

And I don't understand this part:
without forgetting add new row before TOTAL row if I have data much in others sheets or delete empty row before TOTAL row if the data are small in other sheets with comparison empty rows in SH1 sheet

Try:

VBA Code:
Sub Populate_data()
  Dim sh As Worksheet, sh1 As Worksheet
  Dim idx As Long, lr As Long, i As Long, y As Long, nRow As Long
  Dim dic As Object
  Dim a As Variant, b As Variant
  Dim tQty As Double, tBal As Double
  Dim ky As String
  
  Set sh1 = Sheets("SH1")
  Set dic = CreateObject("Scripting.Dictionary")

  For idx = 1 To Sheets.Count
    Set sh = Sheets(idx)
    If sh.Name <> sh1.Name Then
      lr = lr + sh.Range("G" & Rows.Count).End(3).Row
    End If
  Next
  ReDim b(1 To lr, 1 To 5)
  
  For idx = 1 To Sheets.Count
    Set sh = Sheets(idx)
    If sh.Name <> sh1.Name Then
      a = sh.Range("A2", sh.Range("G" & Rows.Count).End(3)).Value
      
      For i = 1 To UBound(a, 1)
        If a(i, 1) <> "TOTAL" Then
          ky = a(i, 7) & "|" & a(i, 4)
          If Not dic.exists(ky) Then
            y = y + 1
            dic(ky) = y
          End If
          nRow = dic(ky)
          b(nRow, 1) = nRow
          b(nRow, 2) = a(i, 7)
          b(nRow, 3) = b(nRow, 3) + a(i, 5)
          b(nRow, 4) = a(i, 4)
          b(nRow, 5) = b(nRow, 3) * b(nRow, 4)
          tQty = tQty + a(i, 5)
          tBal = tBal + (a(i, 4) * a(i, 5))
        End If
      Next
      
    End If
  Next
  
  Application.ScreenUpdating = False
  
  sh1.Range("B22:F" & Rows.Count).Clear
  sh1.Range("B22").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  lr = sh1.Range("C" & Rows.Count).End(3).Row
  With sh1.Range("B" & lr + 1)
    .Value = "TOTAL"
    .Font.Bold = True
    .Offset(0, 2).Value = tQty
    .Offset(0, 4).Value = tBal
  End With
  With sh1.Range("D22:F" & lr + 1)
    .NumberFormat = "#,##0.00"
  End With
  With sh1.Range("B22:F" & lr + 1)
    .HorizontalAlignment = xlCenter
    .Borders.LineStyle = xlContinuous
  End With
  
  Application.ScreenUpdating = True
End Sub


Result:
Dante Amor
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221DROUB 70A L KOR24.00500.0012,000.00
232KM 195/65R15 TA21 KOR4.00385.001,540.00
243VIPIEMME 70A L IT2.00525.001,050.00
254DROUB 70A R KOR6.00500.003,000.00
265KM 225/60R16 TA21 KOR4.00465.001,860.00
276KM 285/60R18 KL61 VIT5.00775.003,875.00
287DROUB 60A L KOR2.00420.00840.00
298XPRO 90A L KOR2.00550.001,100.00
309XPRO 70A R KOR1.00475.00475.00
3110VEGA 70A R KOR2.00500.001,000.00
3211VEGA 70A L KOR2.00500.001,000.00
3312VEGA 55A R KOR2.00425.00850.00
3413LARGEST 60A L HIGH JAP2.00440.00880.00
3514KM 215/65R16 TA31 KOR4.00460.001,840.00
3615265/70R16 ALGERIA4.00630.002,520.00
3716KM 205/65R16 HS63 KOR4.00445.001,780.00
3817GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
3918KM 265/70R16 KOR4.00880.003,520.00
4019DONGA 66A L KOR2.00460.00920.00
4120KM 235/65R17 HP71 KOR4.00625.002,500.00
4221KM 235/55R19 PS71 KOR4.00675.002,700.00
4322DROUB 90A L KOR1.00575.00575.00
4423KM 13R22.5 MA11 KOR2.002,550.005,100.00
4524VEGA 70A R KOR3.00530.001,590.00
4625BATTERY 70A KOR1.00530.00530.00
4726KM 225/70R16 TA21 KOR4.00535.002,140.00
48TOTAL97.0058,485.00
SH1



🧙‍♂️
 
Upvote 0
Solution
Check your result, you are not combining the Brand correctly and if the price is duplicated.

You also omitted this brand: BATTERY 70A KOR
sorry about it
I tested but show mismatch error in this line
VBA Code:
b(nRow, 5) = b(nRow, 3) * b(nRow, 4)
what does it mean ?
 
Upvote 0
sorry about it
I tested but show mismatch error in this line

what does it mean ?

It means you have letters where you should have numbers
Review the data on your sheets to verify that you do not have letters or cells with blank spaces in the Price and Qty columns.

To know the sheet and the row where you have the problem, run the macro, when the error occurs press the debug button, already in the code, bring the mouse pointer closer to the sh variable, a litte window will be shown there with the name of the sheet.

1730491422297.png

Now bring the mouse closer to the variable i, you will see a number, you increment that number by one and that is the row number in the sheet where you have the problem.

1730491661316.png

Check what you have in cells D and E of that row, take a screenshot if necessary and come back here and show it to me to see what problem you have.

😅
 
Upvote 0
thanks for guiding me .
now I've found my bad . the problem was the header is not matched for some rows.
now every thing is ok
many thanks for your solution.:)
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

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