SumIf in a loop

Tuturino

New Member
Joined
May 16, 2022
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hello guys, I'm a beginner in VBA, I would love it if you can help me out.

Essentially what I would like to do is :

I have two set of datas which have similar variables but one (margin column). I would like to do a macro so that the margin 2 (L column) from the dataset 2 will be stored and then transferred in the dataset 1 (D column). The thing is in the dataset 2, I can have two invoices (or more) for one in the dataset 1. So I would like to sum them up and then send the resultat in the D column next to their invoice number.

I'm stuggling how to integrate the sumIf function in a loop or maybe in a for next.

Here a screen shot of the shrinked dataset (which contains in reality 10 000 rows) :

Capture.PNG


So far I can only do it manually...I am a bit lost on how to proceed

VBA Code:
Sub test1()

    X As Double
    
    R_row = Range("D3").Row
    C_clm = Range("D3").Column
    X = Range("J3")
    LrJ = Range("J" & Rows.Count).End(xlUp).Row
    LrL = Range("J" & Rows.Count).End(xlUp).Row

'variables
   Cells(R_row, C_clm) = Application.WorksheetFunction.SumIf(Range("J2:J" & LrJ), X, Range("L2:L" & LrL))
    
   
   
End Sub

Thanks for your help
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
@Tuturino Welcome
Does this help?

VBA Code:
Sub test2()
   Dim LrB As Long
   Dim LrJ As Long
   Dim MyRng As Range
    
    LrB = Range("B" & Rows.Count).End(xlUp).Row
    LrJ = Range("J" & Rows.Count).End(xlUp).Row
    
    Set MyRng = Range("D3:D" & LrB)
Application.ScreenUpdating = False
    With MyRng
    .Formula = "=SUMIF(J3:J" & LrJ & ",B3,L3:L" & LrJ & ")"
    .Value = .Value
    End With
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,332
Messages
6,124,314
Members
449,153
Latest member
JazzSingerNL

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