UDF question: from Chapter 4 VBA and Macros text.

mikecox39

Board Regular
Joined
Mar 5, 2014
Messages
237
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.
 

mikecox39

Board Regular
Joined
Mar 5, 2014
Messages
237
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.
 

kweaver

Well-known Member
Joined
May 8, 2018
Messages
838
Office Version
365, 2010
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:

mikecox39

Board Regular
Joined
Mar 5, 2014
Messages
237
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:

kweaver

Well-known Member
Joined
May 8, 2018
Messages
838
Office Version
365, 2010
I have the 2019 version.

Where in SD are you?
 

kweaver

Well-known Member
Joined
May 8, 2018
Messages
838
Office Version
365, 2010
Small world this Excel community...LOL. I'm in UTC. We should meet sometime and exchange stories. Are you a wine drinker?
 

Forum statistics

Threads
1,085,167
Messages
5,382,089
Members
401,768
Latest member
katana_flyer

Some videos you may like

This Week's Hot Topics

Top