rwmill9716
Active Member
- Joined
- May 20, 2006
- Messages
- 493
- Office Version
- 2013
- Platform
- Windows
Option Explicit
Sub words()
Dim lr&, i&, j&, k&, r, rng, arr(), res(), word As String, n&, c&, t&, st As String, count&
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")
word = Range("F1").Value ' word criteria
n = 100 ' running time
ReDim res(1 To n, 1 To 3)
lr = Cells(Rows.count, "A").End(xlUp).Row
rng = Range("A2:A" & lr).Value ' given list in col A
Range("F3:H100000").ClearContents
Range("N2:XX100000").ClearContents
Randomize
Do
ReDim arr(1 To UBound(rng), 1 To 1)
c = c + 1: k = 0: count = 0
Do
r = Int(Rnd * UBound(rng)) + 1
If Not dic.exists(r) Then
dic.Add r, ""
k = k + 1: arr(k, 1) = rng(r, 1)
End If
Loop Until k >= UBound(rng)
dic.RemoveAll
For i = 1 To UBound(arr)
st = ""
If arr(i, 1) = Left(word, 1) Then
On Error Resume Next
For j = 0 To Len(word) - 1
st = st & arr(i + j, 1)
Next
On Error GoTo 0
If st = word Then count = count + 1
End If
If arr(i, 1) = Right(word, 1) Then
On Error Resume Next
For j = Len(word) - 1 To 0 Step -1
st = st & arr(i - j, 1)
Next
On Error GoTo 0
If st = word Then count = count + 1
End If
If st = word Then res(c, 1) = count: res(c, 2) = st: res(c, 3) = i
Next
If count > 0 Then
t = t + 1
With Range("XX3").End(xlToLeft).Offset(0, 1)
.Offset(-2, 0).Value = t
.Offset(-1, 0).Value = count
.Resize(UBound(arr), 1).Value = arr
End With
End If
Loop Until c >= n
Range("F3").Resize(n, 3).Value = res
MsgBox "Finish! match found: " & t
End Sub
BEBO,A2:A4993 consists of 192 sets of A-Z, sequentially arranged as each set goes from A to Z and then repeats (referred to as the "List").
I tested with n=100 iterations. Adjustable in code.
For each test iteration:
After n test iterations, the results will be in the range E:H.
- This "List" will shuffle in position, but all items remains unchanged (192 A's, 192 B's,..., 192 Z's).
- Afterward, it looks for sequences of R, I, C, or C, I, R (a condition sequence of any length specified in cell F1).
If you want to know the specific results of each test iteration, they can be found in the columns to the right, starting from column M.
Book2.xlsm
drive.google.com
VBA Code:Option Explicit Sub words() Dim lr&, i&, j&, k&, r, rng, arr(), res(), word As String, n&, c&, t&, st As String, count& Dim dic As Object Set dic = CreateObject("Scripting.Dictionary") word = Range("F1").Value ' word criteria n = 100 ' running time ReDim res(1 To n, 1 To 3) lr = Cells(Rows.count, "A").End(xlUp).Row rng = Range("A2:A" & lr).Value ' given list in col A Range("F3:H100000").ClearContents Range("N2:XX100000").ClearContents Randomize Do ReDim arr(1 To UBound(rng), 1 To 1) c = c + 1: k = 0: count = 0 Do r = Int(Rnd * UBound(rng)) + 1 If Not dic.exists(r) Then dic.Add r, "" k = k + 1: arr(k, 1) = rng(r, 1) End If Loop Until k >= UBound(rng) dic.RemoveAll For i = 1 To UBound(arr) st = "" If arr(i, 1) = Left(word, 1) Then On Error Resume Next For j = 0 To Len(word) - 1 st = st & arr(i + j, 1) Next On Error GoTo 0 If st = word Then count = count + 1 End If If arr(i, 1) = Right(word, 1) Then On Error Resume Next For j = Len(word) - 1 To 0 Step -1 st = st & arr(i - j, 1) Next On Error GoTo 0 If st = word Then count = count + 1 End If If st = word Then res(c, 1) = count: res(c, 2) = st: res(c, 3) = i Next If count > 0 Then t = t + 1 With Range("XX3").End(xlToLeft).Offset(0, 1) .Offset(-2, 0).Value = t .Offset(-1, 0).Value = count .Resize(UBound(arr), 1).Value = arr End With End If Loop Until c >= n Range("F3").Resize(n, 3).Value = res MsgBox "Finish! match found: " & t End Sub
View attachment 97964
I see that the count is counting both the word and its inverse, i.e., the pair will always be there.BEBO,
Thanks for your excellent work here.
I have a few questions for clarification:
1. What does the count in Column F and Row 2 (Cols N to DI mean? Why is it always an even number? In column N, for instance, I see only one occurrence, yet the count is 2.
2. I assume that each column (M and beyond) is a randomization of Column A, one that contains a match? Could I increase the size of Column A? Say I wanted to double it; what changes would be necessary in the code?
3. In your test case above, exactly how many randomized columns were analyzed? As I read your example, would I have found "ALL" or "LLA" 18 times in 100 randomizations 5000 letters, Correct? That would be: the 3 letter word "ALL" or its inverse in a random sequence of 500,000 letters.
4. When I run the program, the spinning wheel continues after your message pops up the count of matches. If I hit "Ok" for that message, the spinning wheel stops. I assume there's no reason to just wait until it stops spinning.
HelloBEBO,
Thanks for your excellent work here.
I have a few questions for clarification:
Bebo,Of course, I can assist you with this new request. However, I assume that you have understood how my code works. If you do, please go ahead and customize it for your new requirements, alright? I'll be ready to help you step by step.