View Full Version : Solved: Function help
YellowLabPro
03-10-2007, 06:19 AM
I am working on Functions, from lessons in a book. The following is not a lesson per say, but an example.
 
Function Addtwo(arg1, arg2)
Dim Addtwo As Integer
    Addtwo = arg1 + arg2
    
End Function
 
To make this work, I realize I need to call it from a Sub. But where/how do I store the arguments?
 
thanks,
 
YLP
mdmackillop
03-10-2007, 06:45 AM
Here are three simple examples.  BTW you don't dim the function name within the function, or you'll get an error.
 Sub test1()
MsgBox Addtwo(1, 3)
End Sub
Sub Test2()
ActiveSheet.Range("D1") = Addtwo(Range("A1"), Range("B1"))
End Sub
Sub Test3()
Dim Cel As Range
For Each Cel In Range(Cells(5, 1), Cells(20, 1))
    Cel.Offset(0, 3) = Addtwo(Cel, Cel.Offset(0, 1))
Next
End Sub
Function Addtwo(arg1, arg2) As Long
    Addtwo = arg1 + arg2
End Function
YellowLabPro
03-10-2007, 06:47 AM
Thanks Malcolm.
mdmackillop
03-10-2007, 06:52 AM
and of course the arguments could also be Variable names with values assigned from the sub.
YellowLabPro
03-10-2007, 06:56 AM
Of Course....http://vbaexpress.com/forum/images/icons/icon10.gif, 
Actually very good point-- something that I might not have thought about until later was faced w/ the scenario.
Thanks for pointing that out.
 
 
Best,
 
YLP
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.