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
 
I follow your steps, but now gives error mismatch in this line
VBA Code:
 .Rows(Application.Match(Cells(R, 2).Text, .Item(2), 0)).Range("E1:G1").Copy Cells(R, 3)
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

As no issue on my side so link your workbook on a file host website like Dropbox …​
 
Upvote 0
'Cause you did not follow post #10 and as I warned with the original code « According to your attachment »​
and like you can see in the VBA procedure which deletes all Result data except the first row​
so at least create the headers in worksheet Result …​
As when I use your original XL2BB attachment I have no borders in your sheets​
so before the codeline .HorizontalAlignment = xlRight just add this codeline : .Borders.Weight = xlThin …​
 
Upvote 0
ok . but it doesn't show number format as in my OP . even if use manually by cell format . it will clear .
 
Upvote 0
So mod the source copy address from :G1 to :J1 …​
According to your last attachment : reduce the IndentLevel to 1 and update .Item(1).Borders.Weight = xlThin …​
 
Last edited:
Upvote 0
The issue on my side when pasting from XL2BB attachment I often loose the cells formatting and sometimes the numeric values come as text …​
So according to your post #13 workbook link removing the useless and updating the necessary the revamped demonstration :​
VBA Code:
Sub Demo1r()
    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:J1").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)
        .Borders.Weight = 2
        .HorizontalAlignment = xlCenter
        .Value2 = Evaluate("ROW(1:" & L - 1 & ")")
    End With
    With Range("F2:H" & L)
        .Formula = V
        .Formula = .Value2
    End With
        Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Solution
A faster way exists but without keeping the cells formatting, so you need after the execution to redo the formatting if necessary …​
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
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