+ Reply to Thread
Results 1 to 25 of 25

Summing across unknown range in unknown cell!

  1. #1
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Summing across unknown range in unknown cell!

    Hey folks,

    I've been working on a spreadsheet and these forums have been a great help. I'm now at the very last section and, surprise surprise, it's also the hardest!

    I'm creating a stock trade recording sheet. I have a userform ask the user to enter a date, a time, the number of stock purchased, and the price of the stock. These are then entered in a new row.

    Now what I want to do is have summary cells which say how many stocks were purchased and the total profit made for each day. Since each time is given its own row, I can't know in advance which rows to sum over. I also don't know on which days a trade was made. So a summary cell should only exist if a trade was made that day.

    Could I do something like.... check if the date matches then sum over all the values for that date? So if column A has the dates, can I say "Search which rows in column A have this date" then "for those rows, sum column C"?

    Also, how would I create a a row for each traded date's summary cell and enter the date in it? I've attached a spreadsheets which manually demonstrates what I want to do (no macros) and a spreadsheet with what I have so far (basic macros).

    Any and all help much appreciated, I just need to get my head around creating and dealing with variable ranges. Is that a really advanced task? I don't think this is a one line solution so please bear with me while I make mistakes!

    Many thanks!
    AR
    Attached Files Attached Files
    Last edited by agentred; 10-28-2009 at 09:52 AM.

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Summing across unknown range in unknown cell!

    Hi,

    What about a simple pivot table ... ? Could that solve your problem ...?

    HTH

  3. #3
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Hey thanks for the reply. I think a pivot table might work but I've never programmed one using a macro before. At which point would it be created/updated? Would I then pull the value out of the pivot table and store it in my separate excel sheet?

    It's something to think about but I'm not sure how difficult it would be... I'd like everything to be automatic, i.e. requiring no human interaction at all after entering initial values.
    Last edited by DonkeyOte; 10-26-2009 at 05:11 AM. Reason: unnecessary quote removed

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    You can set the Pivot Table data source to be a Dynamic Named Range - (ie one that adapts as you add/remove data to your source sheet) - for info. on creating such a range see the link in my sig. to Contextures tutorial.

    You can as part of the User Form routine refresh the PT once the data has been added - thus no action on part of end-user is required other than entering the data as is presently the case.

  5. #5
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Eeep OK thank you for pointing me in the right direction. I'll start reading the dynamic named ranges tutorial and try to implement the method.

    One quick question before I start though... For each date, I have a summary row, so it'll go

    oct 1st... 9 am... this trade at this price
    oct 1st... 10 am... another trade at another price
    oct 1st... 1pm... another trade at another price.
    oct 1st.... ... .... summary of oct 1st trades
    oct 2nd... 9am... trade at price

    Will this summary row cause a problem in the pivot table? Will missed dates, i.e. can I go from Oct 1st to Oct 5th without empty date rows in the pivot table? Thanks again,

    AR

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    Quote Originally Posted by agentred View Post
    Will this summary row cause a problem in the pivot table?
    Given the summary row can be distinguished from the non-summary rows by means of various columns - eg Commodity is blank - you can filter out these rows quite easily.

    Will missed dates... ?
    No. In fact this lack of functionality is often a gripe for people with PT's as users often "want" to see data for each day even if it does not exist (ie the opposite of yourself) ... without "dummy data" for the missing days at source this isn't really possible with a Pivot Table - it will only really display that which exists @ source.

  7. #7
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Quote Originally Posted by DonkeyOte View Post
    No. In fact this lack of functionality is often a gripe for people with PT's as users often "want" to see data for each day even if it does not exist (ie the opposite of yourself) ... without "dummy data" for the missing days at source this isn't really possible with a Pivot Table - it will only really display that which exists @ source.
    OK Thank you for your help, I read up the links in your signature about dynamic ranging but since I'm using excel 2007 I think it's easier for me to create a table.

    So now I have an automatically expanding table which I can use to create a pivot table. But how do I have the pivot table auto-update as new rows are added?

    So far everything has been in Excel, will I need to move back in to the VBA codes for this? I have John Walkenbach's Power programming book which has a chapter about pivot tables so I'll start reading that. Is it difficult?

    It feels like every time I take a step forward I end up taking two steps back!

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    Yes you will need to use VBA but I thought from earlier posts this was already the case (user form) ?

    If you are using VBA userform for data entry to update the Table then I would suggest coding a refresh as part of that routine else you are looking at using the Worksheet_Change event, eg:

    Please Login or Register  to view this content.
    Change that in red to reflect

    a) name of your Table
    b) name of the sheet on which PT resides
    c) name of the PT itself

    To insert the above, right click on the sheet containing your Table, select View Code and paste above into resulting window thereafter ensuring macros are enabled.

    The above may not work for you 100% but in that case we would really need to see the proposed layout etc of both data & PT before being able to comment / advise further.

  9. #9
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Hey yeah I just hadn't used vba for the tables so was hoping I could autorefresh everything without having to jump back in to it.

    But thank you for the code, it seems like just what I'll need once I've set up the pivot table. Will try implementing everything and get back to the boards, gracias,

  10. #10
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Alright excellent!

    Thank you again for the advice, I've managed to create a table and pivot table which auto update as necessary.

    BUT (and there's always a but) I need to do calculations with the results of the pivot table and have the results updated in the same row. So for instance,

    Row Date Trades Profit Net Profit Total Capital
    1 1/1/2009 7 $70 (Profit - Trades*0.02) $50 + profit
    2 1/2/2009 6 $80 (Profit - trades*0.02 ) Total capital from 1/1/2009+profit

    So the first three columns in blue are from my pivot table, and they update automatically from the data. But how do I get the next two columns to work? Any ideas? Is it even possible? Because net profit references the value above as well as the value from a pivot table field.

    What I've thought of doing is creating another macro, which is semi-automatic, that is, you click on the net profit cell and run the macro. It then uses offsets to calculate net profit and Total capital. I'm currently writing that up, but is there an easier way?

    I mean, thanks to all your help I've come this far, I can probably calculate the last couple fields by hand if I have to but... to see perfection just inches away from my grasp...!

    Have attached a demo sheet in case the table above doesn't make things very clear... ...And I just realised that a 2007 pivot table isn't compatible with excel 2003 sheets so I hope nobody minds the sheet being an xlsm
    Attached Files Attached Files
    Last edited by agentred; 10-28-2009 at 02:34 AM.

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    You can add Calculated Items etc to Pivot Tables but they can get quite convoluted quite quickly and also lead to undesired layouts.
    Therefore I'd probably err on your side and opt to add the calculations on the fly as part of the routine though I would add reverse the position of the Total Capital and Return on Capital columns (such that Return looks to left for Total Capital rather than to the right).

    In basic terms... this would be a revised PT Update routine to insert the formulae etc - it assumes that the layout of the PT is rigid horizontally (not vertically of course)

    Please Login or Register  to view this content.
    If you expect the PT to contract vertically as well as expand you would need to add some code to remove surplus (legacy) calculations.

  12. #12
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Oh no Excel just crashed!! What's worse, it corrupted my file completely, I have to go back to the file I uploaded here several hours ago...

    Aaaargh! This is the first time this has ever happened with Excel or any Office program.

    But thank you for the code, it looks perfect, lemme try implementing it!

  13. #13
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Hey thank you so much! It's working almost perfectly now, everything is updated and calculated automatically.

    There was one thing I couldn't figure out though, for the very last column, % on capital, you wrote

    Please Login or Register  to view this content.
    So that calculates the day's profit divided by the capital remaining at the end of the day. But I'd like the day's profit divided by the previous day's capital. So for instance, if we have $100

    And On Day 1: We make $10, % return on capital = 10/100 = 10%
    On Day 2: We make $20. % return on capital should be 20/110 (Day 1's closing capital) but currently it's 20/130 (Day 2's closing capital).

    I think I haven't quite grasped the RC[-1] sructure, I tried using RC[-1,-1] but it didn't work...

    Thank you so much, again, I've gone from total vba neophyte to actually understanding what's happening in code thanks to this site!

  14. #14
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    Apologies, I overlooked that when I was setting up, on which basis perhaps simplest to revise to:

    Please Login or Register  to view this content.
    Also if you don't want calcs for the Grand Total rows you can resize the range

    Please Login or Register  to view this content.

  15. #15
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Summing across unknown range in unknown cell!

    Quote Originally Posted by agentred View Post
    Hey thank you so much! It's working almost perfectly now, everything is updated and calculated automatically.


    I think I haven't quite grasped the RC[-1] sructure, I tried using RC[-1,-1] but it didn't work...

    Thank you so much, again, I've gone from total vba neophyte to actually understanding what's happening in code thanks to this site!
    Hi,

    R stands for Row and C stands for Column in relative terms ...
    i.e. RC[-1,-1] would mean : minus 1 row and minus 1 column to find the relative number ...

    HTH

  16. #16
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    i.e. RC[-1,-1] would mean : minus 1 row and minus 1 column to find the relative number ...
    I think you mean R[-1]C[-1]....

    agentred, for a very brief overview of R1C1 notation see a very basic outline I posted here: http://www.excelforum.com/2171545-post2.html
    (there are plenty of better, more thorough resources on the net if you wish to google etc...)
    Last edited by DonkeyOte; 10-28-2009 at 09:12 AM. Reason: added link for benefit of OP

  17. #17
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Quote Originally Posted by DonkeyOte View Post
    Please Login or Register  to view this content.
    Also if you don't want calcs for the Grand Total rows you can resize the range

    Please Login or Register  to view this content.
    No need to apologise, that's perfect, grazie!

    Quote Originally Posted by JeanRage View Post
    Hi,

    R stands for Row and C stands for Column in relative terms ...
    i.e. RC[-1,-1] would mean : minus 1 row and minus 1 column to find the relative number ...

    HTH
    Thanks HTH, I figured it was something like that...

  18. #18
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    Good. Please remember to mark thread as solved.

    Incidentally...

    HTH is forum speak for Hope That Helps ... also note the R1C1 syntax you used was incorrect on which basis I added a link for you to review at your leisure.

  19. #19
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Ahh actually I changed it to

    Please Login or Register  to view this content.
    And this works fine for all the rows except the very first one, which gives #value. Should I just calculate that one by hand? Using

    Please Login or Register  to view this content.
    didn't seem to work

  20. #20
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    The code provided works without issue on your sample file so I'm afraid you will need to upload a further example in which it is failing... I would expect a #VALUE! error only if certain referenced cells are generating text rather than numbers (ie coercion error).

  21. #21
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Quote Originally Posted by DonkeyOte View Post
    The code provided works without issue on your sample file so I'm afraid you will need to upload a further example in which it is failing... I would expect a #VALUE! error only if certain referenced cells are generating text rather than numbers (ie coercion error).
    Yep well the second row divides by the value in the first row, so everything from that point is fine, but the first row divides by the header thus causing the #VALUE! error.

    Row 1 =$C41/(G40)
    Row 2 =$C42/(G41)
    Row 3 =$C43/(G42)

    But the header's in G40... Please find attached the file to demonstrate,
    Attached Files Attached Files

  22. #22
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    You're not using the formula I gave you... ??

    The formula provided previously will take the current Total Capital G41 and subtract from that the current P/L C41 -> that value is equiv. to your opening Total Capital balance.

    The reason I provided that approach was for this very reason !
    (ie your opening balance is not available on the row prior to first transaction so for consistency calculate Opening balance by taking End Balance less Periodic Movement)


    (i should add the reason I was confused it wasn't working was because in post # 19 you said you had modified it but then repeated the formula I had given you which should have worked - ie you did not detail the formula used above (which won't))
    Last edited by DonkeyOte; 10-28-2009 at 09:41 AM.

  23. #23
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Yep, you're completely right. My mistake, many apologies!

    Thanks again, I think I can mark this as solved. Thank you also for the link, I'm looking forward to reading up on it,

    AR

    Quote Originally Posted by DonkeyOte View Post
    You're not using the formula I gave you... ??

    The formula provided previously will take the current Total Capital G41 and subtract from that the current P/L C41 -> that value is equiv. to your opening Total Capital balance.

    The reason I provided that approach was for this very reason !
    (ie your opening balance is not available on the row prior to first transaction so for consistency calculate Opening balance by taking End Balance less Periodic Movement)


    (i should add the reason I was confused it wasn't working was because in post # 19 you said you had modified it but then repeated the formula I had given you which should have worked - ie you did not detail the formula used above (which won't))

  24. #24
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Summing across unknown range in unknown cell!

    I notice you may have multiple sheets and it looks like you are intending to repeat the code for each sheet ... given the layouts of each sheet are

    a) seemingly identical (columns specifically)
    b) table names are consistent and match sheet names (TradesTablemmm)
    c) pivot table names are consistent and match sheet names (SummaryPivotmmm)

    (by sheet name consistency I mean the sheets too are named using mmm construct and therefore you can from sheet name determine both the appropriate Table range & Pivot Table for that sheet)

    then you should consider using the Workbook level Sheet Change event and thus have only routine which will be applied to all monthly sheets.

    The below, pasted into ThisWorkbook, would replace all code in both Oct & Nov sheets (ie that code should be removed in full)

    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Summing across unknown range in unknown cell!

    Quote Originally Posted by DonkeyOte View Post
    I notice you may have multiple sheets and it looks like you are intending to repeat the code for each sheet ... given the layouts of each sheet are

    a) seemingly identical (columns specifically)
    b) table names are consistent and match sheet names (TradesTablemmm)
    c) pivot table names are consistent and match sheet names (SummaryPivotmmm)
    Ahh excellent, you're a saint! I was thinking I could upgrade the code to workbook level when I have more experience under my belt, but that code'll life much much easier. My most sincere thanks,

    AR

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1