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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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