Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default COUNTIF in VBA

Why does this (part of a) macro not work? Who can help me to find a solution?

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(MyData,""*less*"")"
'This one does not work.It doesn't recognize "MyData" as a range.
"MyData" appears as 'text' in the Excelsheet.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default COUNTIF in VBA

The code works fine for me if I have a defined name of MyData.

"MyData" appears as 'text' in the Excelsheet.


What exactly do you mean by this statement?

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Wim VL" <Wim wrote in message
...
Why does this (part of a) macro not work? Who can help me to
find a solution?

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(MyData,""*less*"")"
'This one does not work.It doesn't recognize "MyData" as a
range.
"MyData" appears as 'text' in the Excelsheet.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default COUNTIF in VBA

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(" _
MyData.Address & ",""*less*"")"

--
Regards,
Tom Ogilvy

"Wim VL" <Wim wrote in message
...
Why does this (part of a) macro not work? Who can help me to find a

solution?

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(MyData,""*less*"")"
'This one does not work.It doesn't recognize "MyData" as a range.
"MyData" appears as 'text' in the Excelsheet.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default COUNTIF in VBA

Yes, you diagnosed the problem correctly: the variable MyData is part of your
VBA code and Excel won't recognize it typed into a formula. What you want,
really, is for the formula to contain a reference in standard Excel notation
e.g. A1:B2. You can get this from MyData.Address; so try this:

Selection.Formula = "=COUNTIF(" & MyData.Address & ",""*less*"")"

--
- K Dales


"Wim VL" wrote:

Why does this (part of a) macro not work? Who can help me to find a solution?

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(MyData,""*less*"")"
'This one does not work.It doesn't recognize "MyData" as a range.
"MyData" appears as 'text' in the Excelsheet.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default COUNTIF in VBA

Since every thing is hard-coded anyway, you could simplify that to just

Range("D16").Formula = "=COUNTIF(D4:D14,""*less*"")"

HTH,
Bernie
MS Excel MVP


"Wim VL" <Wim wrote in message
...
Why does this (part of a) macro not work? Who can help me to find a solution?

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(MyData,""*less*"")"
'This one does not work.It doesn't recognize "MyData" as a range.
"MyData" appears as 'text' in the Excelsheet.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default COUNTIF in VBA

Thank you. This the one I needed. It works!

"K Dales" wrote:

Yes, you diagnosed the problem correctly: the variable MyData is part of your
VBA code and Excel won't recognize it typed into a formula. What you want,
really, is for the formula to contain a reference in standard Excel notation
e.g. A1:B2. You can get this from MyData.Address; so try this:

Selection.Formula = "=COUNTIF(" & MyData.Address & ",""*less*"")"

--
- K Dales


"Wim VL" wrote:

Why does this (part of a) macro not work? Who can help me to find a solution?

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(MyData,""*less*"")"
'This one does not work.It doesn't recognize "MyData" as a range.
"MyData" appears as 'text' in the Excelsheet.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default COUNTIF in VBA

I mean that it appears in the formula as such, not as a range. It was not
recognized as a range by Excel.

"Chip Pearson" wrote:

The code works fine for me if I have a defined name of MyData.

"MyData" appears as 'text' in the Excelsheet.


What exactly do you mean by this statement?

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Wim VL" <Wim wrote in message
...
Why does this (part of a) macro not work? Who can help me to
find a solution?

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(MyData,""*less*"")"
'This one does not work.It doesn't recognize "MyData" as a
range.
"MyData" appears as 'text' in the Excelsheet.




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default COUNTIF in VBA

Thank you, Tom. But this one didn't work. The one of K Dales did. Could the
reason be the absence of the "&" before "MyData"?

"Tom Ogilvy" wrote:

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(" _
MyData.Address & ",""*less*"")"

--
Regards,
Tom Ogilvy

"Wim VL" <Wim wrote in message
...
Why does this (part of a) macro not work? Who can help me to find a

solution?

Dim MyData As Range
Dim MyResult As Range
Set MyData = Range("D4:D14")
Set MyResult = Range("D16")
MyResult.Select
Selection.Formula = "=COUNTIF(MyData,""*less*"")"
'This one does not work.It doesn't recognize "MyData" as a range.
"MyData" appears as 'text' in the Excelsheet.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Countif or Something Else? Angiewelly Excel Discussion (Misc queries) 3 June 18th 07 02:21 PM
How do I use a countif function according to two other countif fu. Kirsty Excel Worksheet Functions 2 February 20th 06 11:44 AM
edit this =COUNTIF(A1:F16,"*1-2*")+COUNTIF(A1:F16,"*2-1*") sctroy Excel Discussion (Misc queries) 2 September 25th 05 04:13 AM
COUNTIF or not to COUNTIF on a range in another sheet Ellie Excel Worksheet Functions 4 September 15th 05 10:06 PM
COUNTIF in one colum then COUNTIF in another...??? JonnieP Excel Worksheet Functions 3 February 22nd 05 02:55 PM


All times are GMT +1. The time now is 02:14 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"