In order to figure out what is "not working",
I had to duplicate some data and copy your code.

I think the problem is pretty simple.

In your countif function, you're counting ALL the cells in "aRange"
not the row you're working on!

so, replace aRange with:
Range(Cells(j, 3), Cells(j, 42))

also, just to clean it up a bit.
If your macro finds, say, 17 values > 0, then it will put the
number "17" in column 11... SEVENTEEN TIMES!
(once for each value it finds)

I think after you put the NumberofMonths in column 11, you should add:

Exit For


hope this helps,

paul


________________________________
From: c <cad...@gmail.com>
To: MS EXCEL AND VBA MACROS <excel-macros@googlegroups.com>
Sent: Friday, October 2, 2009 1:01:03 AM
Subject: $$Excel-Macros$$ Help with CountIf in Macro


Hi Everyone,

Need some help with this macro.  Basically, I am searching through
rows and looking for any values in the cells that are >0 in columns AR
- CE.
After I find these cells in that range, I need to count the number of
columns.  This can be done with the COUNTIF function.  I also do not
want to count blank cells.

I want to put the result of the countif in the K column.

Here is what I have so far and it's not working.  Go figure.  Please
help.  I think I'm close.


Dim j, k As Long
Dim iRow As Long
Dim aRange As Range
Dim NumberOfMonths As Long

Dim WS1 As Worksheet
Set WS1 = Worksheets("Dept")

NumberOfMonths = 0


Sheets("Dept").Select
With WS1.Range("AR2:CE250")
Set aRange = WS1.Range("$AR$2:$CE$250")
For j = 2 To aRange.Rows.Count
    For k = 44 To 90
      If ((WS1.Cells(j, k) > 0)) Then
            NumberOfMonths = Application.WorksheetFunction.CountIf
(aRange, ">0")
          WS1.Cells(j,11).Value = NumberOfMonths
  End If
    Next k
Next j
End With




--~--~---------~--~----~------------~-------~--~----~
----------------------------------------------------------------------------------
Some important links for excel users:
1. Excel and VBA Tutorials(Video and Text), Free add-ins downloads at 
http://www.excelitems.com
2. Excel tutorials at http://www.excel-macros.blogspot.com
3. Learn VBA Macros at http://www.vbamacros.blogspot.com
4. Excel Tips and Tricks at http://exceldailytip.blogspot.com
 

To post to this group, send email to excel-macros@googlegroups.com
If you find any spam message in the group, please send an email to:
Ayush Jain  @ jainayus...@gmail.com or
Ashish Jain @ 26may.1...@gmail.com
<><><><><><><><><><><><><><><><><><><><><><>
HELP US GROW !!

We reach over 6,500 subscribers worldwide and receive many nice notes about the 
learning and support from the group. Our goal is to have 10,000 subscribers by 
the end of 2009. Let friends and co-workers know they can subscribe to group at 
http://groups.google.com/group/excel-macros/subscribe
-~----------~----~----~----~------~----~------~--~---

Reply via email to