Insert CountIf Formula using VBA

sparky2205

Active Member
Joined
Feb 6, 2013
Messages
481
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Folks,
I'm trying to enter a dynamic COUNTIF formula in a cell using vba. I don't want the result of the formula. I want to enter the formula itself.
This is the basis of what I've been playing around with:
Excel Formula:
Range("F4").Formula2="CountIf($B$9:$B$"&LastRow&"),"<>n/a")"
This gives me the "Expected: end of statement" error.
I've tried a few variations, most of which return the error above, one of which returned an "invalid character" error.

I've successfully inserted formulas previously but the COUNTIF is causing me problems. Or I'm just doing something silly.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try it like
VBA Code:
Range("F4").Formula2 = "CountIf($B$9:$B$" & LastRow & ",""<>n/a"")"
 
Upvote 0
@Fluff missing an equals sign ;)
Rich (BB code):
Range("F4").Formula2 = "=CountIf($B$9:$B$" & LastRow & ",""<>n/a"")"
 
Upvote 0
Solution
Gentlemen,
thank you very much. As ever I am in your debt.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,842
Messages
6,127,235
Members
449,372
Latest member
charlottedv

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