GoldFusion
GoldFusion

Reputation: 149

Prepopulating userform failed gives "type mismatch" error

So I have a Userform that can either take information from the excel sheet and prepopulate the Userform or output Userform information back to the excel sheet. The issue took place when prepopulating the Userform from excel data.

enter image description here

As seen in the picture above, it's the Userform itself and after clicking on Prepopulate option I get the Run-time error "13": Type Mismatch. So when I debugged it, it went to line 5 line seen below which corresponds to the Prepopulate section of the code.

    With Sheets("DoNotPrint - Setup")
    If .Range("H28").value = "Chief Process Safety" Then
        ToggleButton1 = True
     Else
     If .Range("H28").value = "Senior Process Safety" Or "Interm Process Safety" Or "" Then
        ToggleButton1 = False
    End If
    End If
End With

enter image description here

The picture above is what it looks like on excel where the user inputs the fields and the last field is a drop down list to choose 1 from the three options that is linked to the piece of code within the Userform as shown above.

So I am wondering to get rid of this error do I have to assign some kind of a variable to the range possibly?

Upvotes: 0

Views: 33

Answers (1)

David Zemens
David Zemens

Reputation: 53623

Better (for legibility IMO) to use a Select Case switch in this instance:

With Sheets("DoNotPrint - Setup")
Select Case .Range("H28").Value
    Case "Chief Process Safety"
        ToggleButton1 = True
    Case "Senior Process Safety", "Interm Process Safety", ""
        ToggleButton1 = False
End Select
End With

Alternatively, you need to properly construct your boolean expression:

If .Range("H28").value = "Senior Process Safety" Or _
   .Range("H28").value = "Interm Process Safety" Or _
   .Range("H28").value = "" Then

Because as currently constructed, your If statement is comparing a boolean with a string(s) which results in the Mismatch Error. Breaking it down, it is this:

If (.Range("H28").Value = "Senior Process Safety") Or "Interim Process Safety" Or "" Then

And because you can't compare these types, If True Or "foo" raises a mismatch error just as your code will raise the mismatch error.

Upvotes: 3

Related Questions