Passing Checkboxes as variables

christosf360

New Member
Joined
Apr 19, 2015
Messages
8
Hi everyone,

I have built a Subroutine and one of the required variables is a Checkbox. The Subroutine needs to check if a specified Checkbox has been ticked or not. Here is the declaration of the Subroutine with the Checkbox variable being the last one called "SwitchBox" (Have I used the right variable type to begin with???):

Code:
Sub SwitchText(SwitchFlag As Boolean, SwitchType As String, TextForSwitch As String, _
UnitSelected As String, SwitchBox As OLEObject)

I then try and call it by using the following line:

Code:
Call SwitchText(SpecsFlag, "Specs", "E59", Unit, Specs)

But when I do so I get a "Type mismatch error"

Any suggestions please?...?

Many thanks!!!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Since the parameters in SwitchText have been declared without the keyword ByVal or ByRef, the arguments are passed by reference. As such, the variables being passed to SwitchText must be declared in the calling sub...

Code:
Dim SpecsFlag As Boolean
Dim Unit As String
Dim Specs As OLEObject

Hope this helps!
 
Last edited:
Upvote 0
Hi there, useful info about ByRef and ByVal, unfortunately I still get a type mismatch error. Here is give the whole piece of code (I also tried to declare within the calling sub as shown in the commented piece of code but same error):


Code:
Sub SwitchText(SwitchFlag As Boolean, SwitchType As String, TextForSwitch As String, _UnitSelected As String, ByRef SwitchBox As OLEObject)


SwitchFlag = False


If Specs.Value = True And SpecsFlag = False Then
    Worksheets("Switches").Range("E3") = Worksheets("AllText").Range(TextForSwitch)
    Worksheets("Switches").Range("E3") = Replace(Range("E3"), "#ControlUnit#", UnitSelected)
    Worksheets("Switches").Range("E3") = Replace(Range("E3"), "#Switch#", SwitchType)
Else
    Worksheets("Switches").Range("E3") = ""
    SwitchFlag = Not SwitchFlag
End If


End Sub


Private Sub Specs_Click()
Dim SpecsFlag As Boolean
'Dim SwitchFlag As Boolean
'Dim SwitchType As String
'Dim TextForSwitch As String
'Dim UnitSelected As String
'Dim SwitchBox As OLEObject






Call SwitchText(SpecsFlag, "Specs", "E59", Unit, Specs)


End Sub

Any ideas of what I am doing wrong please?...?

Many thanks
 
Upvote 0
As I posted in my previous post, the variables that you should be declaring are SpecsFlag, Unit, and Specs. So your calling sub should be as follows...

Code:
Private Sub Specs_Click()

    Dim SpecsFlag As Boolean
    Dim Unit As String
    Dim Specs As OLEObject
    
    'etc
    '
    '
    
    Call SwitchText(SpecsFlag, "Specs", "E59", Unit, Specs)


End Sub

By the way, the under score in _UnitSelected is an invalid character. So I think you'll need to remove it.

Does this help?
 
Upvote 0

Forum statistics

Threads
1,213,526
Messages
6,114,136
Members
448,551
Latest member
Sienna de Souza

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