Display result of operation in macro

O

orquidea

Hi All

I have the below subprocedure. I get the numerical right answer when it is
displayed in a selected cell ie Range("A1"), but when I set it to be the
equation of a variable I get "Run Time Error "13" Type Mismatch".

Dim pu As Double
pu = "=COUNTIF(Sheet1!e:e,""*-p*"")"
MsgBox (pu)

Could anyone explain to me why I am getting this error and how can I fix it?

Thanks in advance and Happy Holidays!

Orquidea
 
D

Dave Peterson

You're assigning a string to the pu variable. Just because it looks like a
worksheet formula doesn't mean that VBA will evaluate it.

You could try:

dim pu as long 'why use a double?
pu = application.countif(worksheets("sheet1").range("e:e"), "*-p*")
msgbox pu 'no ()'s required here
 
O

orquidea

Thanks for your help. It worked

Dave Peterson said:
You're assigning a string to the pu variable. Just because it looks like a
worksheet formula doesn't mean that VBA will evaluate it.

You could try:

dim pu as long 'why use a double?
pu = application.countif(worksheets("sheet1").range("e:e"), "*-p*")
msgbox pu 'no ()'s required here
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top