-
Solved: Function help
I am working on Functions, from lessons in a book. The following is not a lesson per say, but an example.
[VBA]Function Addtwo(arg1, arg2)
Dim Addtwo As Integer
Addtwo = arg1 + arg2
End Function[/VBA]
To make this work, I realize I need to call it from a Sub. But where/how do I store the arguments?
thanks,
YLP
-
Here are three simple examples. BTW you don't dim the function name within the function, or you'll get an error.
[VBA] 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[/VBA]
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
-
-
and of course the arguments could also be Variable names with values assigned from the sub.
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
-
Of Course....,
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules