Question about checking for a certain string then counting the number of times it appears in a given range. (VBA)

simplychelsea9

New Member
Joined
Jan 29, 2015
Messages
2
Hello! I am working on a project that is totaling overall values over a certain period, but I am having trouble with the last part, which is finding certain strings in a given range, then counting how many times they appear.

For example, I have this range of data in an excel sheet:

Action
Date
TargetID
ActionMessage
Reset
10/14/2014
company.net
Failure:AccessDenied
Reset
10/14/2014
company.net
Success
Reset
10/14/2014
contractor.net
Success
Reset
10/14/2014
consultant.net
Failure:PasswordError
Reset
10/14/2014
company.net
Failure:AccessDenied

<tbody>
</tbody>











So let's say in cell F13, if I am checking the range for error messages in the ActionMessage Column and want to place the result in F13, I need it to appear like:

Failure: Access denied (2)
Failure: Password Error (1)

Alternatively, if Success if found, I don't want it to list anything.

I don't even know how to begin though.. Do I make an array and then use countif statements? A little push in the right direction would be greatly appreciated because I am completely lost right now. Thanks!
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hey! welcome to MrExcel.

Below is a bit of VBA code. It uses two concepts:

1. Defining a range:
Code:
Set rng = ActiveSheet.Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)

2. Using a Dictionary:
Code:
Set dict = CreateObject("Scripting.Dictionary")

There are other ways of counting occurrences, but I find that the Dictionary Object keeps track of the arrays it uses (keys and items) well and has functions such as: Exists, Add (and others). So, I suggest you spend a bit of time with the Dictionary object.

Code:
Sub DoTheCount()
    Dim rng: Set rng = ActiveSheet.Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
    Dim cCell, dkey, i
    Dim dict: Set dict = CreateObject("Scripting.Dictionary")
    For Each cCell In rng.Cells
        If InStr(cCell, "Failure") > 0 Then
            If dict.exists(cCell.Text) Then
                dict.Item(cCell.Text) = dict.Item(cCell.Text) + 1
            Else
                dict.Add cCell.Text, 1
            End If
        End If
    Next cCell
    i = 0
    For Each dkey In dict.keys
        Range("F13").Offset(i, 0) = dkey & " (" & dict.Item(dkey) & ")"
        i = i + 1
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,539
Members
449,169
Latest member
mm424

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top