0

I keep getting the following error Compile Error: Expected = whenever I try to run my VBA code. What is causing it?

Function cStatus(cValue, mName)
If mName= True Then
rTotal = rTotal + cValue
ElseIf mName= False Then
rTotal = rTotal - cValue
End If
End Function

Private Sub BE_Click()
checkStatus = cStatus(10, "B_E")
End Sub

1 Answer 1

2

You are not assigning a return value for your cStatus function, so the line checkStatus = cStatus(10, "B_E") doesn't know what it is receiving.

' Define a return type for the function (I used string).
Function cStatus(cValue, mName) As String
    ' Wrapping True and False in quotes since you are passing a string.
    ' If this is supposed to be a boolean, then type the parameter (see below).
    If mName = "True" Then
        rTotal = rTotal + cValue
    ElseIf mName = "False" Then
        rTotal = rTotal - cValue
    End If

    ' Assign a return value.
    cStatus = "the value"
End Function

Alternately, you can make cStatus a Sub if you do not need to return a value:

Sub cStatus(cValue, mName)
    If mName = "True" Then
        rTotal = rTotal + cValue
    ElseIf mName = "False" Then
        rTotal = rTotal - cValue
    End If
End Function

Private Sub BE_Click()
    ' No "checkStatus" variable.

    cStatus 10, "B_E"
End Sub

As a side note, none of your parameters are typed, so they will all come in as Variant passed by reference (ByRef). I'm not sure if this intended, but it is good practice to type them regardless as most of the time you will want to pass them by value (ByVal).

For example:

Function cStatus(ByVal cValue As Integer, ByVal mName As Boolean) As String
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.