karamell
karamell

Reputation: 713

VBA Excel: not statement & formula

Is there a way of combining a not statement with a function in the following way:

I want it to work like this example

MsgBox not False

which returns True.

But if I add a function instead of the boolean statement

MsgBox not myFunction()

then it returns a mismatch error.

myFunction() is supposed to return True or False

Any suggestions?

Upvotes: 0

Views: 72

Answers (3)

Mr.Monshaw
Mr.Monshaw

Reputation: 450

what is myFunction defined as? if its not :

Function myFunction(...) As Boolean
'...
End Function

that would be why, 'not' operator is reserved for Boolean (true/false)

if your function is like this and only recieves 1 and 0:

Function myFunction(...) As Integer

add this to the call :

MsgBox not CBool(myFunction(...))

or if you are actually trying to compare text:

Function myFunction(...) As String

then you will need:

EDIT: not string.compare, (StrComp(Str1, Str2, vbTextCompare) = 0)

hope that helps :)

Upvotes: 0

karamell
karamell

Reputation: 713

Simply

MsgBox Not Application.Evaluate(myFunction)

Upvotes: 0

Richard Morgan
Richard Morgan

Reputation: 7691

You will need to give us more code, as the following works:

Public Sub test()
    MsgBox Not myfunction()
End Sub

Function myfunction() As Boolean
    myfunction = False
End Function

Upvotes: 3

Related Questions