Getting counts of rows that match specific regex strings between other regex strings

IOO

New Member
Joined
Apr 11, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I have a column with several names that all begin with uppercase letters and between these names are identifiers that each name is associated with. These identifiers all start with a lowercase letter. The names and identifiers vary with each raw data that are uploaded to this sheet. I'm trying to put together a macro/VBA script using regex and don't know how to go about it. I assume using regex ^[A-Z]* and ^[a-z]* to capture the names and identifiers respectively would be a good start to automate how to get the count of these identifiers and put the count to the cell next to each corresponding name:

For example: In the attached image, the yellow highlights are the identifiers and the names are in bold text, all in column B. The bottom sheet in the image shows what I'm trying to achieve. The counts (in red texts) of the identifiers for each names put in the cell next to the name. I will appreciate help with a code.

Capture.JPG
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
A late hello & welcome to Mr. Excel, IOO

I was searching something & came across your post which is not answered yet. I don't know if you're still looking for a solution or not but I think the below code would achieve your goal without the need to RegEx

VBA Code:
Sub CntKeys()

Dim j&

For x = Range("B" & Rows.Count).End(xlUp).Row To 4 Step -1
    If Left(Cells(x, 2), 1) Like "[A-Z]" Then
        Cells(x, 3) = j
        j = 0
    Else
        j = j + 1
    End If
Next

End Sub

Book1
ABC
1
2
3GroupNameID Count
4Team ATracy2
5z12390
6d76653
7C TeamRichard1
8p56723
9Timothy1
10t3304067
11Freddy1
12twest12
13F TeamNancy1
14d021300
15Dina2
16llfinn
17a3478u
Sheet2
 
Upvote 0
.. or use standard worksheet functions. Just ensure where I have used $100 you use a row number that will definitely be as far as your last data.

20 05 10.xlsm
ABC
1
2
3GroupNameID Count
4Team ATracy2
5z12390 
6d76653 
7C TeamRichard1
8p56723 
9Timothy1
10t3304067 
11Freddy1
12twest12 
13F TeamNancy1
14d021300 
15Dina2
16llfinn 
17a3478u 
18 
Count
Cell Formulas
RangeFormula
C4:C18C4=IF(AND(EXACT(LEFT(B4,1),UPPER(LEFT(B4,1))),B4<>""),COUNTA(B5:B$100)-SUM(C5:C$100)-COUNT(C5:C$100),"")
 
Upvote 0
A late hello & welcome to Mr. Excel, IOO

I was searching something & came across your post which is not answered yet. I don't know if you're still looking for a solution or not but I think the below code would achieve your goal without the need to RegEx

VBA Code:
Sub CntKeys()

Dim j&

For x = Range("B" & Rows.Count).End(xlUp).Row To 4 Step -1
    If Left(Cells(x, 2), 1) Like "[A-Z]" Then
        Cells(x, 3) = j
        j = 0
    Else
        j = j + 1
    End If
Next

End Sub

Book1
ABC
1
2
3GroupNameID Count
4Team ATracy2
5z12390
6d76653
7C TeamRichard1
8p56723
9Timothy1
10t3304067
11Freddy1
12twest12
13F TeamNancy1
14d021300
15Dina2
16llfinn
17a3478u
Sheet2

Wow! Worked like a charm. Could you please elaborate on the "4 step -1", I just want to understand the logic? Thanks!
 
Upvote 0
.. or use standard worksheet functions. Just ensure where I have used $100 you use a row number that will definitely be as far as your last data.

20 05 10.xlsm
ABC
1
2
3GroupNameID Count
4Team ATracy2
5z12390 
6d76653 
7C TeamRichard1
8p56723 
9Timothy1
10t3304067 
11Freddy1
12twest12 
13F TeamNancy1
14d021300 
15Dina2
16llfinn 
17a3478u 
18 
Count
Cell Formulas
RangeFormula
C4:C18C4=IF(AND(EXACT(LEFT(B4,1),UPPER(LEFT(B4,1))),B4<>""),COUNTA(B5:B$100)-SUM(C5:C$100)-COUNT(C5:C$100),"")

Thanks for the response, this is good stuff.
 
Upvote 0
Could you please elaborate on the "4 step -1", I just want to understand the logic? Thanks!

I'm looping from the last row with data in column B to row # 4 and the step -1 is to reduce the number in the loop by 1 since I am looping in reverse order (from 17 to 4)

Glad to help & thanks for the feedback
 
Upvote 0
Thanks for the response, this is good stuff.
You're welcome. Thanks for the follow-up. :)

Also, just a slight variation to mse330's approach you may wish to consider if going down the macro path

VBA Code:
Sub ID_Count()
 Dim x As Long, j As Long
 
 j = Range("B" & Rows.Count).End(xlUp).Row
 For x = j To 4 Step -1
    If Cells(x, 2).Value Like "[A-Z]*" Then
        Cells(x, 3).Value = j - x
        j = x - 1
    End If
  Next x
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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