Use same value for a variable in a sub

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have this function to randomly generate certain numbers. Then I am calling for its verification with another Sub. And display an alert. Then I found out that if there is a match, instead of the alert showing me the value for the match, it goes on to generate a new set of numbers from the random function.

The verification code
Code:
Sub TestMatch ()
If ReturnPin Mod 1111 = 0 Then
        MsgBox ReturnPin
End If
End Sub

The randomly generated code
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

Now what I need is to maintain the same variable value used for the if statement as the MsgBox alert.

Can someone pull it out for me?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
[color=darkblue]Sub[/color] TestMatch()
    [color=darkblue]Dim[/color] x
    x = ReturnPin
    [color=darkblue]If[/color] x Mod 1111 = 0 [color=darkblue]Then[/color]
            MsgBox x
    [color=darkblue]End[/color] [color=darkblue]If[/color]
End [color=darkblue]Sub[/color]
 
Upvote 0
Code:
[COLOR=darkblue]Sub[/COLOR] TestMatch()
    [COLOR=darkblue]Dim[/COLOR] x
    x = ReturnPin
    [COLOR=darkblue]If[/COLOR] x Mod 1111 = 0 [COLOR=darkblue]Then[/COLOR]
            MsgBox x
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]

Thanks
This got me thinking:confused:

Why didn't the x call the ReturnPin again?

Because initially I was thinking of something like "Static" a little bit of insight will be great:cool:
 
Upvote 0
I have this function to randomly generate certain numbers.
You probably should have asked this question in your other thread ("generate 4 - digits pin of same characters or digits"). Anyway, I posted code over there which eliminate a PIN where all the digits are the same from being generated, hence, you would not need the test you are looking to implement here. To save you and other reader's of this thread, here is the function I posted at the other thread...
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
Why would x call ReturnPin again? ReturnPin is only called when it's explicitly listed in the code, and it only listed once. I think you're overthinking this.
 
Upvote 0
Why would x call ReturnPin again? ReturnPin is only called when it's explicitly listed in the code, and it only listed once. I think you're overthinking this.

Hahaha sure. I am doing a bit of over thinking. I am now adapting to certain stuffs
 
Upvote 0
You probably should have asked this question in your other thread ("generate 4 - digits pin of same characters or digits"). Anyway, I posted code over there which eliminate a PIN where all the digits are the same from being generated, hence, you would not need the test you are looking to implement here. To save you and other reader's of this thread, here is the function I posted at the other thread...
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

I understand what you are saying. However I want all those pins generated. Because each class of pin has got its own function in my universe:).

I really appreciate your concerns:cool:
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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