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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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,196,428
Messages
6,015,191
Members
441,882
Latest member
rcgyuk

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