+ Reply to Thread
Results 1 to 3 of 3

Countif function problem in a macro

  1. #1
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Countif function problem in a macro

    I'm using the SUM, COUNTA and COUNTIF functions in a macro. The SUM and COUNTA works but the COUNTIF function does not return results.

    Sub B_Test()

    Dim myRange
    Dim Results
    Dim Run As Long
    myRange = Workbooks(1).Worksheets("Master").Range("S6", Range("S6").End(xlDown))
    Range("M3") = Application.WorksheetFunction.Sum(myRange)
    myRange = Workbooks(1).Worksheets("Master").Range("D6", Range("D6").End(xlDown))
    Range("D3") = Application.WorksheetFunction.CountA(myRange)
    'myRange = Workbooks(1).Worksheets("Master").Range("P6", Range("P6").End(xlDown))
    'Range("E3") = Application.WorksheetFunction.CountIf(myRange, "">0"")

    End Sub
    I have tried countless ways to rewrite the COUNTIF line with no results or compiler errors returned. Originally had problems with the SUM and COUNTIF function and found that column formating was the problem. After clearing all column formats, the SUM function promptly began working but the COUNTIF keeps eluding all my efforts. The column which the COUNTIF is pointed to contains values of 0 to 500. Only values greater than 0 are to be counted.

    I would appreciate if someone could trouble shoot this part of my code.
    Thanks in advance.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Countif function problem in a macro

    Hello Mister P,

    When assigning objects to object variables, you need to use the Set statement.
    Example
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Re: Countif function problem in a macro

    Thanks Leith, for your prompt reply. All functions now work as intended. You have saved me hours trying to resolve the problem.

    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1