vba Range("B8").Formula = "=COUNTIF(B2:J2,B7)"

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello.
I have this formula

Range("B8").Formula = "=COUNTIF(B2:J2,B7)"

I am trying to use Cells Method like:

Cells(8, 2).formula = “=countif(“ & cells(2, 2) & “” : “” & cells(20, 2) & “, & cells(7, 2)”

but no results.

Please can you explain me why, and how to do it.
or it is not possible. I don't know,

thanks for reading.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,​
you are confusing cell content and cell address …​
 
Upvote 0
Use
VBA Code:
Cells(8, 2).Formula = "=countif(" & Cells(2, 2).Address & ":" & Cells(20, 2).Address & "," & Cells(7, 2) & ")"
 
Upvote 0
Solution
... meaning, Cells default property is Value
When any property is omitted, the default property is implicitly obtained. You're in need of addresses, so you have to specify this explicitly. The following might point you in the right direction.

VBA Code:
Sub montecarlo2012()

    Dim AbsRng As String, RelRng As String

    AbsRng = Cells(2, 2).Address & ":" & Cells(20, 2).Address & "," & Cells(7, 2).Address
    RelRng = Replace(AbsRng, "$", "")

    Cells(8, 2).Formula = "=COUNTIF(" & RelRng & ")"

End Sub
 
Upvote 0
Also, a bit cleaner
VBA Code:
[B8].Formula = "=countif(" & [b2].Address & ":" & [J20].Address & "," & [B7] & ")"
 
Upvote 0
Hello Marc, Can you explain me please my confusion
Thanks for your respond.
 
Upvote 0
You are welcome and thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,215,274
Messages
6,123,989
Members
449,137
Latest member
abdahsankhan

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