excel VBA return result of function

thedanotto

Simple Layup for anyone. I've written a function isMac() which looks at the current operating System, if the first 3 letters are Mac, it is supposed to return True. I then want to assign the return value of the function to a variable in another function. I'm currently running a Mac, so when I run the following code, the MsgBox says True, while the Debug.Print returns False

Function isMac() As Boolean
  If Left(Application.OperatingSystem, 3) = "Mac" Then
    result = True
  Else
    result = False
  End If
  MsgBox result
End Function

Sub test()
  Debug.Print isMac()
End Sub

How do I correctly return a Boolean from my IF statement so that I can utilize it in another function?

user4039065

Try assigning the result to the function.

Function isMac() As Boolean
  isMac = CBool(LCase(Left(Application.OperatingSystem, 3)) = "mac")
End Function

Sub test()
  Debug.Print isMac()
End Sub

Another approach would be Compiler Directives and Compiler Constants. In a module code sheet's declarations area as,

#If Mac Then 
    Public Const bMAC as Boolean = True
#Else 
    Public Const bMAC as Boolean = False
#End If

Use bMAC anywhere in your code to determine whether the OS is a Mac or not.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass Excel Range in VBA Function, Process as Array, and Return Result

From Dev

Excel-VBA to return using LEFT of vlookup result

From Dev

How to get VBA function in Excel to return value of active cell?

From Dev

How to get VBA function in Excel to return value of active cell?

From Dev

How do I return an array from a vba function in Excel?

From Dev

excel vba function return array and paste in worksheet formula

From Dev

Return result to parent function

From Dev

Return result of Action to Function

From Dev

Return mongoose result in a function

From Dev

wrapAsync return function not result

From Dev

Querying a range of cells where each cell value should return a different result - Excel VBA ADODB

From Dev

EXCEL-VBA Return highest value from a find function from a txt file VBA

From Dev

How to create a lookup function or VBA code which can return value against only first found result?

From Dev

signed result of Val function in VBA

From Dev

VBA function result type mismatch

From Dev

Store into variable result of function VBA

From Dev

Excel function to VBA

From Dev

Excel not finding VBA Function

From Dev

Excel VBA and Group function

From Dev

Sub or Function in VBA Excel

From Dev

VBA Excel If statement with AND/OR function

From Dev

Find Function in Excel VBA

From Dev

Automate a Excel VBA Function

From Dev

Excel VBA hlookup function

From Dev

Excel VBA and Group function

From Dev

Round function VBA EXCEL

From Dev

Return stored procedure result in function

From Dev

How to get function return result?

From Dev

C - function to return curved result