count number of times that a specific string occurs inside a range of cells

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
457
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I need to get the VBA code where it looks in column P, starting at Row 4 and down... AND only looks at only the visible rows in order to get a count for each string that it is looking for

Column P contains these 'codes' that represent a checkbox on a userform that was previously checked and if it was checked, then it places that 'code' into the cell in column P.


2ecn9g4.jpg


There could be 1 code in each cell, or maybe up to 8 or 9:
(example: the string "EHS1A" shows up in row 6 where there are only 2 'codes', and also in row 7 where there are 8 codes.)
sws6s2.jpg


I believe i will need to have a variable for each one of these 'codes' and it will represent the number of times that the code occurs in the specific range each time it is searched. (I have 26 different 'codes' (such as'EHS1A') that will be searced for in column P and the total for each one will be shown in Row 1 so the user will be able to see how many occurrences for each one is shown on the worksheet.... hope that makes sense.)

I know I will need to use the 'if like' in my VBA code to locate each part of the string such as "EHS1A"... but, I cannot figure out how to search for it just in the visible cells ("SpecialCells(xlCellTypeVisible)") within the required range using the " If Cell Like "*EHS1A*" Then " VBA code.

Thanks in advance for any help that anyone can help me with. Thank you
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
never mind... I got it figured out :)

Code:
Dim cP As Range
Dim aRow As Long
Dim mis4d As String

mis4d = 0
aRow = Cells.Find("*", , , , xlByColumns, xlPrevious, , , False).Row

    For Each cP In Range(Cells(4, 16), Cells(aRow, 16)).SpecialCells(xlCellTypeVisible)
        If cP Like "*MIS4D*" Then
            mis4d = mis4d + 1
        End If
    Next cP

My code now returns "8" (the correct number) when I tested it by selecting the checkbox for "theft" on the userform before the query is ran (no, we dont have a problem with theft here...I just used field that had never been used so I could easily find it later when i go to change the record back to its original state lol ) The query I ran had a total of 15 records and only 8 of them had the string in column P that correlates to 'theft' that it was searching for. Success!! :ROFLMAO:

zx9vll.jpg
 
Upvote 0

Forum statistics

Threads
1,213,564
Messages
6,114,334
Members
448,567
Latest member
Kuldeep90

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