+ Reply to Thread
Results 1 to 4 of 4

How to count PASS/FAIL cells in a range...

Hybrid View

  1. #1
    Registered User
    Join Date
    08-04-2010
    Location
    CA, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    How to count PASS/FAIL cells in a range...

    Hi. I have a test log with PASS/FAIL. These results are logged in Excel under the column heading PASS/FAIL.
    How do I count the number of PASS or FAIL using VBA macro?

    So far I have tried:
    Sub PASS_FAIL_COUNT()
    
        Dim count As Integer
        Dim name As String
        count = 0
        Do
            count = count + 1
            name = Range("PASS/FAIL").Select                    
        Loop Until (name <> "PASS" Or name <> "FAIL")
    End Sub
    This one did not work either:
    Sub PASS_FAIL_COUNT()
    
        Dim r As Range
        Dim cell_count As Integer
        Dim name As String
        cell_count = 0
        Do
            Set r = Range("PASS/FAIL").Select
            name = r.Cells(1, r.Columns.count).Select
            If (name = "PASS" Or name = "FAIL") Then
                cell_count = cell_count + 1
            End If
        Loop Until (name <> "PASS" Or name <> "FAIL")
    End Sub
    the error is at the name = Range("PASS/FAIL").Select line.

    My thought process: once I had selected a range, I can now freely "look" at each string in a cell in that range. Not correct I guess.

    Please advice. thank you.

    p.s. all this is currently done in the active sheet.
    Last edited by dtvonly; 05-12-2014 at 11:49 AM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: How to count PASS/FAIL cells in a range...

    Hi, dtvonly,

    you may use CountIf to check the number for each item, CountA if only values are in that range, get the last cell via

    MsgBox Range("A" & rows.count).End(xlUp).Row - 1
    if you have the header in row 1.

    The command you tried to use assumes that there would be a range being named like that which my Excel doesn´t allow.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: How to count PASS/FAIL cells in a range...

    Error at line Range("PASS/FAIL").Select line is because the PASS/FAIL is not valid Named Range. I assume that PASS/FAIL is a named range in your code which you are referring to. If this is the case change this to a valid Named Range like Pass_Fail and then you can try the following code to get the count......
    Sub PASS_FAIL_COUNT()
    Dim rng, cell As Range
    Dim Pass, Fail As Integer
    Set rng = Range("Pass_Fail")
    Pass = WorksheetFunction.CountIf(rng, "pass")
    Fail = WorksheetFunction.CountIf(rng, "fail")
    MsgBox "Pass count is " & Pass
    MsgBox "Fail count is " & Fail
    End Sub
    or like this.......

    Assuming that column C contains the Pass or Fail. If not, change the column C in red color to the column of your choice in the code.

    Sub PASS_FAIL_COUNT_()
    Dim rng, cell As Range
    Dim lr, countP, countF As Long
    lr = Cells(Rows.count, "C").End(xlUp).Row
    Set rng = Range("C2:C" & lr)
    For Each cell In rng
        If cell = "Pass" Then
            countP = countP + 1
        Else
            countF = countF + 1
        End If
    Next cell
    MsgBox "Pass count is " & countP
    MsgBox "Fail count is " & countF
    End Sub
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  4. #4
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,190

    Re: How to count PASS/FAIL cells in a range...

    2 ways...if you want to use a formula
    put a value in col C for pass, and col D for FAIL, using =IF(B2="PASS",1,"")
    then sum the columns
    or
    else VBA code:
    range("a1").select
    While ActiveCell.Value <> ""
       
    select case ActiveCell.value 
        case  "PASS" 
          iPass = iPass + 1
       
        case  "FAIL" 
          iFail = iFail + 1
    end select
       
       ActiveCell.Offset(1, 0).Select  'next row
    Wend
    
    msgbox ipass & " passed"
    msgbox iFail & " failed"
    Last edited by ranman256; 05-12-2014 at 12:16 PM. Reason: spellg

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to make a cell display "FAIL" when range of cells is FAIL
    By crazychile in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-18-2017, 04:13 PM
  2. [SOLVED] Need a formula for pass/fail based on no. of Y answers but automatically fail if 3a is N
    By Researcher1111 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 09-15-2013, 10:20 PM
  3. Replies: 5
    Last Post: 03-16-2010, 07:05 AM
  4. Replies: 13
    Last Post: 09-29-2008, 10:36 AM
  5. [SOLVED] Macro for test result Count as pass, fail and incomplete.
    By Amar in forum Excel General
    Replies: 1
    Last Post: 06-10-2005, 08:05 AM

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