Sumif formula in vba with offset function

kanishkgarg

New Member
Joined
Sep 29, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi,

I need to enter a SUMIF function in my vba code and then paste it throughout the column. There are two worksheets - s1 and s2.

This is the formula I have come up with but it is not working:

VBA Code:
 s2.Range("B3").Formula2R1C1 = "=SUMIF( " & s1.Name &!R3C5:R50000C5, s2.Name!R3C1 . Offset ( ,-1), s1.Name !R3C20:R50000C20 & ")"

Furthermore, I also need to paste it throughout the column till a specific point (which is determined by the number of cells with data in the adjacent column). -
VBA Code:
'Selection.AutoFill Destination:=Range("B3:B" & Range("A" & Rows.Count).End(xlUp).Row)
'Range(Selection, Selection.End(xlDown)).Select

Any help would be greatly appreciated!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,

I need to enter a SUMIF function in my vba code and then paste it throughout the column. There are two worksheets - s1 and s2.

This is the formula I have come up with but it is not working:

VBA Code:
 s2.Range("B3").Formula2R1C1 = "=SUMIF( " & s1.Name &!R3C5:R50000C5, s2.Name!R3C1 . Offset ( ,-1), s1.Name !R3C20:R50000C20 & ")"

Furthermore, I also need to paste it throughout the column till a specific point (which is determined by the number of cells with data in the adjacent column). -
VBA Code:
'Selection.AutoFill Destination:=Range("B3:B" & Range("A" & Rows.Count).End(xlUp).Row)
'Range(Selection, Selection.End(xlDown)).Select

Any help would be greatly appreciated!
for the fill down you can just use
Excel Formula:
s1.range("B2:B" & range("A" & Rows.count).End(xlUp).Row).FillDown '<_ change s1 to s2 if needed

whats the range your first code refers to
 
Upvote 0
try this

Excel Formula:
s2.Range("B3").Formula2R1C1 = "=SUMIF(RC[1]:R[35]C[5],Sheet2!RC[1]:R[3]C[1].offset(0,-1),Sheet1!R[3]C[20]:R[50000]C[20])"
 
Upvote 0
try this

Excel Formula:
s2.Range("B3").Formula2R1C1 = "=SUMIF(RC[1]:R[35]C[5],Sheet2!RC[1]:R[3]C[1].offset(0,-1),Sheet1!R[3]C[20]:R[50000]C[20])"

This is showing an error : Application-defined or object-defined error
 
Upvote 0
for the fill down you can just use
Excel Formula:
s1.range("B2:B" & range("A" & Rows.count).End(xlUp).Row).FillDown '<_ change s1 to s2 if needed

whats the range your first code refers to
The range refers to the list of IDs from sheet 1 (s1) to identify the correct amounts.
 
Upvote 0
This is showing an error : Application-defined or object-defined error
I'm assuming it is showing an error as you didnt replace the "Sheet" reference with your s1 or s2...............It works fine for me
The code changed for you is as follows
Excel Formula:
s2.range("B3").FormulaR1C1 = "=SUMIF(" & s1.Name & "!R[3]C[5]:R[50000]C[5]," & s2.Name & "!R[3]C[0]," & s1.Name & "!R[3]C[20]:R[50000]C[20])"
s2.range("B3:B" & range("A" & Rows.count).End(xlUp).Row).FillDown '<--- change to s1 if this is where you want the formulas copied down
 
Upvote 0
Solution

Forum statistics

Threads
1,215,592
Messages
6,125,713
Members
449,253
Latest member
Mbogo

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