VBA code to multiply many rows with some conditions

tennis07

Board Regular
Joined
Apr 29, 2015
Messages
57
Hello guys,
please could anyone help me to get the result below:

For each day I have 3 columns: color, value and result.
I need to multiply each row of the column value if the date is the same, the color is the same and the result is W.
Eg. For the first 3 rows I have the same date (01/01/2024), the same color (Green) and all the rows = W. So I multiply 2 * 1,5 * 2 and show the result on the first line of this block (Column MULT), which is 6.
If for example I had the first 3 rows with at least one result = L, then, the first line of this block (Column MULT) would be -1. Then I need to do it for all the rows that I have data on my sheet.
I believe the image helps to understand better what I want to explain.

Thanks a lot guys!
Hope someone could help me.
 

Attachments

  • EXCEL.png
    EXCEL.png
    50.9 KB · Views: 17

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Just to clarify, If there ever is a L on that day for that color, the result is always -1?
 
Upvote 0
1712415422130.png


I wouldn't use VBA. Assuming the data is in A:E, then the following should work. Note you will have to change the ranges to select all your data.

Excel Formula:
=LET(a,FILTER($C$2:$D$21,($A$2:$A$21=$A2)*($B$2:$B$21=$B2)),b,IF(a="L",1,a),IF(OR($A2<>$A1,$B1<>$B2),IF(SUM(CHOOSECOLS(b,2))>0,-1,PRODUCT(b,1)),""))
 
Upvote 0
View attachment 109593

I wouldn't use VBA. Assuming the data is in A:E, then the following should work. Note you will have to change the ranges to select all your data.

Excel Formula:
=LET(a,FILTER($C$2:$D$21,($A$2:$A$21=$A2)*($B$2:$B$21=$B2)),b,IF(a="L",1,a),IF(OR($A2<>$A1,$B1<>$B2),IF(SUM(CHOOSECOLS(b,2))>0,-1,PRODUCT(b,1)),""))
Trying to understand the formula you used, its kind of impossible to me.
Thanks a lot.
 
Upvote 0
Formula only runs when the date changes or color changes from the previous row, otherwise blank.
Takes range C2:D21 and filters it to just that date and color
Changes any L's to 1 to create a condition that if L exists in the second column of the filtered range, equates to -1
Otherwise, multiplies the first column values of filtered range together.
 
Upvote 0
Another option with a formula for versions prior to 365. (I recommend you update your Account details to know what Excel version you are using)

varios 06abr2024.xlsm
ABCDEF
1DateColorValueResultMult
201-eneGreen2W6
301-eneGreen1.5W 
401-eneGreen2W 
501-eneBlack1.5W-1
601-eneBlack2L 
701-eneYellow2W2
801-eneRed2W16
901-eneRed2W 
1001-eneRed2W 
1101-eneRed2W 
1202-eneBlue1.5W-1
1302-eneBlue1.5L 
1402-eneGrey1.5W-1
1502-eneGrey2W 
1602-eneGrey2W 
1702-eneGrey2W 
1802-eneGrey3W 
1902-eneGrey3L 
2002-eneGrey3W 
2102-eneGrey3W 
Hoja2
Cell Formulas
RangeFormula
F2:F21F2=IF(COUNTIFS($B$2:B2,B2,$C$2:C2,C2)=1,IF(COUNTIFS(B:B,B2,C:C,C2,E:E,"L"),-1,PRODUCT(D2:INDEX($D$2:$D$21,LOOKUP(2,1/(($B$2:$B$21=B2)*($C$2:$C$21=C2)),ROW($D$2:$D$21))-2+1))),"")


With vba:

VBA Code:
Sub MultiplyRows()
  Dim lr As Long
  lr = Range("E" & Rows.Count).End(3).Row
  With Range("F2:F" & lr)
    .Formula = _
        "=IF(COUNTIFS($B$2:B2,B2,$C$2:C2,C2)=1,IF(COUNTIFS(B:B,B2,C:C,C2,E:E,""L""),-1," & _
        "PRODUCT(D2:INDEX($D$2:$D$" & lr & ",LOOKUP(2,1/(($B$2:$B$" & lr & "=B2)*($C$2:$C$" & lr & "=C2)),ROW($D$2:$D$" & lr & "))-2+1))),"""")"
    .Value = .Value
  End With
End Sub

Regards
Dante Amor
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,167
Members
452,615
Latest member
bogeys2birdies

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