Home All Groups Group Topic Archive Search About
Author
2 Jun 2005 9:04 PM
HLong
I have a program that calculates some numbers.  Now, it there a easy way of
determine if any of the digits in the result is not repeated? For example, if
my result was 156285640, I would like to know that number 6 is twice in the
result.  Any help would be appreciated.

Author
2 Jun 2005 9:57 PM
Saga
Perhaps something like:

  Dim strX As String
  Dim strDigit As String

  strDigit = "6"
  strX = "156285640"

  MsgBox strDigit & " occurs " & _
    Len(strX) - Len(Replace(strX, strDigit, "")) & _
    " times."

Good luck!
Saga

Show quoteHide quote
"HLong" <HL***@discussions.microsoft.com> wrote in message
news:73DB02F9-D488-4CAF-9804-9483D7854C91@microsoft.com...
>I have a program that calculates some numbers.  Now, it there a easy
>way of
> determine if any of the digits in the result is not repeated? For
> example, if
> my result was 156285640, I would like to know that number 6 is twice
> in the
> result.  Any help would be appreciated.
Author
2 Jun 2005 11:24 PM
Duane Bozarth
Saga wrote:
Show quoteHide quote
>
> Perhaps something like:
>
>   Dim strX As String
>   Dim strDigit As String
>
>   strDigit = "6"
>   strX = "156285640"
>
>   MsgBox strDigit & " occurs " & _
>     Len(strX) - Len(Replace(strX, strDigit, "")) & _
>     " times."
>
> Good luck!
> Saga
>
> "HLong" <HL***@discussions.microsoft.com> wrote in message
> news:73DB02F9-D488-4CAF-9804-9483D7854C91@microsoft.com...
> >I have a program that calculates some numbers.  Now, it there a easy
> >way of
> > determine if any of the digits in the result is not repeated? For
> > example, if
> > my result was 156285640, I would like to know that number 6 is twice
> > in the
> > result.  Any help would be appreciated.

For the more general case, convert to string w/ Format$(), take each
character and sort them, then look for repeated sequences.
Author
3 Jun 2005 3:48 AM
BeastFish
> "HLong" <HL***@discussions.microsoft.com> wrote in message
> news:73DB02F9-D488-4CAF-9804-9483D7854C91@microsoft.com...
> >I have a program that calculates some numbers.  Now, it there a easy
> >way of
> > determine if any of the digits in the result is not repeated? For
> > example, if
> > my result was 156285640, I would like to know that number 6 is twice
> > in the
> > result.  Any help would be appreciated.


Here's something I just threw together on the quick.  Just adding to the
stew <g>

Private Function HowMany(ByVal Numb As String, ByVal Digit As String) As
Integer
    Dim iPos As Long, hPos As Long
    hPos = 1
    Do
       iPos = InStr(hPos, Numb, Digit)
       If iPos > 0 Then
          hPos = iPos + 1
          HowMany = HowMany + 1
         End If
    Loop Until iPos = 0
End Function
Author
3 Jun 2005 11:28 AM
HLong
Thanks to all.  I used your function and it worked great.

Show quoteHide quote
"BeastFish" wrote:

> > "HLong" <HL***@discussions.microsoft.com> wrote in message
> > news:73DB02F9-D488-4CAF-9804-9483D7854C91@microsoft.com...
> > >I have a program that calculates some numbers.  Now, it there a easy
> > >way of
> > > determine if any of the digits in the result is not repeated? For
> > > example, if
> > > my result was 156285640, I would like to know that number 6 is twice
> > > in the
> > > result.  Any help would be appreciated.
>
>
> Here's something I just threw together on the quick.  Just adding to the
> stew <g>
>
> Private Function HowMany(ByVal Numb As String, ByVal Digit As String) As
> Integer
>     Dim iPos As Long, hPos As Long
>     hPos = 1
>     Do
>        iPos = InStr(hPos, Numb, Digit)
>        If iPos > 0 Then
>           hPos = iPos + 1
>           HowMany = HowMany + 1
>          End If
>     Loop Until iPos = 0
> End Function
>
>
>
>
Author
4 Jun 2005 9:44 PM
Jim Edgar
<snip>
Show quoteHide quote
> Here's something I just threw together on the quick.  Just adding to the
> stew <g>
>
> Private Function HowMany(ByVal Numb As String, ByVal Digit As String) As
> Integer
>     Dim iPos As Long, hPos As Long
>     hPos = 1
>     Do
>        iPos = InStr(hPos, Numb, Digit)
>        If iPos > 0 Then
>           hPos = iPos + 1
>           HowMany = HowMany + 1
>          End If
>     Loop Until iPos = 0
> End Function
>
>
>

How about a good old one-liner:

Private Sub Form_Load()
    Dim sNum As String
    Dim sDig As String
    sNum = "123456234634257625312354"
    sDig = "2"
    MsgBox HowMany(sNum, sDig)
End Sub

Private Function HowMany(ByVal sNumber As String, _
                                             ByVal sDigit As String) _
                                             As Integer
    HowMany = UBound(Split(sNumber, sDigit))
End Function

Jim Edgar
Author
3 Jun 2005 1:25 AM
Larry Serflaten
"HLong" <HL***@discussions.microsoft.com> wrote
> I have a program that calculates some numbers.  Now, it there a easy way of
> determine if any of the digits in the result is not repeated? For example, if
> my result was 156285640, I would like to know that number 6 is twice in the
> result.  Any help would be appreciated.

See if this might be useful:

HTH
LFS

Private Sub CheckDigits(ByRef Text As String)
Dim Count(0 To 255) As Integer
Dim idx As Long, ch As Long

  For idx = 1 To Len(Text)
    ch = Asc(Mid$(Text, idx, 1))
    Count(ch) = Count(ch) + 1
  Next

For idx = "0" To "9"
  Debug.Print idx, Count(idx)
Next

End Sub
Author
3 Jun 2005 9:56 AM
Tony Proctor
It fails if there's a DBCS in the text Larry  :-)

        Tony Proctor

Show quoteHide quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:uTzlQq9ZFHA.3132@TK2MSFTNGP09.phx.gbl...
>
> "HLong" <HL***@discussions.microsoft.com> wrote
> > I have a program that calculates some numbers.  Now, it there a easy way
of
> > determine if any of the digits in the result is not repeated? For
example, if
> > my result was 156285640, I would like to know that number 6 is twice in
the
> > result.  Any help would be appreciated.
>
> See if this might be useful:
>
> HTH
> LFS
>
> Private Sub CheckDigits(ByRef Text As String)
> Dim Count(0 To 255) As Integer
> Dim idx As Long, ch As Long
>
>   For idx = 1 To Len(Text)
>     ch = Asc(Mid$(Text, idx, 1))
>     Count(ch) = Count(ch) + 1
>   Next
>
> For idx = "0" To "9"
>   Debug.Print idx, Count(idx)
> Next
>
> End Sub
>