Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Simple VBA Accumulator (Function?)

Can a function get repeated values, add them, and return a total to
the calling procedure?
I need to reuse this code with differing numbers of items.

Example

Pass these parameters

add_to(1)
add_to(2)

Get the returned total from function

function accum(number)
total = total + number

RETURN TOTAL to Procedure
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Simple VBA Accumulator (Function?)

Does this simple macro help. If not, details.

Sub addto()
var1 = 2
var2 = 3
MsgBox var1 + var2 '5
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

wrote in message
...
Can a function get repeated values, add them, and return a total to
the calling procedure?
I need to reuse this code with differing numbers of items.

Example

Pass these parameters

add_to(1)
add_to(2)

Get the returned total from function

function accum(number)
total = total + number

RETURN TOTAL to Procedure


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Simple VBA Accumulator (Function?)

not sure what you mean by "repeated values"
you can pass a variable list of parameters if thats what you mean?

or use a ParamArray in your parameter list

" wrote:

Can a function get repeated values, add them, and return a total to
the calling procedure?
I need to reuse this code with differing numbers of items.

Example

Pass these parameters

add_to(1)
add_to(2)

Get the returned total from function

function accum(number)
total = total + number

RETURN TOTAL to Procedure

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Simple VBA Accumulator (Function?)

I'm not sure what you are really looking for, but perhaps the
following might get you started:

Function AddThemUp(ParamArray Args() As Variant) As Double
Dim N As Long
Dim L As Long
For N = LBound(Args) To UBound(Args)
If IsNumeric(Args(N)) = True Then
L = L + Args(N)
End If
Next N
AddThemUp = L
End Function


This function takes any number of input parameters and returns the sum
of those parameters. E.g,.,

Dim X As Long
X = AddThemUp(10,20,30)
Debug.Print X

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)





On Mon, 7 Sep 2009 05:03:54 -0700 (PDT), "
wrote:

Can a function get repeated values, add them, and return a total to
the calling procedure?
I need to reuse this code with differing numbers of items.

Example

Pass these parameters

add_to(1)
add_to(2)

Get the returned total from function

function accum(number)
total = total + number

RETURN TOTAL to Procedure

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Simple VBA Accumulator (Function?)

On further reflection, I might know what you're looking for. You might
want declare a module-level variable and use that as your accumulator.
In the code module, before and outside of any procedure, enter

Dim X As Long

Any code in any procedure within this code module can read and change
the value stored in X. The value of X will remain even when a Sub or
Function ends.

So, your code could do something like

Dim X As Long
Sub ABC()
X = 1234
End Sub

Sub DEF()
X = X + 1
End Sub

Here, ABC() sets X to an initial value, 1234, which will be retained
even after ABC terminates. Then, DEF() adds 1 to X and X will retain
that new value after DEF terminates.

If you really do what an Increment function, you could use

Function IncrementX(Optional N As Long = 1) As Long
X = X + N
IncrementX = X
End Function

If N is omitted, X is increment by 1. If N is specified, X gets the
new value X+N. In either case, the function returns the new value of
X.

Using a variable that is declared outside of a procedure is a matter
of what is called the "scope" of the variable. See
www.cpearson.com/Excel/Scope.aspx for more information about scoping
variables and procedures.


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)





On Mon, 7 Sep 2009 05:03:54 -0700 (PDT), "
wrote:

Can a function get repeated values, add them, and return a total to
the calling procedure?
I need to reuse this code with differing numbers of items.

Example

Pass these parameters

add_to(1)
add_to(2)

Get the returned total from function

function accum(number)
total = total + number

RETURN TOTAL to Procedure

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accumulator BadBoy Excel Worksheet Functions 2 April 24th 09 12:25 PM
Simple problem, simple formula, no FUNCTION ! Ron@Buy Excel Worksheet Functions 6 September 28th 07 04:51 PM
accumulator with other than one dan Excel Worksheet Functions 8 November 24th 06 06:29 PM
Please help fix my accumulator Tim McMahon Excel Programming 3 May 23rd 04 09:06 PM
Please help me get this accumulator working Bill Craig[_3_] Excel Programming 1 May 19th 04 10:36 PM


All times are GMT +1. The time now is 07:30 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"