Hi,
The simplest way is to creat an array of command buttons indexed from 0 to 8 and then paste the below code in the code view. First you will have to:
- Create a button and name it "cmd".
- Create array of 9 buttons named "cmdch". (to create an array of buttons create 1 button, name it, set index property to 0, copy and paste it 8 times and you will get 9 buttons having the same name with different indexes).
- Paste the below code:
Dim Coun, Coun1 As Integer 'COUNTERSDim Cap(9) As String 'ARRAY FOR CAPTIONSDim Temp As String 'VARIABLE TO STORE RANDOM VALUE INPrivate Sub cmd_Click()'EMPTY ANY AVAILABLE CAPTIONS For Coun1 = 0 To 8
Me.cmdCh(Coun1).Caption = ""
Next 'FILL THE ARRAY WITH THE CAPTIONS (1...8 , X)
For Coun = 1 To UBound(Cap) - 1
Cap(Coun) = Coun
Next
Cap(9) = "X" '--------------------------
For Coun1 = 0 To 8
E:'GENERATE A RANDOM INDEX FOR THE ARRAY AND GET ITS CORRESPONDING VALUE Temp = Cap(Round(Rnd() * 9))
For Coun = 0 To 8 'IF CAPTION IS ALREADY AVAILABLE GENERATE ANOTHER ONE If Temp = Me.cmdCh(Coun).Caption Then GoTo E
Next'ASSIGN VALUE TO CAPTION Me.cmdCh(Coun1).Caption = Temp
Next
End Sub
Private Sub Form_Load()
End SubHope this work for you.
Enter your message below
Sign in or Join us (it's free).