Variable in countif formula

Rooks

New Member
Joined
Aug 13, 2007
Messages
3
Hi,

I am trying to do a countif statement that has a variable number in it. The formula works when it looks like

Code:
   ActiveCell.FormulaR1C1 = _
        "=COUNTIF(RC[-24]:RC[-1],""> 10"")/COUNT(RC[-24]:RC[-1])"

I am trying to make it so that the '> 10' part can be changed. I tried the below, but both come back as application-defined or object defined error 1004.

Code:
   mprice1 = "> " + CStr(price1)
   ActiveCell.FormulaR1C1 = _
        "=COUNTIF(RC[-24]:RC[-1],[" & mprice1 & "])/COUNT(RC[-24]:RC[-1])"


Code:
   mprice1 = "> " + CStr(price1)
   ActiveCell.FormulaR1C1 = _
        "=COUNTIF(RC[-24]:RC[-1]," & mprice1 & ")/COUNT(RC[-24]:RC[-1])"

Any helpful suggestions on how to get a variable in the formula? Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Well in the first one your trying to replace the wrong part of the formula.

In the second I think the problem could involve quotes.

Try this.
Code:
mprice1 = Chr(34) & ">" & CStr(price1) & Chr(34)
   ActiveCell.FormulaR1C1 = _
        "=COUNTIF(RC[-24]:RC[-1]," & mprice1 & ")/COUNT(RC[-24]:RC[-1])"
 
Upvote 0
Hi

Try:

Code:
mprice1 = "> " + CStr(price1) 
   ActiveCell.FormulaR1C1 = _ 
        "=COUNTIF(RC[-24]:RC[-1],""" & mprice1 & """)/COUNT(RC[-24]:RC[-1])"
 
Upvote 0
Thanks so much.

Both accomplish what I figured must have been an easy fix and it was. Just needed those extra quotes. Thanks a million.
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,272
Members
448,558
Latest member
aivin

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