Add formula to last row and use a range B:lastrow in the formula

Glasgowsmile

Active Member
Joined
Apr 14, 2018
Messages
280
Office Version
  1. 365
Platform
  1. Windows
I'm attempting something like the below but I'm having trouble getting it to work.

The B column is filled by vba code so I don't know how many rows it will take up. I want to be able to add a formula in the B column after the last row but this means I can't just do B18:B2000 as the reference for this formula since it gives me a circular reference.

How can I find the last row and use that last row as the end of the range for the B column?

VBA Code:
Sub last_row()
Worksheets("ARCS").Activate
Dim lastrow As Long

lastrow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row

With ActiveSheet
 .Cells(Rows.Count, "C").End(xlUp)(2).Formula = "=IF(COUNTIF(B18:lastrow, ""Yes""), ""New Value"")"
End With
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
See if this does what you want:
VBA Code:
Sub MyMacro()

    Dim lastrow As Long

'   Find lastr row in column B with an entry
    lastrow = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Put formula in row after last row in column B
    Cells(lastrow + 1, "B").Formula = "=IF(COUNTIF(B18:B" & lastrow & ", ""Yes""), ""New Value"")"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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