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!!!
Bookmarks