Consulting

Results 1 to 11 of 11

Thread: Counting the number of times a string appears on a filtered worksheet.

  1. #1
    VBAX Regular
    Joined
    Nov 2018
    Posts
    75
    Location

    Counting the number of times a string appears on a filtered worksheet.

    Hi

    So my goal is to count the number of times a string occurs in a filtered worksheet. The string is "Ex lamp did not match Proposed". I have the following code which keeps giving me a value of one.
    Sub CountString()
    Dim cl As Range
    Dim rng1 As Range
    Dim DD As Long
        Set rng1 = Range("B2:B200")
    
    
        
        For Each cl In rng1
        If cl.EntireRow.Hidden = False Then
            If cell.Value = "Ex lamp did not match Proposed." Then
                    On Error Resume Next
                    DD = Application.WorksheetFunction.CountIf(ActiveSheet.Cells, "Ex lamp did not match Proposed.")
                    End If
                    Worksheets("Chart&Calc").Range("K2") = DD
    
    
        End If
        Next cl
    End Sub
    Thoughts?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You define a variable cl, but use cell in one line, so should be

    Sub CountString()
    Dim cl As Range
    Dim rng1 As Range
    Dim DD As Long
        Set rng1 = Range("B2:B200")
    
    
        
        For Each cl In rng1
        If cl.EntireRow.Hidden = False Then
            If cl.Value = "Ex lamp did not match Proposed." Then
                    On Error Resume Next
                    DD = Application.WorksheetFunction.CountIf(ActiveSheet.Cells, "Ex lamp did not match Proposed.")
                    End If
                    Worksheets("Chart&Calc").Range("K2") = DD
    
    
        End If
        Next cl
    End Sub
    but really you just need to add them

    Sub CountString()
    Const FILTER_VALUE As String = "Ex lamp did not match Proposed."
    Dim cl As Range
    Dim rng1 As Range
    Dim DD As Long
    
        Set rng1 = Range("B2:B200")
    
        For Each cl In rng1
        
            If cl.EntireRow.Hidden = False Then
            
                If cl.Value = FILTER_VALUE Then
                        
                    DD = DD + 1
                End If
            End If
        Next cl
                
        Worksheets("Chart&Calc").Range("K2") = DD
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    You could probably save some looping

    Option Explicit
    
    
    Sub CountString()
    
    
        Const FILTER_VALUE As String = "Ex lamp did not match Proposed."
    
    
        Dim cl As Range
        Dim rng1 As Range
        Dim DD As Long
    
    
        With ActiveSheet
            Set rng1 = Intersect(.Rows("2:200"), .UsedRange).SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeConstants, xlTextValues)
    
    
            For Each cl In rng1
                If cl.Value = FILTER_VALUE Then DD = DD + 1
            Next cl
                
            'Worksheets("Chart&Calc").Range("K2") = DD
            MsgBox DD
        
        End With
    
    
    End Sub

    I tried a CountIf() on rng1 insttead of the For Each but for some reason VBA didn't like it
    Last edited by Paul_Hossler; 09-09-2020 at 05:26 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
        Cells(1).CurrentRegion.Columns(2).SpecialCells(12).Name = "snb"
        
        For Each it In [snb].Areas
           y = y + Application.CountIf(it, "aa1")
        Next
        
        MsgBox y
    End Sub

  5. #5
    VBAX Regular
    Joined
    Nov 2018
    Posts
    75
    Location
    Hey all,

    so as I read through the responses I need to expand this a bit. So "Ex lamp did not match Proposed" would be visible in column G but the filtering would be based on options in Column B. So Column B would be filtered (Ex column B could say "Arcada") then column G would have several options, one of them being "Ex lamp did not match proposed". So the code would be looking at column G based off what is filtered in column B if that makes sense to count up each text string in column G.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    How about attaching a real life example workbook with realistic data, and the expected answer?

    Meanwhile. maybe something this will work for your new requirements. It's not the fastest (no arrays) but is the most straight-forward (IMHO)

    Option Explicit
    
    
    Sub HowMany()
        Dim B As Range, R As Range
        Dim N As Long
        
        With ActiveSheet
            Set B = .Cells(2, 1).CurrentRegion.Columns(2).SpecialCells(xlCellTypeVisible)
            
            'MsgBox B.Address
        
            For Each R In B.Cells
                If R.Row > 1 Then   '   header
                    If .Cells(R.Row, 7).Value = "Ex lamp did not match Proposed" Then N = N + 1
                End If
            Next
    
    
        End With
        
        MsgBox N
    
    
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 09-10-2020 at 07:56 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    Nov 2018
    Posts
    75
    Location
    Paul,

    see the attached workbook. In Column B on Sheet1, that is what will be filtered, then countIF would be conducted on each one of the values in column G of Sheet 1 and that resultant would be inputed into "Chart&Calc" range K2. For my example I used "Ex lamp did not match Proposed." but all of those values will need the same approach. Hope that is clearer.
    Attached Files Attached Files
    Last edited by Bob Phillips; 09-10-2020 at 08:34 AM. Reason: Removed unnecessary quote

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    I think a pivot table would be easier and more flexible

    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Regular
    Joined
    Nov 2018
    Posts
    75
    Location
    Thanks. I think the people above me got tunnel visioned on wanting to just "hit a button" but this should do as well. Also it seems any variant of the solutions above will work as well if I wanted to go the button route. Thanks for the help!
    Last edited by Bob Phillips; 09-13-2020 at 08:10 AM. Reason: Removed unnecessary quote

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    Management always wants a 'push button' approach. Problems arise when they push the wrong button

    Pivot tables with slicers can make a pseudo-dashboard fairly easily

    Capture.JPG
    Attached Files Attached Files
    Last edited by Paul_Hossler; 09-11-2020 at 08:24 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    VBAX Regular
    Joined
    Nov 2018
    Posts
    75
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Management always wants a 'push button' approach. Problems arise when they push the wrong button

    Pivot tables with slicers can make a pseudo-dashboard fairly easily

    Capture.JPG
    No go on the pivot table but the button push with filtering makes them happen. They will have only one button to push. Thanks for the help sir!

Posting Permissions

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