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

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,165
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:

christosf360

New Member
Joined
Apr 19, 2015
Messages
8
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
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,165
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?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,559
Office Version
365
Platform
Windows
Change OLEObject to Object.
 

Watch MrExcel Video

Forum statistics

Threads
1,099,064
Messages
5,466,380
Members
406,478
Latest member
Amar kumar

This Week's Hot Topics

Top