It´s probably a too easy question for a forum, but I´m kinda hard stuck and my prog skills are very limited. After submitting a userform in vba, I want the program to check if the user made a logic mistake. There are several CheckBoxes and a combination of selections is possible, but not all combinations are allowed. So I have written the following code to prevent the user from submitting the form with selecting two specific CheckBoxes. But the program completely ignores this if-statement and no matter what CheckBoxes are selected, nothing changes.
If userform1.CheckBoxOption3.Value = True And userform1.CheckBoxOption7.Value = True Then
MsgBox "This Combination is not possible!", vbCritical
Exit Sub
End If
Am I missing something obvious?
Edit: Additional Code:
Private Sub CmdFertig_Click()
Worksheets("sheets1").Range("I22").Value = userform1.TextBox1.Value & "°"
Worksheets("sheets1").Range("I13").Value = userform1.TextBox2.Value & "°"
Worksheets("sheets1").Range("E17").Value = userform1.TextBox3.Value & "°"
If userform1.CheckBox1.Value = True Then
Worksheets("sheets1").Range("g24").Value = userform1.TextBox1.Value & "°"
End If
If userform1.CheckBox2.Value = False Then
Worksheets("sheets1").Range("f24").Value = ""
Worksheets("sheets1").Range("f25").Value = ""
End If
If userform1.CheckBox3.Value = True Then
Worksheets("sheets1").Range("g25").Value = "Wechselseitig"
End If
If userform1.CheckBox5.Value = True Then
Worksheets("sheets1").Range("g25").Value = "Einseitig"
End If
If userform1.CheckBox7.Value = True Then
Worksheets("sheets1").Range("h25").Value = "Im UZ voreilend"
End If
If userform1.CheckBox3.Value = True And userform1.CheckBox7.Value = True Then
MsgBox "This Combination is not possible!", vbCritical
Exit Sub
End If
userform1.Hide
Worksheets("sheets1").ExportAsFixedFormat _
Type:=xlTypePDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
Filename:=userform1.Text1.Value & ".pdf", _
OpenAfterPublish:=True
Dim sPath As String
sPath = "O:\F1\completed\"
With Worksheets("sheets1")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sPath & userform1.TextBox1.Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
SetAttr sPath & userform1.TextBox1.Value & ".pdf", vbReadOnly
End Sub
CodePudding user response:
As a first step like suggestd in my comments I would remove the references to the default instance in the code itself. Like written here
A UserForm is essentially a class module with a designer and a VB_PredeclaredId attribute. That PredeclaredId means VBA is automatically creating a global-scope instance of the class, named after that class.
UserForm1.Show at the call site, where UserForm1 isn’t a local variable but the “hey look it’s free” default instance, which means you’re using an object without even realizing it (at least without New-ing it up yourself) – and you’re storing state that belongs to a global instance, which means you’re using an object but without the benefits of object-oriented programming.
So removing all references to the default instance in the code might already help. For the posted code
Private Sub CmdFertig_Click()
Worksheets("sheets1").Range("I22").Value = TextBox1.Value & "°"
Worksheets("sheets1").Range("I13").Value = TextBox2.Value & "°"
Worksheets("sheets1").Range("E17").Value = TextBox3.Value & "°"
If CheckBox1.Value = True Then
Worksheets("sheets1").Range("g24").Value = TextBox1.Value & "°"
End If
If CheckBox2.Value = False Then
Worksheets("sheets1").Range("f24").Value = ""
Worksheets("sheets1").Range("f25").Value = ""
End If
If CheckBox3.Value = True Then
Worksheets("sheets1").Range("g25").Value = "Wechselseitig"
End If
If CheckBox5.Value = True Then
Worksheets("sheets1").Range("g25").Value = "Einseitig"
End If
If CheckBox7.Value = True Then
Worksheets("sheets1").Range("h25").Value = "Im UZ voreilend"
End If
If CheckBox3.Value = True And CheckBox7.Value = True Then
MsgBox "This Combination is not possible!", vbCritical
Exit Sub
End If
Hide
Worksheets("sheets1").ExportAsFixedFormat _
Type:=xlTypePDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
Filename:=Text1.Value & ".pdf", _
OpenAfterPublish:=True
Dim sPath As String
sPath = "O:\F1\completed\"
With Worksheets("sheets1")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sPath & TextBox1.Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
SetAttr sPath & TextBox1.Value & ".pdf", vbReadOnly
End Sub
I also suggest to read about