Have you ever encountered an error while working with Visual Basic for Applications (VBA) that reads "Function call on left hand side of assignment msgbox"? You're not alone. This error occurs when you're trying to assign the result of a function to a variable, but the function is not returning a value. Let's dive into understanding this error, its causes, and how to fix it.
Understanding the Error: Function Call on Left Hand Side of Assignment Msgbox
The error "Function call on left hand side of assignment msgbox" is a compile-time error in VBA. It occurs when you're trying to assign the result of a function to a variable, but the function doesn't return a value. In other words, you're treating a function as if it were a variable, which is not allowed in VBA.
Example of the Error
Here's a simple example that might trigger this error:

```vba Sub Test() Dim result As Integer result = MsgBox("Hello, World!") End Sub ```
In this example, `MsgBox` is a function that displays a message box, but it doesn't return a value. So, trying to assign its result to `result` causes the error.
Causes of the Error
- Forgetting to Use the Keyword
Function: If you're trying to create a function but forget to use the `Function` keyword, VBA won't know that you're defining a function. Instead, it will treat the function name as a variable, leading to this error. - Not Returning a Value from a Function: If you've defined a function with the `Function` keyword but haven't used the `Function` keyword again to return a value, VBA won't know what value to assign to the variable you're using on the left-hand side of the assignment.
How to Fix the Error
Ensure the Function Returns a Value
To fix this error, you need to ensure that the function you're calling returns a value. Here's how you can do this:
```vba Function GetMsgBoxResult() As Integer GetMsgBoxResult = MsgBox("Hello, World!") End Function ```
In this example, `GetMsgBoxResult` is a function that calls `MsgBox` and returns its result. Now, you can use this function in your assignment like this:

```vba Sub Test() Dim result As Integer result = GetMsgBoxResult() End Sub ```
Understanding the MsgBox Function
It's worth noting that the `MsgBox` function doesn't return a value. It's used to display a message box and it always returns `0`. If you want to use `MsgBox` and get the user's response, you should use the `MsgBox` function with the `vbOKCancel` or `vbYesNo` constant, like this:
```vba Sub Test() Dim result As VbMsgBoxResult result = MsgBox("Hello, World!", vbOKCancel) End Sub ```
In this example, `vbOKCancel` is a constant that tells `MsgBox` to display an OK and Cancel button. The function will return `vbOK` if the user clicks OK, and `vbCancel` if the user clicks Cancel.
Conclusion
The "Function call on left hand side of assignment msgbox" error is a common mistake in VBA that can be easily fixed by understanding how functions work in VBA. By ensuring that the function you're calling returns a value, you can avoid this error and write more efficient VBA code.























