By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,101 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,101 IT Pros & Developers. It's quick & easy.

Random function 1 - 4

P: 44
I need to create a random code to run a function 1 - 4. i.e. random number is 1 so it runs function 1 random number is 2 so it runs function 2. I would like to have this in VBA because that is all I have.

Thanks for your support.
Apr 3 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 5K+
P: 8,434
I need to create a random code to run a function 1 - 4. i.e. random number is 1 so it runs function 1 random number is 2 so it runs function 2. I would like to have this in VBA because that is all I have.
Generate a value into a variable using the rnd function, then use Select Case to make the decision.
Apr 3 '07 #2

P: 44
Generate a value into a variable using the rnd function, then use Select Case to make the decision.
When you say that do you mean something like this:

Expand|Select|Wrap|Line Numbers
  1. Select Case KeyCode
  2. Case vbKeyEscape
  3.     End
  4.     GO.Visible = False
  5. Case vbKeyLeft
  6.     G_Ship.Left = (G_Ship.Left - 18)
  7. Case vbKeyRight
  8.     GO.Visible = False
  9.     G_Ship.Left = (G_Ship.Left + 18)
  10. Case vbKeyUp
  11.     GO.Visible = False
  12.     G_Ship.Top = (G_Ship.Top - 18)
  13. Case vbKeyDown
  14.     GO.Visible = False
  15.     G_Ship.Top = (G_Ship.Top + 18)
  16. End Select
If so is their something I should put as the case. If not can you give me an example?
Apr 3 '07 #3

Expert 5K+
P: 8,434
When you say that do you mean something like this:
Expand|Select|Wrap|Line Numbers
  1. Select Case KeyCode
  2. Case vbKeyEscape
  3.     End
  4.     GO.Visible = False
  5. ...
  6. End Select
If so is their something I should put as the case. If not can you give me an example?
You said that you needed to "create a random code to run a function", not use a keypress. What I was suggesting was something like...
Expand|Select|Wrap|Line Numbers
  1. MyNumber = RND * 4 ' <-- Probably not quite correct.
  2. Select Case MyNumber
  3.   Case 1
  4.     Do function 1
  5.   Case 2
  6.     Do function 2
  7.   Case 3
  8.     Do function 3
  9.   Case 4 ' Or "Case Else"
  10.     Do function 4
  11. End Select
By the way, any statement following the End statement, such as the one I highlighted above, will not be executed. End causes the program to end immediately.
Apr 3 '07 #4

P: 44
You said that you needed to "create a random code to run a function", not use a keypress. What I was suggesting was something like...
Expand|Select|Wrap|Line Numbers
  1. MyNumber = RND * 4 ' <-- Probably not quite correct.
  2. Select Case MyNumber
  3.   Case 1
  4.     Do function 1
  5.   Case 2
  6.     Do function 2
  7.   Case 3
  8.     Do function 3
  9.   Case 4 ' Or "Case Else"
  10.     Do function 4
  11. End Select
By the way, any statement following the End statement, such as the one I highlighted above, will not be executed. End causes the program to end immediately.
I understand I was asking about how to do the case. I will be sure to take your example into consideration.
Apr 3 '07 #5

Expert 5K+
P: 8,434
I understand I was asking about how to do the case. I will be sure to take your example into consideration.
Cool.

Hope it works out.
Apr 3 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.