none
Using control arrays to generate random numbers RRS feed

  • Question

  • For my culminating I am making a game which is like a picture puzzle except it uses number up to 11 and has one blank space to make the swap...point is to make the numbers in the correct order going from left to right. The numbers are in a 3 by 4 grid. This is what I have... I want the grid to be setup when the player launches the user form so i am generating random numbers the problem is i am not sure where to go from here and feel like the code is kinda messed up. Btw, I am just a grade 11 student who has just started programming so please try and keep your answers simple and easy for me to understand...I am not asking for you to do my work, just help out a newbie to get an idea of how to solve this problem! thank you so much and your help is much appreciated!

    Also every time I run it, it says type mismatch and highlights the "Controls" in line 7...So confused like why? is it a problem with the trigger???

    Dim abcd Dim lblarray(1 To 12)

    Private Sub UserForm_Activate() For abc = 1 To 12 Set lblarray = Controls("label") & lblarray Next abc For abcd = 1 To 12 Randomize Labelabcd.Caption = Int(Rnd * 11) + 1 Next abcd

    End sub



    Sunday, January 18, 2015 1:07 PM

All replies

  • I assume that you're using a VBA userform with labels.

    Let's say that you have labels Label1 through Label12.

    You cannot use something like Labeln where n is a variable with value between 1 and 12. VBA doesn't recognize that Label is fixed and n is variable.

    Instead, you can refer to the n-th label as

    Me.Controls("Label" & n)

    To assign the numbers 1 to 11 randomly to the labels, you have to make sure that you don't assign the same number twice. With a line such as

    Labelabcd.Caption = Int(Rnd * 11) + 1

    you run the risk of assigning the same number to several labels; moreover you won't leave any label blank.

    The best way to do it is to fill an array of 12 elementss with the numbers 1 through 11 and leave one element blank.

    Then shuffle the elements of the array, for example by exchanging each element of the array with a randomly chosen element.

    Finally, set the captions of the labels to the elements of the shuffled array.

    See if you can take it from there.

    I'll post the code below, but don't look at it if you want to try it out for yourself first.

     

     

     

     

     

     

     

    Private Sub UserForm_Initialize()
        Dim i As Long
        Dim j As Long
        Dim tmp As String
        Dim arr(1 To 12) As String
        ' Initialize the array with the numbers 1...11; arr(12) is left blank
        For i = 1 To 11
            arr(i) = CStr(i)
        Next i
        ' Shuffle the elements of the array
        Randomize
        ' Loop through the elements
        For i = 1 To 12
            ' Random element
            j = Int(Rnd * 12) + 1
            ' Exchange the i-th and j-th element
            tmp = arr(j)
            arr(j) = arr(i)
            arr(i) = tmp
        Next i
        ' Set the captions of the labels
        For i = 1 To 12
            Me.Controls("Label" & i).Caption = arr(i)
        Next i
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)


    Sunday, January 18, 2015 1:35 PM
  • I see Hans has already explained while I was preparing an example, but you might as well have it anyway.

    Paste the following into a new empty form, run it and click the form

    Dim mLblArray(1 To 12) As MSForms.Label
    
    Private Sub UserForm_Initialize()
    Dim i As Long
    Dim ctl As MSForms.Control
        For i = 1 To 12
            Set ctl = Me.Controls.Add("forms.label.1", "label" & i)
            With ctl
                .Left = 9
                .Top = (i - 1) * 24 + 3
                .Height = 18
                .Width = 30
                .BorderStyle = 1
            End With
        Next
        Me.Height = i * 24
        
        ' add the labels to the array
        For i = 1 To 12
            Set mLblArray(i) = Me.Controls("label" & i)
        Next
    
    End Sub
    
    Private Sub UserForm_Click()
    ' click the form
    Dim i As Long
        Randomize
        For i = 1 To 12
            mLblArray(i).Caption = "  " & Int(Rnd * 11) + 1
        Next
    End Sub

     
    Sunday, January 18, 2015 1:49 PM
    Moderator