+ Reply to Thread
Results 1 to 3 of 3

Userform setfocus does not work?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2014
    Posts
    37

    Userform setfocus does not work?

    Hi.
    I have a userform with 4 inputboxes. For one of them I want to compare the input to a range. This works. Then I have a loop that says if the input is not among the values (text and number) in the range, a variable gets a number. This works. If the input is not in the range, a message box says that the input does not exist, and returns to the userform. This works. BUT here is the strange part; I then want the .setfocus to be in the same textbox, but this does not happen? The next textbox is highlighted...I've tried several solutions, but can't fint the one???!!!

    Here are the full codes to the userform;
    Explanations:
    formMastnummer is the textbox I'm talking about.
    formFriksjonsvinkel_en is another textbox
    formGrunnvannstand_en is another textbox
    formOppfylling_en is another textbox
    Beregningsinput_en is the userform.

    I copied all the codes, maybe some of the other codes interfere with the one I want to work.

    As you can see, I have setfocus in the same if sentence as the msgbox, (marked with <----- below), but .setfocus does not work, while the msgbox does...?

    (Btw: no error code are displayed)

    Private Sub formMastnummer_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    Dim formOmr As Range
    Dim fMastNr As String
    Dim fSisteRad As Long
    Dim formTeller As Integer
    
    Dim rs As Worksheet
    Set rs = Sheets("Reaction")
    
    fSisteRad = rs.Range("A" & Rows.Count).End(xlUp).Row
    Set formOmr = Range("A9", "A" & fSisteRad)
    
    fMastNr = Beregningsinput_en.formMastnummer
        
    
    formTeller = 9
        Do
    
            If Application.Proper(fMastNr) = Application.Proper(rs.Range("A" & formTeller)) Then
                sMastRad = rs.Range("A" & formTeller).Row
                TesT = 1
                Exit Do
            End If
    
            formTeller = formTeller + 1
            TesT = 2
        Loop Until formTeller = fSisteRad
        
            If TesT = 2 Then
                MsgBox "Mastnummer finnes ikke" '<----------DOES WORK
                With Me.formMastnummer
                    .SetFocus '<-----------------------------------DOES NOT WORK?
                    .Value = vbNullString
    '                .SelStart = 0
                End With
            End If
    
    End Sub
    
    
    
    
    
    Private Sub Friksjonsvinkel_en_Change()
    
        If TypeName(Me.ActiveControl) = "TextBox" Then
            With Me.ActiveControl
                If Not IsNumeric(.Value) And .Value <> vbNullString Then
                    MsgBox "Bruk kun tall"
                    .SetFocus
                    .Value = vbNullString
                End If
            End With
        End If
    
    End Sub
    
    Private Sub Oppfylling_en_Change()
    
        If TypeName(Me.ActiveControl) = "TextBox" Then
            With Me.ActiveControl
                If Not IsNumeric(.Value) And .Value <> vbNullString Then
                    MsgBox "Bruk kun tall"
                    .SetFocus
                    .Value = vbNullString
                End If
            End With
        End If
    
    End Sub
    
    Private Sub Avbryt_en_Click()
    
    Unload Beregningsinput_en
    AvbrytBeregningerEn = 1
    
    End Sub
    
    Private Sub Beregning_en_Click()
        
        If Me.formMastnummer.Value = "" Then
            MsgBox "Fyll inn mastnummer"
            Me.formMastnummer.SetFocus
            Exit Sub
        End If
        
        If Me.formFriksjonsvinkel_en.Value = "" Then
            MsgBox "Fyll inn verdi for friksjonsvinkel"
            Me.formFriksjonsvinkel_en.SetFocus
            Exit Sub
        End If
        
        If Me.formGrunnvannstand_en.Value = "" Then
            MsgBox "Fyll inn verdi for grunnvannstand"
            Me.formGrunnvannstand_en.SetFocus
            Exit Sub
        End If
        
        If Me.formOppfylling_en.Value = "" Then
            MsgBox "Fyll inn verdi for oppfylling"
            Me.formOppfylling_en.SetFocus
            Exit Sub
        End If
        
        MastInput = Beregningsinput_en.formMastnummer
        nyo = CDbl(Beregningsinput_en.formOppfylling_en)
        nyv = CDbl(Beregningsinput_en.formGrunnvannstand_en)
        nyf = CDbl(Beregningsinput_en.formFriksjonsvinkel_en)
        
        Unload Beregningsinput_en
        AvbrytBeregningerEn = 2
        
        
    End Sub
    
    Private Sub formFriksjonsvinkel_en_Change()
    
        If TypeName(Me.ActiveControl) = "TextBox" Then
            With Me.ActiveControl
                If Not IsNumeric(.Value) And .Value <> vbNullString Then
                    MsgBox "Bruk kun tall"
                    .SetFocus
                    .Value = vbNullString
                End If
            End With
        End If
    
    End Sub
    
    
    
    
    Private Sub UserForm_Initialize()
    
       formMastnummer.SetFocus
          
    End Sub

    Thanks in advance!!!

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Userform setfocus does not work?

    You do not need .SetFocus, you must only add
    Cancel = True
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Registered User
    Join Date
    05-22-2014
    Posts
    37

    Re: Userform setfocus does not work?

    Are you kidding me...that easy!

    THANKS!!!!!! saved my day!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] SetFocus on a Userform TextBox
    By bibu in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-22-2015, 05:18 PM
  2. Excel UserForm SetFocus
    By gopal baheti in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2015, 05:42 AM
  3. Multiple .SetFocus in UserForm does not work
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2014, 10:21 PM
  4. [SOLVED] SetFocus to another tab on a multi tab userform
    By abordeau in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-21-2014, 01:08 PM
  5. SetFocus doesn't work properly on a Userform on ComboBoxes and TextBoxes
    By pimre in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-15-2013, 03:15 PM
  6. question setfocus on a userform
    By GregJG in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-27-2007, 11:28 AM
  7. [SOLVED] setfocus does not work as expected
    By wgoldfarb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-17-2005, 03:06 AM

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