Macro for detect palindromes and repeats in letters/numbers string

  • Thread starter Luciano Paulino da Silva
  • Start date
B

Bernie Deitrick

OK for for palindromes, but the string that you posted

AABBAABB

has the repeats

AA
AAB
AABB
AB
ABB
BB

not just AA and BB...

If that is what you actually want, then I will modify the code to do so.

HTH,
Bernie
MS Excel MVP
 
L

Luciano Paulino da Silva

Dear Bernie,
Yes, you are correct that there are other repeats in this string.
Thanks in advance,
Luciano
 
L

Luciano Paulino da Silva

It is exactly that I want.
Thank you,
Luciano

Dear Bernie,
Yes, you are correct that there are other repeats in this string.
Thanks in advance,
Luciano
 
L

Luciano Paulino da Silva

Yes, it is exactly what I want. Is it possible to you modify the code
for me?
Thanks in advance,
Luciano
 
B

Bernie Deitrick

Luciano,

Same drill: Enter

=REPEATS(A1) into cells A2:B??? using Ctrl-Shift-Enter.

HTH,
Bernie
MS Excel MVP

Option Explicit

Function Repeats(strBig As String) As Variant
Dim FoundRpts() As String
Dim RptCount As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim x As Integer
Dim RptExists As Boolean

RptCount = 1
ReDim FoundRpts(1 To 2, 1 To 2)
For i = 1 To Len(strBig) - 1
For j = 2 To Len(strBig) - i + 1
x = isRpt(Mid(strBig, i, j), strBig, i + 1)
If x > 0 Then
If RptCount = 1 Then
FoundRpts(1, 2) = Mid(strBig, i, j)
FoundRpts(2, 2) = x
RptCount = 2
Else
RptExists = False
For k = 2 To UBound(FoundRpts, 2)
If FoundRpts(1, k) = Mid(strBig, i, j) Then
RptExists = True
End If
Next k
If Not RptExists Then
ReDim Preserve FoundRpts(1 To 2, 1 To RptCount + 1)
FoundRpts(1, RptCount + 1) = Mid(strBig, i, j)
FoundRpts(2, RptCount + 1) = x
MsgBox FoundRpts(1, RptCount + 1) & " " & x
RptCount = RptCount + 1
End If
End If
End If
Next j
Next i

FoundRpts(1, 1) = "Repeats found:"
FoundRpts(2, 1) = RptCount - 1
Repeats = Application.Transpose(FoundRpts)

End Function
Function isRpt(strRpt As String, strPar As String, i As Integer) As Integer
isRpt = 0
If InStr(i, strPar, strRpt) > 0 Then
isRpt = (Len(Mid(strPar, i, Len(strPar))) - _
Len(Replace(Mid(strPar, i, Len(strPar)), strRpt, ""))) / _
Len(strRpt) + 1
End If
End Function
 
L

Luciano Paulino da Silva

Dear Bernie,
I tried a lot to run the code but it is not working. Have you some
idea about what could it be happening?
I just receive the error: #NAME! for all cells in the interval.
Thanks in advance,
Luciano
 
B

Bernie Deitrick

Luciano,

Did you check your typing?
Did you put it into a standard codemodule and not a worksheet codemodule?
Did you copy the code into a codemodule in the correct workbook?

It worked for me - let me know if you can't get it to work, and I will send
you a working version, if you gmail account is not a spoof.

Bernie

Dear Bernie,
I tried a lot to run the code but it is not working. Have you some
idea about what could it be happening?
I just receive the error: #NAME! for all cells in the interval.
Thanks in advance,
Luciano
 
B

Bernie Deitrick

And, last but not least, did you copy the code and paste it into your
codemodule? That is what I meant by "Same drill" copy the code below my
signature, paste it into a codemodule, etc.

Sorry for being too brief in my instructions.

Bernie


Dear Bernie,
I tried a lot to run the code but it is not working. Have you some
idea about what could it be happening?
I just receive the error: #NAME! for all cells in the interval.
Thanks in advance,
Luciano
 
L

Luciano Paulino da Silva

Dear Bernie,
Thank you very much for your attention.
Yes, I have performed all these operations, but I had created a new
sheet into the same file that it was the palindromes code.
Now, I created a new file and it is running. It is appearing a box
showing all detected repeats for which I have to press OK in order to
step the next one. Can I put it out?
Thank you very much for all your help,
Luciano
 
L

Luciano Paulino da Silva

Dear Bernie,
Thank you very much!
It is working. However, I have to sum the values bellow B3 in order to
get the total numbers of repeats observed that is different from the
total number of different repeats. Have you some idea why the sum
comand did not work?
Thanks in advance,
Luciano
 
B

Bernie Deitrick

I accidentally left in a debugging msgbox - just remove the line with the
msgbox statement:

MsgBox FoundRpts(1, RptCount + 1) & " " & x

Sorry about that,
Bernie
MS Excel MVP

Dear Bernie,
Thank you very much for your attention.
Yes, I have performed all these operations, but I had created a new
sheet into the same file that it was the palindromes code.
Now, I created a new file and it is running. It is appearing a box
showing all detected repeats for which I have to press OK in order to
step the next one. Can I put it out?
Thank you very much for all your help,
Luciano
 
B

Bernie Deitrick

The count that is returned automatically is the number of unique repeats,
not the total number of repeats. I chose to return the unique number of
repeats so that you can use that to figure out how many rows (unique number
+ 1) you need to include in your Ctrl-Shift-Entry function entry, if the
last cells are not NA# values.

Bernie
 
L

Luciano Paulino da Silva

Do not worry!
I saw that before you can send me the msg.
Thank you very much!
Luciano
 
L

Luciano Paulino da Silva

Dear Bernie,
I would like to thanks you for all this help. I'm currently running
absolutely all my strings with no problem and this is due to your
efforts.
Thanks you!
I'm currently with my new "problem" that I have posted yesterday...
Luciano
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top