brothertruffle880
asked on
Excel 2010 VBA - difficulty creating a custom function
I'm learning how to create custom functions and have created one successfully and the second one is not working. ... can someone give me the syntax fix for what I'm trying to do? ... I'm trying to get used to the syntax/grammar of VBA and creating functions. Not a real enterprise application ... yet. ¿
This is working perfectly:
Function discount_amount(quantity, price)
discount_amount = quantity * price
End Function
This is not working:
Function addup_numbers(firstnum, lastnum)
addup_numbers = sum(firstnum : lastnum)
End Function
This is working perfectly:
Function discount_amount(quantity, price)
discount_amount = quantity * price
End Function
This is not working:
Function addup_numbers(firstnum, lastnum)
addup_numbers = sum(firstnum : lastnum)
End Function
or just
Function addup_numbers(firstnum, lastnum)
addup_numbers = firstnum + lastnum
End Function
Function addup_numbers(firstnum, lastnum)
addup_numbers = firstnum + lastnum
End Function
Basically in user defined functions you usually (always?) need to use VBA code rather than sheet formulas.
OK, UDF, my favorite topic, love 'em.
Start by declaring variables in the function
E.G:
I am guessing that you are trying to sum all cells between a range not just the first and last cell? or?
Start by declaring variables in the function
E.G:
Function addup_numbers(firstnum as double, lastnum as double) as double
addup_numbers = firstnum + lastnum
End Function
I am guessing that you are trying to sum all cells between a range not just the first and last cell? or?
As you have used the colon ':' I am guessing you are trying to do a range:
Function addup_numbers(allnums as range) as double
for each c in allnums
if isnumeric(c) then
addup_numbers = addup_numbers + c.value
end if
next
End Function
NOTE:
if you want to add up numbers between two cells such as StartCell and EndCell and then use VBA to call the remaining cells... I wouldn't...
Try to avoid a function which does not include the range or cells involved in the formula in brackets, as if you change the cells that are referenced but not in the brackets then the result of the formula will not calculate automatically.
if you want to add up numbers between two cells such as StartCell and EndCell and then use VBA to call the remaining cells... I wouldn't...
Try to avoid a function which does not include the range or cells involved in the formula in brackets, as if you change the cells that are referenced but not in the brackets then the result of the formula will not calculate automatically.
ASKER
Barman:
Thank you.
What I'm trying to understand is the VBA syntax used in implementing the sum function within my user-created function.
I'm trying to add the following numbers:
The range a1 up to and including a10.
Just a simple sum function to add up a contiguous range.
My purpose in posting the question was I wanted to know how come it rejected my use of the colon! I know that there may be faster and better ways of getting this job done but I want to make my solution work. I want to understand the syntax of my use of the SUM function within a UDF. This is purely a learning exercise.
I do not merely want to add firstnum and lastnum together. I should have more accurately stated my arguments as first_num_in_range and last_num_in_range
Thank you.
What I'm trying to understand is the VBA syntax used in implementing the sum function within my user-created function.
I'm trying to add the following numbers:
The range a1 up to and including a10.
Just a simple sum function to add up a contiguous range.
My purpose in posting the question was I wanted to know how come it rejected my use of the colon! I know that there may be faster and better ways of getting this job done but I want to make my solution work. I want to understand the syntax of my use of the SUM function within a UDF. This is purely a learning exercise.
I do not merely want to add firstnum and lastnum together. I should have more accurately stated my arguments as first_num_in_range and last_num_in_range
As I have stated in earlier posts, it is not good practice to do this as the function result will not calculate automatically if any of the values between first and last number in the range change.
However to do what you wish:
However to do what you wish:
Function addup_numbers(first_num_in_range As Range, last_num_in_range As Range) As Double
Dim allnums As Range
Set allnums = Range(first_num_in_range, last_num_in_range)
Debug.Print allnums.Address
For Each c In allnums
If IsNumeric(c) Then
addup_numbers = addup_numbers + c.Value
End If
Next
End Function
ASKER
Thanks Barman.
Very useful solution. It works perfectly.
Question: How can I make the sum function calculate a range in a UDF
Where do I put quotes or ampersands, etc.
That's all I want.
I know this is a sucky solution to a simple problem but I'm just trying to understand syntax of VBA i.e. how and where to place quote marks. ampersands, etc.
Function addup_numbers(first_in_ran ge, last_in_range)
addup_numbers = sum(first_in_range : last_in_range)
End Function
Very useful solution. It works perfectly.
Question: How can I make the sum function calculate a range in a UDF
Where do I put quotes or ampersands, etc.
That's all I want.
I know this is a sucky solution to a simple problem but I'm just trying to understand syntax of VBA i.e. how and where to place quote marks. ampersands, etc.
Function addup_numbers(first_in_ran
addup_numbers = sum(first_in_range : last_in_range)
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You don't.
Sum is a worksheet function.
Sum is a worksheet function.
Function addup_numbers(first_num_inNice._range As Range, last_num_in_range As Range) As Double
addup_numbers = WorksheetFunction.Sum(Range(first_nu m_in_range , last_num_in_range))
End Function
addup_numbers = firstnum + lastnum
End Function
Note the + rather than the :