Application.Evaluate - unexpected results

Jehannum_2000

New Member
Joined
Sep 14, 2024
Messages
15
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
I have a simple function which is meant to evaluate a numerical expression input by the user:

VBA Code:
Function eval(n As String) As Double
'Evaluates numeric expression contained in string n

Application.Volatile

On Error GoTo Handle_Error: 'need handler for /0 exceptions etc. from user input

eval = Application.Evaluate(n)
Exit Function

Handle_Error:
MsgBox ("Incorrect expression.")
eval = 0

End Function

When the user enters a simple number, not an expression, the function sometimes works and sometimes doesn't work.

In particular, the numbers 1, 3, 5, 7, 60, 124 and 125 give an error (Type Mismatch, I think). All other positive integers up to 126 work perfectly.

However, if I include the following line in the function (before Application.Evaluate) it always works without an error:

VBA Code:
n = n & "+0"

I guessed the above fix, but don't really understand why it works.

Are the 'forbidden' numbers numbers that were previously entered at some point that Excel doesn't want to evaluate again for efficiency reasons? If so, why does appending "+ 0" cause them to work?

I would like to understand a little more about what's going on. Any help appreciated!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How/where does the user input the number?
A user form, either by typing directly into a text input box in the form, or - if an appropriate cell on the spreadsheet is selected - the form pulls the number from the cell into the textbox.

It gives the same result whichever source is used: certain numbers (1, 3, 5, 7, 60, 125) cause an error while others don't, and valid expressions such as 6*19 never seem to give an error (hence I had the workaround idea of appending "+0" to numbers to turn them into expressions).
 
Upvote 0
What is the actual calling code?
 
Upvote 0
VBA Code:
Private Sub ConvertButton_Click()

Dim gasType As String
Dim heatInput As Double
Dim gasRate As Double
Dim sh As Worksheet
Set sh = ActiveSheet

gasType = Range("Gas_type")

heatInput = eval(HeatInputTextBox)
'used eval because they may have entered an expression, not a simple value

If heatInput < 0 Then
    MsgBox ("Heat input cannot be negative.")
    Unload Me
    Exit Sub
End If
 
Upvote 0
VBA Code:
Private Sub UserForm_Initialize()

If (Val(ActiveCell.Value) > 0) And (ActiveCell.Column = 12) Then
    HeatInputTextBox = Round(Val(ActiveCell), 2)
Else
    HeatInputTextBox = ""
End If
m3perHourTextBox = ""
kWnetButton = True

End Sub
 
Upvote 0
I can't replicate your error.
Can you post a workbook somewhere so that I can double-check the code in situ? Also, which version(s) of Excel have you tested in?
 
Upvote 0
Unfortunately, I can't post the workbook.

I've tried it on my own laptop (Microsoft Office Professional Plus 2021) and the work computer (don't know the version, but different from mine) with the same error for the same inputs.

I just think it must be some weird side effect of Application.Evaluate ...

The workaround works, so it's not critical to fix it - it's just my curiosity about Excel weirdness.

Thanks for trying!
 
Upvote 0
Oh, I forgot to mention - I set up another completely fresh spreadsheet and just copied the eval function and a simple input textbox.

This time, every input worked except the number 1 !?
 
Upvote 0
I just think it must be some weird side effect of Application.Evaluate
If it were that simple, I'd be able to replicate it, and you would always have the same errors on the same inputs. It sounds like some other factor is at work to me, but I can't think what. That's why I wanted a sample workbook (doesn't need any data in it) just so I can rule out (or in) something specific to the workbook rather than the code.
 
Upvote 0

Forum statistics

Threads
1,222,625
Messages
6,167,149
Members
452,099
Latest member
Auroraaa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top