Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old May 11th, 2003, 02:30 PM
jjanes jjanes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Location: San Ramon, CA
Posts: 70 jjanes User rank is Private First Class (20 - 50 Reputation Level)jjanes User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 23 m 48 sec
Reputation Power: 7
excel question

How do you set up a formula to contain a dynamic range for example the sum()

=sum(A1:A72) works if you know the range starts at A! and end at A72

what if the lower limit is unknown???

key strokes shown below
=sum(A1:<endkey><Down arrow key>)

yields this
=sum(A1:aX) where X is the current activecell row in column A

how do I build this in a Excel VB macro???

I currently have a macro that when ever a button gets pushed a row is added to the bottom of a growing list I them want to sum up different totals but I want to use the dynamic range for the sum function

when ever I do a record macro to help me determine the proper syntax I only get the final result not the actions to get to the final result

I get something like
ActiveCell.Value= "=sum(A2:A17)" which is great for just this one instance then next time the macro would run the value would have to change from a17 to A18 and so on and so on

Thanks for your assistance
Jeff

Reply With Quote
  #2  
Old May 11th, 2003, 06:32 PM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
excel has a few different ways to expand cell / range objects to fill areas - equivalent to pressing ctrl-arrow or the home end keys etc.

i think what you need is the end property that each cell / range has:
Code:
"=sum(a2:" & range("a2").end(xldown).address(false,false) & ")"
i haven't tested this - it's off the top of my head, more or less

Last edited by epl : May 13th, 2003 at 05:48 AM.

Reply With Quote
  #3  
Old May 13th, 2003, 02:00 AM
mohecan mohecan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Melbourne, Australia
Posts: 212 mohecan User rank is Private First Class (20 - 50 Reputation Level)mohecan User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
You could also try using a named range and putting the name in the formula

e.g. set a named range of DATATOSUM = A1:A1000

the forumla =SUM(DATATOSUM)

Otherwise, to do it in VBA macros, you'd need to first find the last row (use xlup function) and store it in a variable

Then create your formula based on the variable.
__________________
How can I soar like an eagle when
I'm flying with turkey's?

Reply With Quote
  #4  
Old July 31st, 2003, 09:37 AM
juk_kuj juk_kuj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Ontario
Posts: 24 juk_kuj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
..... "=sum(a2:" & range("a2").end(xldown).address(false,false) & ")"


Your code above is very usful, i need a code that does that but with one more twist. I have labled an int and i need it to automatically change clumns with a click of a button, so it will sum up say colum A, then click again and it will sum up column B. This is what i tried, but it does not work, can anybody give me some advice


the code is where k is the int, and k is incremented


"=sum(" & K & "7:" & Range(K & "7").End(xlDown).Address(False, False) & ")"

Thanx

Reply With Quote
  #5  
Old July 31st, 2003, 09:43 AM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
i would suggest you use the cells(row,column) notation rather than the range(address) notation as follows:
Code:
...."=sum(" & range(cells(2,c),cells(2,c).end(xldown)).address(false,false) & ")"
you may need be careful with activesheet etc. if you plan to use this over multiple sheets, btw, but it's easily adjusted to cope

bracket error corrected as below

Last edited by epl : July 31st, 2003 at 10:31 AM.

Reply With Quote
  #6  
Old July 31st, 2003, 10:23 AM
juk_kuj juk_kuj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Ontario
Posts: 24 juk_kuj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thnx for the quick reply EPL, appreciated greatly!

I have a question still, (i still have an error) the

"=sum(" & Range(Cells(7, K), Cells(2, K)).End(xlDown).Address(False, False) & ")"

The second Cells(2,k)...... What wxactly is that saying, do you still not have to specify the end of the column? Cells(2,k), so how does that reach the end of the clumn when it is not hard coded?


Thnx

Reply With Quote
  #7  
Old July 31st, 2003, 10:29 AM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
yes - sorry the second cells(..).end sepcifies the end column (the range to be summed is the range(top,bottom) from the top cell(7,k) to the bottom cell(7,k).end(...) .. if that's any clearer!

so the problem is with my breacket placement - there should only be one bracket after the second cell(7,k), with the .end(...) as a subproperty of the cell, not the range - the "spare" bracket then goes after the end(...) as follows:
Code:
"=sum(" & range(cells(7,k),cells(7,k).end(xldown)).address(false,false) & ")"
apologies and hope that helps ... i'll edit the above error, if you don't mind

Last edited by epl : July 31st, 2003 at 10:32 AM.

Reply With Quote
  #8  
Old July 31st, 2003, 10:39 AM
juk_kuj juk_kuj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Ontario
Posts: 24 juk_kuj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you, again your help is cherished.

That has solved my problem, hopfully i can repay the favor some time.


Take care EPL

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > excel question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway