Formula to the last row

th259

New Member
Joined
Oct 24, 2023
Messages
35
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Range("G3").FormulaR1C1 = "=SUMIF(R[13]C[1]:R[42]C[1],RC[1],R[13]C:R[42]C)"

I had the code above, but I want to change it so that it goes to the last row in the worksheet.

I changed it to:
VBA Code:
 Dim lastRow As Long
        lastRow = Cells(Rows.Count, "A").End(xlUp).Row
        ' Adjust the SUMIF formula ranges to absolute references
        Range("G3").FormulaR1C1 = "=SUMIF(R16C17:R" & lastRow & "C17,RC[1],R16C16:R" & lastRow & "C16)"

but the final formula after the VBA is
Excel Formula:
=SUMIF($C$16:$C$26,Q3,$B$16:$B$26)
instead of the formula I wanted
Excel Formula:
=SUMIF($Q$15:$Q$26,Q3,$P$15:$P$26)

Can somebody please tell me how to fix this?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try change R16 to R15:

Code:
 Range("G3").FormulaR1C1 = "=SUMIF(R15C17:R" & lastRow & "C17,RC[1],R15C16:R" & lastRow & "C16)"
 
Upvote 0
I'm sorry, I made a mistake copying and pasting. The row the VBA picked is fine, it's the column. The code is picking up column C instead of Q and column B instead of P.
 
Upvote 0
It isn't picking up the wrong columns for me

Book1
ABCDEFG
1Order ID
2106-2-F90
3104-6-N380
4109-2-N19
5102-8-N50
6108-3-F66
7103-11-F27
8103-26-F95
9105-25-N18
10104-7-N07
11103-15-F55
12106-6-N94
13104-17-N48
14105-28-N51
15104-31-N32
16105-16-N08
17102-27-F90
18110-5-N72
19108-1-F09
20107-14-N42
21105-22-N14
Sheet1
Cell Formulas
RangeFormula
G3G3=SUMIF($Q$16:$Q$21,H3,$P$16:$P$21)
 
Upvote 1
The H3 was supposed to be Q3. I figured it out, there are some cutting and pasting columns afterward, so it was supposed to be column 8. I fixed it and it worked.
VBA Code:
Range("G3").FormulaR1C1 = "=SUMIF(R16C8:R" & lastRow & "C8, RC[1], R16C7:R" & lastRow & "C7)"

Thank you for checking!
 
Upvote 0

Forum statistics

Threads
1,219,483
Messages
6,148,590
Members
450,823
Latest member
papilo

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