+ Reply to Thread
Results 1 to 5 of 5

worksheetfunction.countifs with loop

Hybrid View

Lifeseeker worksheetfunction.countifs... 02-02-2012, 12:45 PM
MarvinP Re:... 02-02-2012, 12:53 PM
Simon Lloyd Re:... 02-02-2012, 12:53 PM
Simon Lloyd Re:... 02-02-2012, 12:55 PM
Lifeseeker Re:... 02-02-2012, 01:26 PM
  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    worksheetfunction.countifs with loop

    hi there,

    I'm testing out a VBA using application.worksheetfunction.countifs()

    Dim n As Integer
    Dim result As Double
    Dim lastr As Integer
    lastr = Worksheets("Sheet1").UsedRange.Rows.Count
    
    For n = 2 To lastr
    result = Application.WorksheetFunction.CountIfs(Cells(n, 1), "O", Cells(n, 2), "Yes")
    Next n
    Worksheets("Sheet1").Range("J1").Value = result
    When i hit the button, i keep getting 0.

    any ideas?

    I'm using excel 2010

    thank you
    Attached Files Attached Files
    Last edited by Lifeseeker; 02-02-2012 at 01:26 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,251

    Re: worksheetfunction.countifs with loop

    Hi Lifeseeker,
    See if this isn't what you really want.
    Private Sub OCount_Click()
    
        Dim n As Integer
        Dim result As Double
        Dim TotalResult As Double
        Dim lastr As Integer
        
        lastr = Worksheets("Sheet1").UsedRange.Rows.Count
        
            For n = 2 To 100
                result = Application.WorksheetFunction.CountIfs(Cells(n, 1), "O", Cells(n, 2), "Yes")
                TotalResult = TotalResult + result
            Next n
            
        Worksheets("Sheet1").Range("J1").Value = TotalResult
    
    End Sub
    Your code above only showed the result for the last row, which was zero.

    Read http://www.cpearson.com/excel/Debug.htm to learn how to figure these out for yourself. I set a breakpoint in your code and stepped through it line by line. I could also have set an "Add Watch" to the variable result to see how it changed with each loop.
    Last edited by MarvinP; 02-02-2012 at 12:58 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Re: worksheetfunction.countifs with loop

    Have you checked that actually entering a formual like that on your worksheet produces a result?, i suspect it doesn't, have a go

    Google Me
    Find me located here Simon Lloyd
    and what i'm about Here
    The above is NOT a link to a forum so is NOT against Rule 13
    Last edited by Simon Lloyd; 02-02-2012 at 12:56 PM.

  4. #4
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Re: worksheetfunction.countifs with loop

    Doh!, totally missed that

    Google Me
    Find me located here Simon Lloyd
    and what i'm about Here
    The above is NOT a link to a forum so is NOT against Rule 13

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: worksheetfunction.countifs with loop

    Nice!,

    thank you.

    The result was only showing the last row as I didn't really have the
    [code]

+ 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