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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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