Runtime error 13 Type Mismatch

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
On my userform, I have 16 checkboxes. Some of these checkboxes enable textboxes like the code below shows.
Code:
Private Sub chkbxAlignment_Click()
    If Me.chkbxAlignment.Value = True Then
        Me.txtbxAlignment.Enabled = True
        Me.txtbxAlignment.SetFocus
    Else
        Me.txtbxAlignment.Value = ""
        Me.txtbxAlignment.Enabled = False
    End If
End Sub

The error code in the title occurs on this line of code:
Code:
    If "TextBox" Like "*" & TypeName(cf) & "*" Then If cf.Value Then s = s & cf.Caption & ", "

What I am wanting to happen is if the user clicks on a checkbox that has a textbox I want the checkbox caption to display followed by a " - " and the textbox caption. Below is the code.
VBA Code:
Case Is = "Frame2"
    For Each cf In ctrl.Controls
        If "CheckBox" Like "*" & TypeName(cf) & "*" Then If cf.Value Then s = s & cf.Caption & ", "
        If "TextBox" Like "*" & TypeName(cf) & "*" Then If cf.Value Then s = s & cf.Caption & ", "
    Next

Thak you
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I see that you decided for my code:
But you should comment or a feedback in the same thread.

Before you were checking an optionbutton, now you want to check a textbox.

As you now have a Texbox you cannot verify if it is true or false.
Do you want to check if it has text or is it empty?
And on the other hand, the Textbox does not have the .Caption property, if you want the content of the textbox, it would be .Value or .Text
 
Upvote 0
This form has many frames, 10 to be exact. Inside some of the frames are either checkboxes, checkboxes that enable textboxes, and/or optionbuttons. I was trying to modify your code to allow it to work with textboxes but how can I bind the two together. There names are: chbxAlignment and txtbxAlignment. Would mind commenting your code please so I can learn from it? Thank You.
 
Upvote 0
to allow it to work with textboxes
For a checkbox, it is clear to me, if the checkbox is selected then s = checkbox.caption
That's what this code is for:
If "CheckBox" Like "*" & TypeName(cf) & "*" Then If cf.Value Then s = s & cf.Caption & ", "

What do you want for the textbox?
You can explain it with an example.
 
Upvote 0
If the checkbox that enables a textbox is checked then I would like in this example to be if chkbxAlignment= true then s_1=chkbxAlignment.caption & " - " & txtbxAlignment.value but the question is can it find all three checkboxes that enable textboxes in a frame1 using a modified version of your For Loop? Thank you
 
Upvote 0
:unsure:

You could explain with examples.
You have 3 checkboxes, what are they called and what do they have in the caption?
and 3 textbox, what are they called?
Assuming that the 3 checkboxes are marked, and you have data in the textbox (put 3 examples one data for each texbox)
What would be the result in s?
 
Upvote 0
My userform is used for maintenance work performed. I have 10 frames, each frame contains checkboxes and/or optionbuttons
In Frame1 (Named Adjustment) I have 16 checkboxes total. Three of those checkboxes enable textboxes. The first checkbox(chkbxAlignment) that has the caption "Alignment" at which point the corresponding textbox(txtbxAlignment) is enabled so the user must enter what was aligned.
Is there not a way to bind the two together so if the user clicks that checkbox it also pulls the value of the textbox as well?
Here's what I have so far:
VBA Code:
Private Sub chkbxAlignment_Click()
    On Error Resume Next
    If Me.chkbxAlignment.Value = True Then
        Me.txtbxAlignment.Enabled = True
        Me.txtbxAlignment.SetFocus
    Else
        Me.txtbxAlignment.Value = ""
        Me.txtbxAlignment.Enabled = False
    End If
End Sub

Private Sub chkbxAlignment_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Me.chkbxAlignment.Value = True Then s2 = Me.chkbxAlignment.Caption & " - " & Me.txtbxAlignment.Value
End Sub
and for the textbox, I have this code:
Code:
Private Sub txtbxAlignment_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    fr2 = Me.chkbxAlignment.Caption & "-" & Me.txtbxAlignment.Value
    
    If txtbxAlignment.Value = "" Then Me.chkbxAlignment.Value = False And Me.txtbxAlignment.Enabled = False
    Dim result As Integer
        result = MsgBox("No value was entered inside the Alignment textbox. Please enter what was aligned?", vbQuestion, "Nothing Entered")
        If result = 1 Then Exit Sub
End Sub
This is probably a different issue, but my messagebox repeats twice before exiting the subroutine.
Here are all the checkboxes that enable textboxes in frame2:
chkbxAlignment & txtbxAlignment
chkbxBlank1 & txtbxBlank1
chkbxBlank2 & txtbxBlank2
If there is a For Loop that works with this, I was going to apply it to the other frames that have the same type of checkboxes. Thanks again for all your help.
 
Upvote 0
Sorry, but I am not understanding what the end goal is.
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,757
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top