generate 4 - digits pin of same characters or digits

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I want a way to generate any of these numbers at a time:
1234
0000
1111
2222
3333
4444
5555
6666
7777
8888
9999

I have this line below here which can check for the availability of those pins . I have been staring at it for a while now hoping to find a way around it but no success yet.
I need tech support.

Code:
Select Case MyInput
        Case String (4, Left(MyInput, 1)), "1234"
         MsgBox "Found"
        Case Else
          MsgBox "Not Found"
End Select

I have been able to use an array with Rand Between function to get it like this

Code:
GetPin = MyArray(Application. RandBetween(LBound(MyArray), UBound(MyArray)))

But instead of hard coding the list of possible pins into the array, I am looking for a way to pick or generate one of such pins.

I just don't want them appear in plain text in my code.

Thanks in advance
 
How about something like this?

Code:
Function ReturnPin() As String
    Dim i As Integer, i2 As Integer
    Randomize
    i = Rnd * 10
    i2 = Rnd * 9
    Select Case i
        Case 10
            i = Rnd * 6
            ReturnPin = i & i + 1 & i + 2 & i + 3
        Case Else
            ReturnPin = i & i & i2 & i2
    End Select
End Function

Hahaha:mad:

It would have taken me millennia to figure this logic out!!

Working very great

Now all questions exhausted.

Have a great time
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You might be interested in this alternative construction for shknbk2's function (Message #10 )...
Code:
Function ReturnPin() As String
  Dim i As Integer, i2 As Integer
  i = [RANDBETWEEN(0,10)]
  If i < 10 Then i2 = Evaluate("MID(""0123456789"",RANDBETWEEN(1,9),1)")
  ReturnPin = Evaluate("IF(" & i & "<10,TEXT(" & i & i & i2 & i2 & ",""0000""),MID(""0123456789"",RANDBETWEEN(1,6),4))")
End Function
In another thread where you were testing the PIN's, you indicated that all four digits being the same was an invalid PIN. If you need this function to not return such numbers, then use this version of my function instead...
Code:
Function ReturnPin() As String
  Dim i As Integer, i2 As Integer
  i = [RANDBETWEEN(0,10)]
  If i < 10 Then i2 = Evaluate("MID(SUBSTITUTE(""0123456789""," & i & ",""""),RANDBETWEEN(1,9),1)")
  ReturnPin = Evaluate("IF(" & i & "<10,TEXT(" & i & i & i2 & i2 & ",""0000""),MID(""0123456789"",RANDBETWEEN(1,6),4))")
End Function
 
Upvote 0
Okay cool. For now I am interested in the first part of the code you provided. I will later see what I can do with the second part. For now I need those same numbers in place

It's seems to randomly select the various numbers cooler. Almost even for the "abcd", "aaaa", "aabb" formats.

Shknbk's version favoured the "aabb" than the others:cool:

PS.
Did you intentionally leave out the

Randomize key word?
 
Last edited:
Upvote 0
PS.
Did you intentionally leave out the

Randomize key word?
Yes... you only need the Randomize statement when you use VB's Rnd function... I used Excel's RANDBETWEEN function instead which does not require the Randomize statement.
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

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