copy columns and summing based on column with ignore the others

Mussa

Active Member
Joined
Jul 12, 2021
Messages
251
Office Version
  1. 2019
  2. 2010
hi
I need macro to merge data based on column B and sum the values in column H but the column I should calculate average price and column J =column i x column h
with ignores columns C,D with considering my data are about 2500 rows

orginal data
KarmaTest1.xlsm
ABCDEFGHIJ
1DATECODEINV-N.OCLIENT-N.OBRANDTYPEORGINQTYPRICETOTAL
24/1/2021A02BlS1/12-021CC-MMSN1/12AC-AD1AA-100TTY110140.001,400.00
34/2/2021A03BlS1/12-022CC-MMSN1/13AC-AD2AA-101TTY212200.002,400.00
44/3/2021A04BlS1/12-023CC-MMSN1/14AC-AD3AA-102TTY316130.002,080.00
54/4/2021A02BlS1/12-024CC-MMSN1/15AC-AD1AA-100TTY110146.001,460.00
64/5/2021A03BlS1/12-025CC-MMSN1/16AC-AD2AA-101TTY212200.002,400.00
74/6/2021A04BlS1/12-026CC-MMSN1/17AC-AD3AA-102TTY316144.002,304.00
main
Cell Formulas
RangeFormula
J2:J7J2=I2*H2




expected result
KarmaTest1.xlsm
ABCDEFGH
1ITEMCODEBRANDTYPEORGINQTYPRICETOTAL
21A02AC-AD1AA-100TTY120143.002,860.00
32A03AC-AD2AA-101TTY224200.004,800.00
43A04AC-AD3AA-102TTY332137.004,384.00
result
Cell Formulas
RangeFormula
H2:H4H2=G2*F2
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi Marc L,

for instance A02 in first image =140+146/2=143 if I have three values when repeat the item in column B should sum the three values and divide for numbers count and so on . it depend on numbers count when divide.
 
Upvote 0

Where should be the result : in the original worksheet or in another one like 'Result' ?​
 
Upvote 0
According to your attachment an Excel basics VBA demonstration to paste to the Result worksheet module :​
VBA Code:
Sub Demo1()
    Dim L&, R&, V
        UsedRange.Offset(1).Clear
        Application.ScreenUpdating = False
    With [Main!A1].CurrentRegion.Columns
       .AdvancedFilter 2, , [B1], True
        L = UsedRange.Rows.Count:  If L = 1 Then Exit Sub
    For R = 2 To L
       .Rows(Application.Match(Cells(R, 2).Text, .Item(2), 0)).Range("E1:G1").Copy Cells(R, 3)
    Next
        V = .Item(2).Address(, , , True) & ",B2,"
        V = Array("=SUMIF(" & V & .Item(8).Address(, , , True) & ")", _
                  "=AVERAGEIF(" & V & .Item(9).Address(, , , True) & ")", "=F2*G2")
    End With
    With Range("A2:A" & L & ",F2:H" & L).Columns
             .HorizontalAlignment = xlRight
             .IndentLevel = 2
             .Item(1).Value2 = Evaluate("ROW(1:" & L - 1 & ")")
        With .Item("F:H")
             .Formula = V
             .Formula = .Value2
        End With
    End With
        Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
thanks . it shows "application defined or object defined error" in this line
VBA Code:
.AdvancedFilter 2, , [B1], True
and clear my data in sheet MAIN
 
Upvote 0
It well works on my side as it is with your attachment so you felt in the bad reader trap !​
Read again post #6 but well this time, just follow the dark red directions …​
 
Upvote 0
ok may be I make mistake but I no know what is . but it doesn't arrange data and not show the numberformat as in sheet main, also delete some borders from some cells.
 
Upvote 0
As it works a treat on my side so where did you paste the procedure ?​
If the name starts with 'Module' then this is very not the Result worksheet module as advised.​
Right click on the Result tab, choose the option about code then paste the procedure there and delete the previous one.​
To test in the same condition as mine, open a brand new workbook where you must have two sheets aka Main & Result​
then paste the respective data to each sheet from your original post and finally paste the procedure in the Result worksheet module.​
After the Dim codeline add this codeline : If Me.Name <>"Result" Then Beep: Exit Sub …​
If an error raises 'cause of the Me statement so this is not a worksheet module.​
If a beep sound arises, this is not the Result worksheet module …​
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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