Excel formula vs user-defined VBA function

jdmc45

Board Regular
Joined
May 8, 2011
Messages
146
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have created a simple UDF in VBA and the same formula on my spreadsheet. The values created by both ways are quite different, and I cannot figure out why there is such a large discrepancy.

In VBA I am using many VBA functions such as Log, Exp and then some Excel functions such as WorksheetFunction.Pi, WorksheetFunction.NormSDist(x) for example.

Firstly, why do you think the results are so different? I am talking errors in the range of 50% of the answer to 150% of the answer.

Secondly, is there a way to troubleshoot a UDF in VBA the same way as a procedure? The help menu is useless in VBA. Can I see the values for certain inputs in my code, similar to using the break point and run method for sub routines?

Any help is much appreciated.

James
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
To debug UDF, do the following.

1. Set breakpoint in the beginning of UDF.
2. Go to the cell with UDF.
3. Press F2.
4. Press Enter.

You'll be in VBE with yellow line at breakpoint. Press F8 to step-by-step.
 
Upvote 0
thanks for that, works a treat. Is there a way to check the value of more than just the variable though? for example, instead of placing the cursor over the x in sqr(x) and see 4, is there a way to see the value of the whole sqr(x) value, which in this case would be 2?

So frustrating! the formulas in both are exactly the same, using the same data. Maybe VBA does some rounding in the intermediate steps that makes it inaccurate?

Cheers!
 
Upvote 0
Then post Excel's formula and VBA's one. :)
 
Upvote 0
Use the Locals or Watch windows to see the current values of any variables.
 
Upvote 0
To see the result of sqr(x), you should place curson over "sqr", not "x".
 
Upvote 0
Are you confusing common logs with natural logs by any chance?

The VBA function Log() is the equivalent of the worksheet function =LN, not =LOG.

Verify this for yourself using some simple numbers and the Immediate window (Ctrl-G).

(I dont understand any of this - I'm not a mathematician.)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,551
Members
452,927
Latest member
rows and columns

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