Apply formula to cells without relative cell referencing in VBA

gracecyl

New Member
Joined
May 21, 2018
Messages
12
Hi,

How do I prevent relative cell referencing with this formula?


Dim LastRow As Long

LastRow = Worksheets("data").Cells(Rows.Count, "I").End(xlUp).Row


Range(Range("N2"), Range("N2").End(xlDown)).formula = _
"=SUMIFS(data!J2:J" & LastRow & ",data!I2:I" & LastRow & ",Sheet1!I2)"

Range("N2").Select

Selection.AutoFill Destination:=Range(Range("N2"), Range("N2").End(xlDown))
Range(Range("N2"), Range("N2").End(xlDown)).Select
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try this

Code:
  "=SUMIFS(data!J[COLOR=#ff0000]$[/COLOR]2:J[COLOR=#ff0000]$[/COLOR]" & LastRow & ",data!I[COLOR=#ff0000]$[/COLOR]2:I[COLOR=#ff0000]$[/COLOR]" & LastRow & ",Sheet1!I[COLOR=#ff0000]$[/COLOR]2)"

- not sure if you need the final $ - that depends on what the formula is supposed to be summing!
 
Last edited:
Upvote 0
In that case

Code:
Sub AutoFill()
    Dim LastRow As Long
    LastRow = Worksheets("data").Cells(Rows.Count, "I").End(xlUp).Row
    
    With Range("N2")
        .Formula = "=SUMIFS(data!J$2:J$" & LastRow & ",data!I$2:I$" & LastRow & ",Sheet1!I$2)"
        .Copy Range(Range("N2"), Range("N2").End(xlDown))
    End With
End Sub


Do you have something to prevent Range("N2").End(xlDown) filling formula all the way to row 1048576 ?
 
Last edited:
Upvote 0
It’s not possible to lock cells with “$” on VBA.

Yes, it is. You're just putting a formula in, which works the same however you do it. I doubt you want to lock that last reference though as you'll end up with an entire column of the same result.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,864
Members
449,052
Latest member
Fuddy_Duddy

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