+ Reply to Thread
Results 1 to 10 of 10

UserForms staying functional

Hybrid View

  1. #1
    Forum Contributor wmorrison49's Avatar
    Join Date
    09-25-2007
    Posts
    267

    UserForms staying functional

    Hi everyone,
    I am trying to make a little game for a friend of mine. It picks a random number 1-1000 then he gets 10 chances to guess the number. After each guess, it tells him if the number is higher or lower. I have a userform that you put it your first guess, hit a button, and it tells you if the number is higher or lower. All the guess blanks and buttons are on the same UserForm. However, after you push the first button, the UserForm doesn't work anymore. How do I get it to stay functional the whole time?

    Thanks in advance.

  2. #2
    Forum Contributor wmorrison49's Avatar
    Join Date
    09-25-2007
    Posts
    267
    Actually, I am having problems with the UserForm reading the data right off the UserForm too. I have attached what I have so far. Feel free to make changes or whatever you want. Thank you guys very much.
    Attached Files Attached Files

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You don't declare Magicnumber as a variable and I can't see where you actually give MagicNumber a value. You also place the response to Guess2 in Response1. This works

    Option Explicit
    Dim MagicNumber As Long
    Private Sub CommandButton1_Click()
        With Me
            If .Guess1.Value = MagicNumber Then
                .Response1.Text = "YOU ARE AMAZING!"
            ElseIf .Guess1.Value > MagicNumber Then
                .Response1.Text = "The number is lower"
            ElseIf .Guess1.Value < MagicNumber Then
                .Response1.Text = "The number is higher"
            End If
        End With
    End Sub
    
    Private Sub CommandButton2_Click()
        With Me
            If .Guess2 = MagicNumber Then
                .Response2.Text = "YOU ARE AMAZING!"
            ElseIf .Guess2 > MagicNumber Then
                .Response2.Text = "The number is lower"
            ElseIf .Guess2 < MagicNumber Then
                .Response2.Text = "The number is higher"
            End If
        End With
    End Sub
    
    
    Private Sub UserForm_Initialize()
        MagicNumber = Timer / 1000
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Contributor wmorrison49's Avatar
    Join Date
    09-25-2007
    Posts
    267
    The code works great, so thanks. For my own understanding though, there are a couple things I was hoping you could clear up.
    1) What does Option Explicit do?
    2) What does With Me do?
    Last edited by wmorrison49; 10-09-2007 at 01:04 PM.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Option Explicit basically forces you to declare your variables, for a fuller explanation read this:
    http://www.excel-it.com/clear_code.htm

    Me is a keyword that refers in this case to the UserForm. In a workbook event or WorkSheet event it would refer to the Workbook or worksheet
    Last edited by royUK; 10-09-2007 at 01:14 PM.

  6. #6
    Forum Contributor wmorrison49's Avatar
    Join Date
    09-25-2007
    Posts
    267
    I have used Me before to hide UserForms (Me.Hide), so I have SOME familiarity with it, but not a lot. What does the With in front of it denote? Also, is there any easy way for it to prompt the user if they want to play again, and then reset everything?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1