UDF question: from Chapter 4 VBA and Macros text.

mikecox39

Active Member
Joined
Mar 5, 2014
Messages
251
Office Version
  1. 365
Platform
  1. Windows
I'm studying Jelen's book "VBA and Macros" and at the start of Chapter 4, on UDF's, he directs the reader to open a new module in the the VBA Editor and type the following function into the module: Add (Number1,Number2).


Can someone tell me what I'm doing wrong because when I enter that line I get a compile error.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Too late to edit:

I think I see what's wrong. The line should have been prefaced with "Function"!

I only realized that after seeing the example, but now I don't understand the example because it's not at all like the line given in the instruction.
 
Upvote 0
You're right...something is missing (also, it's Chapt 14, I believe, not 4)

I would have written:

Code:
Function Add(Number1, Number2)
Add = Number1 + Number2
End Function

which seems to add the two numbers together without a hitch.
 
Last edited:
Upvote 0
Thanks, now it makes perfect sense.

don't understand why he left the Function part out. Then, at location 2190, there is this, that followed the instruction with the missing Function prefix:

Function Add (Number1 As Integer, Number2 As Integer) As Integer
Add =Number1+Number2
End Function

I understand about data types but I'm surprised by the club-footed way he set it up. What if I didn't know about data types, I'd really be confused! I'm still a little confused, by the last As Integer. But I think he steps through the code a little later on.

(also, it's Chapt 14, I believe, not 4)
No, it's definitely chapter 4. "User-Defined Functions. Chapter 14 is "Excel Power". Maybe your looking at a different edition. I have Microsoft Excel 2010, 8th printing

Hey, your local!
 
Last edited:
Upvote 0
Small world this Excel community...LOL. I'm in UTC. We should meet sometime and exchange stories. Are you a wine drinker?
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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