Count Sequence in a Column

rwmill9716

Active Member
Joined
May 20, 2006
Messages
493
Office Version
  1. 2013
Platform
  1. Windows
I need a macro or formula that reads the word in Cell C1 and its inverse in E1 and the number of spaces between letters in Cell C2, then counts the number of times this word and its inverse with the appropriate spaces occurs in Column A. The word should contain upto 8 letters and the spaces from 0 to 50. In the example, 2 were found. Note, any letter, including the ones used in the search word, can occur in the spaces.


230906 Genesis Code Random.xlsm
ABCDE
1Lettersricorcir
2i1
3n
4t# Occurrences
5h2
6e
7b
8e
9r
10i
11i
12n
13c
14n
15g
16G
17o
18d
19c
20r
21i
22a
23r
24e
25d
26t
27h
28e
29h
30e
31a
32v
33e
34n
35a
36n
37d
38t
39h
40e
41e
42a
43r
44t
45h
46a
47n
48d
49t
50h
51e
52e
Sheet2
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi rwmill9716,

See how this goes (note based on my understanding and the logic I used in the code A10 should be yellow not A11):

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim i As Long, j As Long
    Dim intPos As Integer
    Dim dblCount As Double
    Dim strTemp As String
    
    Application.ScreenUpdating = False
    
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing the data. Change to suit.
    
    j = wsSrc.Cells(Rows.Count, "A").End(xlUp).Row
    intPos = 1
    
    'Clear existing shading
    With wsSrc
        .Range("A2:A" & j).Interior.Color = xlNone
        'Text in cell C1
        For i = 2 To j
            If Left(wsSrc.Range("A" & i), Val(wsSrc.Range("C2"))) = Mid(wsSrc.Range("C1"), intPos, Val(wsSrc.Range("C2"))) Then
                strTemp = IIf(Len(strTemp) = 0, Mid(wsSrc.Range("C1"), intPos, Val(wsSrc.Range("C2"))), strTemp & Mid(wsSrc.Range("C1"), intPos, Val(wsSrc.Range("C2"))))
                If StrConv(strTemp, vbLowerCase) = StrConv(wsSrc.Range("C1"), vbLowerCase) Then
                    dblCount = dblCount + 1
                    strTemp = ""
                End If
                wsSrc.Range("A" & i).Interior.Color = RGB(255, 255, 0) 'Fill cell in yellow.
                intPos = intPos + Val(wsSrc.Range("C2"))
            End If
        Next i
        'Text in cell E1
        intPos = 1
        strTemp = ""
        For i = 2 To j
            If wsSrc.Range("A" & i).Interior.ColorIndex = xlNone Then
                If Left(wsSrc.Range("A" & i), Val(wsSrc.Range("C2"))) = Mid(wsSrc.Range("E1"), intPos, Val(wsSrc.Range("C2"))) Then
                    strTemp = IIf(Len(strTemp) = 0, Mid(wsSrc.Range("C1"), intPos, Val(wsSrc.Range("C2"))), strTemp & Mid(wsSrc.Range("C1"), intPos, Val(wsSrc.Range("C2"))))
                    If StrConv(strTemp, vbLowerCase) = StrConv(wsSrc.Range("C1"), vbLowerCase) Then
                        dblCount = dblCount + 1
                        strTemp = ""
                    End If
                    wsSrc.Range("A" & i).Interior.Color = RGB(255, 192, 0) 'Fill cell in orange.
                    intPos = intPos + Val(wsSrc.Range("C2"))
                End If
            End If
        Next i
        'Number of occurrences for the text in cells C1 and E1 found in Col. A
        With wsSrc.Range("C5")
            .Interior.Color = RGB(255, 192, 0) 'Fill cell in orange.
            .Value = dblCount
        End With
    End With
    
    Application.ScreenUpdating = True
    
    MsgBox "Done"

End Sub

Regards,

Robert
 
Upvote 0
Thanks, Trebor, for all your work.

This code didn't do anything to the page. Probably, I wasn't clear as to what I was looking for.

I placed 'ric' is C1 and its inverse 'cir' in E1. Additionally, I placed a "1" in C2. What I expected to see was a '2' in C5 which was a count of the occurrences of
'r i c' and 'c i r' in the data set in column A. The '1' in C2 creates the single space between the letters in the target words. Note, this skipped will be filled with any of the letters (here, riinc and criar). If C2 had been a 2, then my target words would have been 'r i c' and 'c i r' and no occurrences would have been found in column A. I want to find these hidden codes in text. Note, column A will contain ~1,000 cells, and my skip sequence could be 0 to 50 placed in cell C2. It's easy to see the occurrences with 0 to 5 spaces, but much more difficult for the greater spaces.
 
Upvote 0
Give this a try. You do not need to give the reverse in E1 as the code automatically includes the reverse of C1.

VBA Code:
Sub myCount()
  Dim s As String, x As String
  Dim c As Long, L As Long, pos As Long

  L = Range("A" & Rows.Count).End(xlUp).Row - 1
  x = Replace(Trim(Replace(StrConv(Range("C1").Value, vbUnicode), ChrW(0), " ")), " ", String(Range("C2").Value, "?"))
  s = Join(Application.Transpose(Range("A2:A" & L + 1).Value), "") & Space(Len(x)) & x & Space(Len(x)) & StrReverse(x)
  pos = Application.Search(x, s)
  Do While pos < L
    c = c + 1
    pos = Application.Search(x, s, pos + 1)
  Loop
  x = StrReverse(x)
  pos = Application.Search(x, s)
  Do While pos < L
    c = c + 1
    pos = Application.Search(x, s, pos + 1)
  Loop
  Range("C5").Value = c
End Sub

With the sample data that gives 2 in C5.

A couple of other examples

These occurrences are found in rows 4:6, 26:28, 38:40, 49:51 and 26:24 (reverse)
rwmill9716.xlsm
ABC
1Letterste
2i1
3n
4t# Occurrences
5h5
Sheet1


These occurrences are found in rows 38:41, 49:52 and 44:41 (reverse)
rwmill9716.xlsm
ABC
1Letterste
2i2
3n
4t# Occurrences
5h3
6e
Sheet1
 
Upvote 0
This code didn't do anything to the page

That's odd as it highlighted the same cells for me (except for A11) as in your example 🤔

I haven't tested it but when Peter posts a solution it's generally the one to go with in any case 👌 😎
 
Upvote 0
That's odd
I'm wondering if perhaps the OP did see or act on the comment in this line of your code since their sample data at least is on 'Sheet2'
VBA Code:
Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing the data. Change to suit.

In any case I think there are a few issues with your code.
  • As you mentioned it highlights A10 rather than A11. In fact it really counts incorrectly there (at least by my understanding of the requirement) since if you change cell A11 to x the code still gives a count of 1 even though the sequence of cells is now not "r?i?c"
  • It returns a count of 1 when it should return a count of 2 because it does not check for the E1 value (with spaces) which can be found in cells A19:A23 as highlighted in the sample data.
  • It also still only returns a count of 1 if the C1 value (with spaces) occurs more than once in the data. For example in the sample data below it should count 2 not 1

rwmill9716.xlsm
ABC
1Lettersric
2r1
3n
4i# Occurrences
5h1
6c
7b
8e
9r
10i
11i
12n
13c
14n
Sheet1


BTW, I think that this non-looping and much more compact methods also works.
I have assumed that the search for the letters is not case-sensitive so that if C1 was "Ric" the count would still be 2. If the search should be case-sensitive and the count for "Ric" should be zero, then just remove the IgnoreCase line in the code.

VBA Code:
Sub myCount_v2()
  Dim RX As Object
  Dim s As String
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = True  '<- Remove this line if the search should be case-sensitive
  RX.Global = True
  RX.Pattern = Replace(Trim(Replace(StrConv(Range("C1").Value, vbUnicode), ChrW(0), " ")), " ", String(Range("C2").Value, "."))
  s = Join(Application.Transpose(Range("A2", Range("A" & Rows.Count).End(xlUp)).Value), "")
  Range("C5").Value = RX.Execute(s).Count + RX.Execute(StrReverse(s)).Count
End Sub
 
Last edited:
Upvote 0
Solution
Thanks Peter. I'm not great with regular expressions but totally agree that if there's a non looping solution it's the way to go.

I appreciate your effort in going my code and highlighting where it was not returning the correct results 😎 👌

Many thanks,

Robert
 
Upvote 0
I'm wondering if perhaps the OP did see or act on the comment in this line of your code since their sample data at least is on 'Sheet2'
VBA Code:
Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing the data. Change to suit.

In any case I think there are a few issues with your code.
  • As you mentioned it highlights A10 rather than A11. In fact it really counts incorrectly there (at least by my understanding of the requirement) since if you change cell A11 to x the code still gives a count of 1 even though the sequence of cells is now not "r?i?c"
  • It returns a count of 1 when it should return a count of 2 because it does not check for the E1 value (with spaces) which can be found in cells A19:A23 as highlighted in the sample data.
  • It also still only returns a count of 1 if the C1 value (with spaces) occurs more than once in the data. For example in the sample data below it should count 2 not 1

rwmill9716.xlsm
ABC
1Lettersric
2r1
3n
4i# Occurrences
5h1
6c
7b
8e
9r
10i
11i
12n
13c
14n
Sheet1


BTW, I think that this non-looping and much more compact methods also works.
I have assumed that the search for the letters is not case-sensitive so that if C1 was "Ric" the count would still be 2. If the search should be case-sensitive and the count for "Ric" should be zero, then just remove the IgnoreCase line in the code.

VBA Code:
Sub myCount_v2()
  Dim RX As Object
  Dim s As String
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = True  '<- Remove this line if the search should be case-sensitive
  RX.Global = True
  RX.Pattern = Replace(Trim(Replace(StrConv(Range("C1").Value, vbUnicode), ChrW(0), " ")), " ", String(Range("C2").Value, "."))
  s = Join(Application.Transpose(Range("A2", Range("A" & Rows.Count).End(xlUp)).Value), "")
  Range("C5").Value = RX.Execute(s).Count + RX.Execute(StrReverse(s)).Count
End Sub
Thanks, Peter and Robert, for your time on this. Peter's macro works great.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,124,999
Members
449,201
Latest member
Lunzwe73

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