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
Range("F1").Resize(UBound(c, 1), UBound(c, 2)).Value = c
ReDim b(1 To UBound(a, 1), 1 To 1000) 'times
Sub rand_word_v2()
Dim a As Variant, b As Variant, c As Variant, arr As Variant
Dim i&, j&, k&, lr&, n&, m&, x&, y&, z&
Dim w1 As String, w2 As String, w3 As String
Randomize
a = Range("A1", Range("A" & Rows.Count).End(3)).Value
ReDim b(1 To UBound(a, 1), 1 To 1000) 'times
ReDim c(1 To UBound(b, 1), 1 To UBound(b, 2))
With Range("C1")
w1 = LCase(Mid(.Text, 1, 1))
w2 = LCase(Mid(.Text, 2, 1))
w3 = LCase(Mid(.Text, 3, 1))
End With
For j = 1 To UBound(b, 2)
arr = Evaluate("ROW(1:" & UBound(a, 1) & ")") 'total records
lr = UBound(a, 1)
For z = 1 To UBound(a) 'how many do i want
x = Int(Rnd * lr + z)
y = arr(z, 1)
arr(z, 1) = arr(x, 1)
arr(x, 1) = y
lr = lr - 1
m = arr(z, 1) 'random number
b(z, j) = a(m, 1)
Next
For i = 3 To UBound(b, 1)
If LCase(b(i - 2, j)) = w1 And LCase(b(i - 1, j)) = w2 And LCase(b(i, j)) = w3 Then
k = k + 1
For n = 1 To UBound(b, 1)
c(n, k) = b(n, j)
Next
End If
Next
Next
Range("D1").Value = k
Range("F1").Resize(UBound(c, 1), UBound(c, 2)).Value = c
End Sub
Dante,Try the following macro.
The result of the count in cell D1.
From column F onwards the matches.
View attachment 97919
NOTES:
1. It works for 3 letters in cell D1.2. If you don't want the result columns, remove this line from the macro:Range("F1").Resize(UBound(c, 1), UBound(c, 2)).Value = c
3. Set the number of times in this line of the macro:ReDim b(1 To UBound(a, 1), 1 To 1000) 'times
Put the following code in a module:
VBA Code:Sub rand_word_v2() Dim a As Variant, b As Variant, c As Variant, arr As Variant Dim i&, j&, k&, lr&, n&, m&, x&, y&, z& Dim w1 As String, w2 As String, w3 As String Randomize a = Range("A1", Range("A" & Rows.Count).End(3)).Value ReDim b(1 To UBound(a, 1), 1 To 1000) 'times ReDim c(1 To UBound(b, 1), 1 To UBound(b, 2)) With Range("C1") w1 = LCase(Mid(.Text, 1, 1)) w2 = LCase(Mid(.Text, 2, 1)) w3 = LCase(Mid(.Text, 3, 1)) End With For j = 1 To UBound(b, 2) arr = Evaluate("ROW(1:" & UBound(a, 1) & ")") 'total records lr = UBound(a, 1) For z = 1 To UBound(a) 'how many do i want x = Int(Rnd * lr + z) y = arr(z, 1) arr(z, 1) = arr(x, 1) arr(x, 1) = y lr = lr - 1 m = arr(z, 1) 'random number b(z, j) = a(m, 1) Next For i = 3 To UBound(b, 1) If LCase(b(i - 2, j)) = w1 And LCase(b(i - 1, j)) = w2 And LCase(b(i, j)) = w3 Then k = k + 1 For n = 1 To UBound(b, 1) c(n, k) = b(n, j) Next End If Next Next Range("D1").Value = k Range("F1").Resize(UBound(c, 1), UBound(c, 2)).Value = c End Sub
--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
So I didn't understand your requirement.1. The rearrangements of column A don't appear to be random, i.e., it's not possible for "Ric" to show up in each column.
2. What I want is column A to be 10,000 rows (192 complete sets of 26 letters alphabet) {of course, the program development can be much smaller}.
3. That complete column is then randomized; it's ok to put the randomized column in another column.
4. The letter sequence in cell C1 is searched for in the randomized column, and the number of times it occurs is output in D1.
5. Here, I show a 3-letter sequence, but I would like to be able to change that to other length words.
230209 Sequencing Share.xlsm | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | |||
1 | Letters | =RAND() | =CONCATENATE(A2,A3,A4) | =CONCATENATE(A4,A3,A2) | =IF(OR(F$1=C2,G$1=D2),1,0) | RIC | CIR | Results | |||
2 | F | 0.56356 | FLL | LLF | 0 | 0 | 0 | ||||
3 | L | 0.94582 | LLO | OLL | 0 | 0 | |||||
4 | L | 0.00596 | LOC | COL | 0 | 1 | |||||
5 | O | 0.04993 | OCF | FCO | 0 | 2 | |||||
6 | C | 0.79065 | CFA | AFC | 0 | 0 | |||||
7 | F | 0.86092 | FAG | GAF | 0 | 0 | |||||
8 | A | 0.19480 | AGP | PGA | 0 | 0 | |||||
9 | G | 0.58370 | GP | PG | 0 | 0 | |||||
10 | P | 0.33145 | P | P | 0 | 1 | |||||
11 | 1 | ||||||||||
12 | |||||||||||
Word Probability |
Cell Formulas | ||
---|---|---|
Range | Formula | |
B2:B10 | B2 | =RAND() |
C2:C10 | C2 | =CONCATENATE(A2,A3,A4) |
D2:D10 | D2 | =CONCATENATE(A4,A3,A2) |
E2:E10 | E2 | =IF(OR(F$1=C2,G$1=D2),1,0) |
F2 | F2 | =SUM(E2:E17) |
F3:F10 | F3 | =IF(E3=1,1,"") |
Cells with Conditional Formatting | ||||
---|---|---|---|---|
Cell | Condition | Cell Format | Stop If True | |
E9:E10 | Cell Value | >0 | text | NO |
E1:E8,E11:E1048576 | Cell Value | >0 | text | NO |
F:F | Cell Value | >">0" | text | NO |
Excel 2013That still does not tell us what Excel version you are using. Refer to post #5
Thanks. Can you now put it in your account details so that it is always readily available for helpers to refer to like this?Excel 2013