Consulting

Results 1 to 7 of 7

Thread: Textbox Loop

  1. #1
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location

    Textbox Loop

    hi

    im making little game. about skills and hotkeys. Picture coming and press short keys.

    create userform1
    start and textbox

    press start button timer is starting
    and findin a key

    after questin for loop how many times press a key(5-10-15-20-50-100 whatever)

    and there is my questin. i dont trigger for loop. key is always same not change

    pls help me
    thx


    my codes

    Dim n As Integer
    Dim shortkeys As Variant
    Dim correct, incorrect, sum As String
    Dim StartTime As Double
    Dim val As String
    
    
    Private Sub CommandButton1_Click()
    
      StartTime = Timer
     
     Call keys
      
    End Sub
    
    
    Sub keys()
    
    shortkeys = Array("m", "a", "g", "b", "h", "p", "y", "o", "s", "g", "r", "u", "l", "u", "u", "d", "e", "c", "f", "d", "f", "k", "p", "f", "h", "d", "m", "f", "p", "c", "w", "r", "s", "w", "o", "i", "b", "t", "c", "e", "g", "g", "s", "s", "s", "g", "g", "g", "p", "p", "p", "a", "a", "a", "m", "m", "m", "b", "l", "l", "d", "a", "t", "t", "t", "t", "l", "m", "r", "h", "g", "b", "c", "s", "c", "a", "w", "r", "b", "s", "u", "f", "g", "c", "g", "t", "n", "r", "v", "l", "u", "b", "n", "f", "b", "x", "a", "b", "w", "r", "t", "m", "t", "c", "b", "v", "l", "t", "d", "v", "r")
    
    n = Application.WorksheetFunction.RandBetween(LBound(shortkeys), UBound(shortkeys))
    
    'Show keys
    Label7.Caption = shortkeys(n)
    
    val = InputBox("Enter repeat number")
    
    End Sub
    
    
    Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    
    For i = 1 To val
    
    
        If TextBox1.Text = shortkeys(n) Then
        
    correct = correct + 1
    
        Else
        
    incorrect = incorrect + 1
    
        End If
    
    
    TextBox1.Text = ""
    sum = correct + incorrect
    percentage = correct / sum
    
    Label6.Caption = Format(percentage, "Percent")
    Label4.Caption = correct
    
    Label1.Caption = Format((Timer - StartTime) / 86400, "hh:mm:ss")
    
    
    Next i
    
    
    End Sub
    Attached Files Attached Files

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Initialize routine added
    Option Explicit
    Dim n As Integer
    Dim shortkeys As Variant
    Dim correct, incorrect, sum As String
    Dim StartTime As Double
    Dim val As String
    
    Private Sub CommandButton1_Click()
        StartTime = Timer
        Call keys
    End Sub
    
    
    Sub keys()
        shortkeys = Array("m", "a", "g", "b", "h", "p", "y", "o", "s", "g", "r", "u", "l", "u", "u", "d", "e", "c", "f", "d", "f", "k", "p", "f", "h", "d", "m", "f", "p", "c", "w", "r", "s", "w", "o", "i", "b", "t", "c", "e", "g", "g", "s", "s", "s", "g", "g", "g", "p", "p", "p", "a", "a", "a", "m", "m", "m", "b", "l", "l", "d", "a", "t", "t", "t", "t", "l", "m", "r", "h", "g", "b", "c", "s", "c", "a", "w", "r", "b", "s", "u", "f", "g", "c", "g", "t", "n", "r", "v", "l", "u", "b", "n", "f", "b", "x", "a", "b", "w", "r", "t", "m", "t", "c", "b", "v", "l", "t", "d", "v", "r")
        n = Application.WorksheetFunction.RandBetween(LBound(shortkeys), UBound(shortkeys))
        'Show keys
        Label7.Caption = shortkeys(n)
       
    End Sub
    Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        Dim i As Long, percentage As Single
        For i = 1 To val
            If TextBox1.Text = shortkeys(n) Then
                correct = correct + 1
            Else
                incorrect = incorrect + 1
            End If
            TextBox1.Text = ""
            sum = correct + incorrect
            percentage = correct / sum
            Label6.Caption = Format(percentage, "Percent")
            Label4.Caption = correct
            Label1.Caption = Format((Timer - StartTime) / 86400, "hh:mm:ss")
        Next i
    End Sub
    
    
    Private Sub UserForm_Initialize()
        val = InputBox("Enter repeat number")
        keys
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location
    Thx but loop is dont trigger and always same key

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I don't understand the rules of the game.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Label1.Caption = Mid("magbhpyosgruluudecfdfkpfhdmfpcwrswoibtceggsssgggpppaaammmblldattttlmrhgbcscawrbsufgcgtnrvlubnfbxabwrtmtcbvltdvr", Rnd * 110, 1)

  6. #6
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location
    i change my code
    game rule is label7 or shortkeys(n) must be same with textbox1.text (If TextBox1.Text = shortkeys(n) Then)

    but im so confused. My loop is dont trigger. and score is same .

    Option Explicit
    Dim n As Integer
    Dim shortkeys As Variant
    Dim correct, incorrect, sum As String
    Dim StartTime As Double
    Dim val As String
    Sub keys()
        shortkeys = Array("m", "a", "g", "b", "h", "p", "y", "o", "s", "g", "r", "u", "l", "u", "u", "d", "e", "c", "f", "d", "f", "k", "p", "f", "h", "d", "m", "f", "p", "c", "w", "r", "s", "w", "o", "i", "b", "t", "c", "e", "g", "g", "s", "s", "s", "g", "g", "g", "p", "p", "p", "a", "a", "a", "m", "m", "m", "b", "l", "l", "d", "a", "t", "t", "t", "t", "l", "m", "r", "h", "g", "b", "c", "s", "c", "a", "w", "r", "b", "s", "u", "f", "g", "c", "g", "t", "n", "r", "v", "l", "u", "b", "n", "f", "b", "x", "a", "b", "w", "r", "t", "m", "t", "c", "b", "v", "l", "t", "d", "v", "r")
        n = Application.WorksheetFunction.RandBetween(LBound(shortkeys), UBound(shortkeys))
         'Show keys
        Label7.Caption = shortkeys(n)
         
    End Sub
    
    Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
            Dim i As Long, percentage As Single
    
            For i = 1 To val
            Call keys
    
            If TextBox1.Text = shortkeys(n) Then
                correct = correct + 1
            Else
                incorrect = incorrect + 1
            End If
            TextBox1.Text = ""
            sum = correct + incorrect
            
            Label4.Caption = correct
            percentage = correct / sum
            Label6.Caption = Format(percentage, "Percent")
            Label1.Caption = Format((Timer - StartTime) / 86400, "hh:mm:ss")
        Next i
       
    End Sub
     
     
    Private Sub UserForm_Initialize()
        val = InputBox("Enter repeat number")
        StartTime = Timer
        
    End Sub

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Still don't know what you're trying to achieve. Best guess

    Option Explicit
    Dim n As Integer
    Dim shortkeys As Variant
    Dim correct, incorrect, sum As String
    Dim StartTime As Double
    Dim val As Long, v As Long
    Sub keys()
        shortkeys = Array("m", "a", "g", "b", "h", "p", "y", "o", "s", "g", "r", "u", "l", "u", "u", "d", "e", "c", "f", "d", "f", "k", "p", "f", "h", "d", "m", "f", "p", "c", "w", "r", "s", "w", "o", "i", "b", "t", "c", "e", "g", "g", "s", "s", "s", "g", "g", "g", "p", "p", "p", "a", "a", "a", "m", "m", "m", "b", "l", "l", "d", "a", "t", "t", "t", "t", "l", "m", "r", "h", "g", "b", "c", "s", "c", "a", "w", "r", "b", "s", "u", "f", "g", "c", "g", "t", "n", "r", "v", "l", "u", "b", "n", "f", "b", "x", "a", "b", "w", "r", "t", "m", "t", "c", "b", "v", "l", "t", "d", "v", "r")
        n = Application.WorksheetFunction.RandBetween(LBound(shortkeys), UBound(shortkeys))
         'Show keys
        Label7.Caption = shortkeys(n)    
    End Sub
     
    Private Sub TextBox1_Change()
        Dim i As Long, percentage As Single
        v = v + 1
        If v > val Then
            MsgBox "No more attempts"
            Exit Sub
        End If
         
        If TextBox1.Text = shortkeys(n) Then
            correct = correct + 1
        Else
            incorrect = incorrect + 1
        End If
        
        With Me.TextBox1
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        Call keys
        sum = correct + incorrect
        Label4.Caption = correct
        percentage = correct / sum
        Label6.Caption = Format(percentage, "Percent")
        Label1.Caption = Format((Timer - StartTime) / 86400, "hh:mm:ss")
    End Sub
     
    Private Sub UserForm_Initialize()
        val = CLng(InputBox("Enter repeat number"))
        StartTime = Timer
        Call keys
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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