PC Review


Reply
Thread Tools Rate Thread

declare multiple variables

 
 
geebee
Guest
Posts: n/a
 
      28th Feb 2008
hi,

is there any way i can declare multiple variables at the same time without
creating tons of different functions. for example, lets say i have:

Function zum() As Variant
Application.Volatile
zum = 0
For Each w In Worksheets
zum = zum + w.Range("C2").Value
Next
End Function

i want to be able to modify it to something like:

Function zum() As Variant
Application.Volatile
zum = 0
For Each w In Worksheets
zum = zum + w.Range("C2").Value
zum2 = zum2 + w.Range("D2").Value
zum3 = zum3 + w.Range("F2").Value
Next
End Function


how should I modify it?

thanks in advance,
geebee


 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      28th Feb 2008
It looks like you are doing a UDF which by default can only return one value.
How about something more like this...

Function zum(byval Target as range) As double
dim w as worksheet

Application.Volatile
zum = 0
For Each w In Worksheets
zum = zum + w.Range(Target.Address).Value
Next w
End Function

Use it like
=Zum(C2)
or
=Zum(D2)

--
HTH...

Jim Thomlinson


"geebee" wrote:

> hi,
>
> is there any way i can declare multiple variables at the same time without
> creating tons of different functions. for example, lets say i have:
>
> Function zum() As Variant
> Application.Volatile
> zum = 0
> For Each w In Worksheets
> zum = zum + w.Range("C2").Value
> Next
> End Function
>
> i want to be able to modify it to something like:
>
> Function zum() As Variant
> Application.Volatile
> zum = 0
> For Each w In Worksheets
> zum = zum + w.Range("C2").Value
> zum2 = zum2 + w.Range("D2").Value
> zum3 = zum3 + w.Range("F2").Value
> Next
> End Function
>
>
> how should I modify it?
>
> thanks in advance,
> geebee
>
>

 
Reply With Quote
 
geebee
Guest
Posts: n/a
 
      29th Feb 2008
hi,

this works good. but now i would like to know how i can add hem. lts say
for example that cell C1 has a formula of =zum(E12) in it. and cell C2 has a
formula of =zum(E13) in it. and so forth. i would like to know how i can
add these zums up in cell C5.i tried to use =sum(C1:C4) but its not working.

thanks in advance,
geebee


"Jim Thomlinson" wrote:

> It looks like you are doing a UDF which by default can only return one value.
> How about something more like this...
>
> Function zum(byval Target as range) As double
> dim w as worksheet
>
> Application.Volatile
> zum = 0
> For Each w In Worksheets
> zum = zum + w.Range(Target.Address).Value
> Next w
> End Function
>
> Use it like
> =Zum(C2)
> or
> =Zum(D2)
>
> --
> HTH...
>
> Jim Thomlinson
>
>
> "geebee" wrote:
>
> > hi,
> >
> > is there any way i can declare multiple variables at the same time without
> > creating tons of different functions. for example, lets say i have:
> >
> > Function zum() As Variant
> > Application.Volatile
> > zum = 0
> > For Each w In Worksheets
> > zum = zum + w.Range("C2").Value
> > Next
> > End Function
> >
> > i want to be able to modify it to something like:
> >
> > Function zum() As Variant
> > Application.Volatile
> > zum = 0
> > For Each w In Worksheets
> > zum = zum + w.Range("C2").Value
> > zum2 = zum2 + w.Range("D2").Value
> > zum3 = zum3 + w.Range("F2").Value
> > Next
> > End Function
> >
> >
> > how should I modify it?
> >
> > thanks in advance,
> > geebee
> >
> >

 
Reply With Quote
 
Heimo
Guest
Posts: n/a
 
      2nd Mar 2008
Hi,

replace the line:
zum = zum + w.Range(Target.Address).Value

with this line:
zum = zum + worksheetfunction.sum(w.Range(Target.Address)

It should work

Much fun, Heimo


"geebee" <(E-Mail Removed)(noSPAMs)> schrieb im Newsbeitrag
news:A0402D76-02C4-46B6-B209-(E-Mail Removed)...
> hi,
>
> this works good. but now i would like to know how i can add hem. lts say
> for example that cell C1 has a formula of =zum(E12) in it. and cell C2 has
> a
> formula of =zum(E13) in it. and so forth. i would like to know how i can
> add these zums up in cell C5.i tried to use =sum(C1:C4) but its not
> working.
>
> thanks in advance,
> geebee
>
>
> "Jim Thomlinson" wrote:
>
>> It looks like you are doing a UDF which by default can only return one
>> value.
>> How about something more like this...
>>
>> Function zum(byval Target as range) As double
>> dim w as worksheet
>>
>> Application.Volatile
>> zum = 0
>> For Each w In Worksheets
>> zum = zum + w.Range(Target.Address).Value
>> Next w
>> End Function
>>
>> Use it like
>> =Zum(C2)
>> or
>> =Zum(D2)
>>
>> --
>> HTH...
>>
>> Jim Thomlinson
>>
>>
>> "geebee" wrote:
>>
>> > hi,
>> >
>> > is there any way i can declare multiple variables at the same time
>> > without
>> > creating tons of different functions. for example, lets say i have:
>> >
>> > Function zum() As Variant
>> > Application.Volatile
>> > zum = 0
>> > For Each w In Worksheets
>> > zum = zum + w.Range("C2").Value
>> > Next
>> > End Function
>> >
>> > i want to be able to modify it to something like:
>> >
>> > Function zum() As Variant
>> > Application.Volatile
>> > zum = 0
>> > For Each w In Worksheets
>> > zum = zum + w.Range("C2").Value
>> > zum2 = zum2 + w.Range("D2").Value
>> > zum3 = zum3 + w.Range("F2").Value
>> > Next
>> > End Function
>> >
>> >
>> > how should I modify it?
>> >
>> > thanks in advance,
>> > geebee
>> >
>> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declare variables to a code? Pas Microsoft Excel Misc 6 10th Apr 2010 01:14 PM
DECLARE VARIABLES PROBLEM ytayta555 Microsoft Excel Programming 7 19th Mar 2008 03:53 PM
Declare variables in own subroutine dancer Microsoft ASP .NET 6 15th Sep 2007 12:54 AM
where to declare variables John Salerno Microsoft C# .NET 5 14th Nov 2005 03:00 AM
Where to declare variables Harlan Microsoft Access Getting Started 1 20th Aug 2004 01:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:33 PM.