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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Here is a function that returns one of those pins randomly with only "1234" hard-coded. Is this anything like what you are looking for?

Code:
    GetPin = ReturnPin


Function ReturnPin() As String
    Dim i As Integer
    Randomize
    i = Rnd * 10
    Select Case i
        Case 10
             ReturnPin= "1234"
        Case Else
             ReturnPin= i & i & i & i
    End Select
End Function

I assume that in your code, GetPin is somehow related to MyInput.
 
Upvote 0
Here is a function that returns one of those pins randomly with only "1234" hard-coded. Is this anything like what you are looking for?

Code:
    GetPin = ReturnPin


Function ReturnPin() As String
    Dim i As Integer
    Randomize
    i = Rnd * 10
    Select Case i
        Case 10
             ReturnPin= "1234"
        Case Else
             ReturnPin= i & i & i & i
    End Select
End Function

I assume that in your code, GetPin is somehow related to MyInput.

Sure it is exactly like what I am looking for.

And simpler than I ever imagined.

Thanks for it
 
Upvote 0
@shknbk2,

Is there a way to add to the above code the numbers in the form:
0123
"1234"
2345
3456
Etc?

So that the hard coded 1234 part becomes the class of the numbers above?

If anyone else has a knowledge about this please help me out.

Regards
 
Upvote 0
Sure, this goes from 0123 to 6789.

Code:
[COLOR=#333333]Function ReturnPin() As String
[/COLOR]    Dim i As Integer
    Randomize
    i = Rnd * 6
    ReturnPin= i & i + 1 & i + 2 & i + 3
 [COLOR=#333333]End Function[/COLOR]
 
Upvote 0
Sure, this goes from 0123 to 6789.

Code:
[COLOR=#333333]Function ReturnPin() As String
[/COLOR]    Dim i As Integer
    Randomize
    i = Rnd * 6
    ReturnPin= i & i + 1 & i + 2 & i + 3
 [COLOR=#333333]End Function[/COLOR]

Cool . I did something like this but could not replace the 10. Thanks for the fix.

Is it possible to merge both of the codes you have supplied?

I am thinking of making the "case 10" part in the first code take the latest code you have provided but I don't know what to add or what to remove
 
Upvote 0
Sure.

Code:
Function ReturnPin() As String
    Dim i As Integer
    Randomize
    i = Rnd * 10
    Select Case i
        Case 10
            i = Rnd * 6
            ReturnPin = i & i + 1 & i + 2 & i + 3
        Case Else
            ReturnPin = i & i & i & i
    End Select
End Function
 
Upvote 0
Sure.

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

Great!!!!

I do have a bit of syntax issues. :cool:

Have a wonderful time
 
Upvote 0
Sure.

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

Hi

One last thing:

How do I get numbers like

0011
0022
.......
1100
1122
1133
........
2200
2211
2233
........

Added to the code we have so far?

I can do say
Case 0
ReturnPin = i & i & i + 1 & i + 1

Etc but how to update the above code with it is the headache now
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,128
Members
453,021
Latest member
Justyna P

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