MS Office Forum / Excel / Programming / October 2008
Possible to replicate formulae
|
|
Thread rating:  |
MikeR - 27 Sep 2008 15:46 GMT I'd like a sheet to track fees received from clients. I envisioned a layout something like this. I'd like weekly, monthly and YTD totals.
I won't know in advance how many clients per week. Is there a way to programatically replicate the totals formula as rows are inserted?
How to group the weekly, monthly totals? TIA Mike
2008 September 09/01/2008 Joe Smith $200.00 09/05/2008 Jill James $175.00 October November December 2009
Otto Moehrbach - 27 Sep 2008 19:31 GMT Mike One way: Have a separate sheet for each year. In each sheet have the months across the top (row 1?) and the dates in Column A. To sum columns, simply put the sum formula at the top of the column and include a range to sum in the formula that is 3 or 4 times anything you expect. In other words, if you expect no more than 1000 cells to sum, have the formula sum 5000 cells. You can also have a Summary sheet to summarize whatever you want. Come back if you want more help as you get into it. HTH Otto
> I'd like a sheet to track fees received from clients. I envisioned a > layout something like this. I'd like weekly, monthly and YTD totals. [quoted text clipped - 14 lines] > December > 2009 MikeR - 27 Sep 2008 20:17 GMT Thanks, Otto - How does your plan accomodate the weekly and monthly totals? Did I miss something?
> Mike > One way: Have a separate sheet for each year. In each sheet have the [quoted text clipped - 23 lines] >> December >> 2009 Otto Moehrbach - 28 Sep 2008 16:09 GMT Mike I wasn't giving you the whole solution in one swack. I gave you what I did to get things started (format, layout). Once you have a layout established, you can try some formulas to give you what summations you want. If formulas won't do it, VBA is next. What layout (sheets, columns, rows) did you arrive at? What do you call a week? Monday through Friday? More? Less? How do you plan on designating what week you want to see? Perhaps something like "the 17th week of the year"? Or maybe the week from this date to that date? Or maybe all the weeks? Where do you want these summaries placed and in what format (sheets, columns, rows)? HTH Otto
> Thanks, Otto - > How does your plan accomodate the weekly and monthly totals? Did I miss [quoted text clipped - 27 lines] >>> December >>> 2009 MikeR - 29 Sep 2008 22:58 GMT The layout is pretty much as I drew it in the OP. I see it as 1 sheet. The biggest part of the puzzle is replicating whatever the formuale are on the insertion of new rows. As I said, I won't know in advance how many rows there will be.
A week is Mon. thru Fri. I want to see all the weeks. I was hoping to be able to loop thru the entries finding the week number of each entry (and the row number) and sum up wk1, wk2, wk3, etc. placing the total in a col to the right of the data columns on the last row for each week. The monthly total goes to the right of the data cols, on the row with the month names. The YTD could go on the row with thw year label.
Not sure what you mean by format in your last comment. The format for all the calculations will be currency (dollars).
All this means a pretty dynamic sheet. My location scheme is dependent on being able to designate a location in code, I suppose. I'm not sure any of this is possible, as I'm pretty new to Excel.
> Mike > I wasn't giving you the whole solution in one swack. I gave you what I [quoted text clipped - 40 lines] >>>> December >>>> 2009 MikeR - 01 Oct 2008 14:03 GMT Hello??
Otto Moehrbach - 01 Oct 2008 19:14 GMT Mike Here's what I envision. As I said before, a separate sheet for each year. In each sheet I'll assume the dates are in Column A starting with A2. Since you have a variable number of entries per month and you want the monthly sums to the right of the last entry of each month, I would use VBA instead of formulas. I will work up something for you for the month sums and I will leave the weekly sums alone for now. The weekly sums can be a bit messy. Does this sound like something you can live with? Otto
> Hello?? MikeR - 02 Oct 2008 01:08 GMT > Mike > Here's what I envision. As I said before, a separate sheet for each [quoted text clipped - 4 lines] > and I will leave the weekly sums alone for now. The weekly sums can be a > bit messy. Does this sound like something you can live with? Otto Otto Sounds OK to me. I expected the solution to INVOLVE VBA. The placement of the results isn't a big deal. The weekly sums are a bigger requirement, but I'll take what I can get. <vbg> Mike
Otto Moehrbach - 02 Oct 2008 15:34 GMT Mike I chose to write the code in 4 macros (shown below). It doesn't do anything with the weekly sums, as I said, but I'm still interested in getting the weekly sums. Tell me this: Your date column has a string of dates, as you said. Is EVERY Monday and EVERY Friday listed in that string of dates? I ask this because Excel is a beast of logic and cannot think at all. I may still be able to do it even if all the Mondays and Fridays are not listed, but the code would be more complex. The macros below are written with certain assumptions. They are: Column A has the months. Column B has the dates. Column C has the names. Column D has the amounts for each date and name. Column E has the monthly sums. Row 1 has a formula in D1 that sums Column D to give you the yearly sum. The dates can start in any row below row 2. The only macro that you want to run is the GetTotals macro. All the other macros are called by that one macro. The code does this: Clears ALL of Columns A & E from Row 3 down. Sorts Columns B:D by the dates; Finds all the cells in the date column that belong to the same month as the first date. Places the month in Column A one cell able the first date of that month. Sums Column D (for that month) and places that number in Column E in the row of the month. Looks for the next month and repeats everything for that month. Note, and this is important, this code will operate on the ACTIVE sheet, so you will need to make sure that you have chosen the right sheet before you run this code. I can include an error trap in this code to prevent the code from running on a wrong sheet, if you wish, but you will first have to tell me what constitutes a right and wrong sheet. If you wish, send me an email and I'll send you the small file I used to develop this code. My email address is moehrbachextra@bellsouth.net. Remove the "extra" from this address. HTH Otto
Sub GetTotals() Call ClearColAE Call SortByDate Call GetMonthSums End Sub
Private Sub ClearColAE() 'Clear (erase) Columns A & E Range("A3", Range("A" & Rows.Count)).ClearContents Range("E3", Range("E" & Rows.Count)).ClearContents End Sub
Private Sub SortByDate() Dim rDates As Range Set rDates = Range("B2", Range("B" & Rows.Count).End(xlUp)) rDates.Resize(, 3).Sort Key1:=Range("B3"), Order1:=xlAscending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom End Sub
Private Sub GetMonthSums() Dim FirstCell As Range Dim LastCell As Range Dim TheMonth As Long Dim c As Long Set FirstCell = Range("B3") Do Until FirstCell = "" TheMonth = Month(FirstCell) For c = 1 To 1000 If IsEmpty(FirstCell.Offset(c)) Or _ Month(FirstCell.Offset(c)) <> TheMonth Then Set LastCell = FirstCell.Offset(c - 1) Exit For End If Next c FirstCell.EntireRow.Insert FirstCell.Offset(-1, -1).Value = _ Format(FirstCell.Value, "mmmm") FirstCell.Offset(-1, -1).Resize(, 5).Font.Bold = True FirstCell.Offset(-1, 3).Value = _ Application.Sum(Range(FirstCell, LastCell).Offset(, 2)) Set FirstCell = LastCell.Offset(1) Loop End Sub
>> Mike >> Here's what I envision. As I said before, a separate sheet for each [quoted text clipped - 11 lines] > but I'll take what I can get. <vbg> > Mike MikeR - 03 Oct 2008 03:32 GMT Otto Re dates: There may be no entries for any given day. I suppose it would be OK to put in dates with no amount, but if it's possible not to do that, I'd prefer not to.
I moved the monthly sums to col F.
Nothing is calculating, not even the formula for YTD....
I'll send an e-mail.
> Mike > I chose to write the code in 4 macros (shown below). It doesn't do [quoted text clipped - 91 lines] >> but I'll take what I can get. <vbg> >> Mike MikeR - 03 Oct 2008 03:58 GMT > If you wish, send me an email and I'll send you the small file I used to > develop this code. My email address is moehrbachextra@bellsouth.net. > Remove the "extra" from this address. HTH Otto Otto I e-mailed you but it bounced. If you want to try e-mailing me, it's nf4lNoSpam@pobox.com (remove the obvious)
MikeR - 10 Oct 2008 14:48 GMT Did you get my e-mail response to your e-mail?
Otto Moehrbach - 10 Oct 2008 17:22 GMT Mike The last I sent you (email) is below. No, I have not gotten an email response to this. moehrbachoextra@bellsouth.net, remove the "extra. Otto
> Did you get my e-mail response to your e-mail? MikeR - 13 Oct 2008 16:26 GMT > Mike > The last I sent you (email) is below. No, I have not gotten an email > response to this. moehrbachoextra@bellsouth.net, remove the "extra. Otto >> Did you get my e-mail response to your e-mail? Hmmmm..... Can you check your spam folder (if you have one)? I clicked reply to send to you. Here's the e-mail I sent on the 6th. ====================================================================================
> Mike > I think I forgot to attach the file. Otto Got it Otto -
The button moves down the page every time it's clicked, and on the Data sheet, grows in height to 6 rows high. Prehaps it can be in a non-scrolling area? I can't figure out how to move it. I'd like to insert a column between C & D for payment type.
>> What did you do with the macros? I copied and pasted them into a module.
>>Where did you place them in your file? I don't understand the question.
>>What did you do to run them? Nothing :-(
>>I placed a button on the sheet to run the macro, just for my convenience. Where are the methods/properties of the button?
>> Notice the layout of everything. The layout is very important because the code is dumb and is written to work with that specific layout. >>I can change the code to work with any layout you want. I used the "Data" sheet. The "Original" sheet was just to save my starting >>point. OK
>>Read again what I said in my post about what the macros do. What version of Excel are you running? I running 2K. Clearing column E is what removes the formatting, if I format a totals cell for currency, right?
I don't understand the following from your post. Why/how could there be an incorrect sheet?
Note, and this is important, this code will operate on the ACTIVE sheet, so you will need to make sure that you have chosen the right sheet before you run this code. I can include an error trap in this code to prevent the code from running on a wrong sheet, if you wish, but you will first have to tell me what constitutes a right and wrong sheet.
Getting there...Trying to understand the code. I'll have to find a good book on Excel VBA.
Mike
Otto Moehrbach - 13 Oct 2008 17:43 GMT Apparently we have a problem with email. Here is my last email to you: Mike
I used a button just for my convenience. You can run the macro in several different ways if you don't want a button. You can click on Tools - Macro - Macros and click on the macro name and click on OK. Or you can set a hot key to run it. Something like Ctrl-Shift-X. I don't know how much you know about these things so just ask
If you want, you can make the first row higher and move the button into that row. To move the button, right-click on the button, then left-click on the button. Then move the mouse cursor around on the button until it changes to a cross. Then hold the left mouse button down and move the button.
You said:
Where are the methods/properties of the button?
The button is a Forms button. This means that the button was created by clicking on View - Toolbars - Forms, then selecting the button, placing the button, and assigning the macro to it. Right-click on the button and select Format Control. That shows you everything that you can do with the button. Not much.
You said:
Clearing column E is what removes the formatting, if I format a totals
cell for currency, right?
"Clearing" in Excel-speak means to erase the contents only. If you clear Column E you will clear the contents of all the cells in Column E. This will have no effect whatsoever on the formatting currently present in Column E.
You said:
I don't understand the following from your post. Why/how could there be
an incorrect sheet?
Note, and this is important, this code will operate on the ACTIVE sheet, so
you will need to make sure that you have chosen the right sheet before you
run this code. I can include an error trap in this code to prevent the code
from running on a wrong sheet, if you wish, but you will first have to tell
me what constitutes a right and wrong sheet.
It is common for people to have more than one sheet in a workbook. In your case you would have a sheet like you described and that is the sheet that you want THIS code to work with. But you could have some other sheet that holds some other stuff that you don't want THIS code to mess with. Remember that the code is dumb. I wrote this code to work on the active sheet. As such this code will do this and that and move this here and that there and so forth and so forth ON WHATEVER SHEET IS THE CURRENT ACTIVE SHEET. Obviously, having some other sheet active and running this code is going to mess up what is on that sheet. If you use a button on the sheet to run the macro, you will always have the proper sheet active when the code runs. But if you use some other method of running that macro, then you would have to take care that the right sheet is active. As I said before, I can write the code to check what sheet is active before it changes anything or I can write the code to work with the proper sheet regardless of what sheet is active at the time. To do this, of course, would have to know how to identify what is the right sheet and what isn't. Again, this code is written to work on the active sheet. Let me know of any changes you want.
A good VBA book is "Microsoft Office Excel XXXX Power Programming With VBA" by John Walkenbach. The "XXXX" is the most recent version (year) that the particular book covers. You want to buy the newest book available since all the books cover every version up to and including the year in the title. I found Amazon.com has the best prices.
Does the code do what you want on the file I sent you? If you want to add/delete columns, do so on the Data sheet of that file and send it back to me with an explanation of what changes you made/want. Otto
>> Mike >> The last I sent you (email) is below. No, I have not gotten an email [quoted text clipped - 54 lines] > > Mike MikeR - 14 Oct 2008 15:49 GMT > Apparently we have a problem with email. Here is my last email to you: Seems so... I wonder what the problem is? I just found this email and replied to it. Lets see if it makes it. But if not, here's where I am: I was able to add a column and adjust your macro to suit.
> Mike > [quoted text clipped - 3 lines] > can set a hot key to run it. Something like Ctrl-Shift-X. I don't know how > much you know about these things so just ask I know almost nothing.
> If you want, you can make the first row higher and move the > button into that row. To move the button, right-click on the button, then > left-click on the button. Then move the mouse cursor around on the button > until it changes to a cross. Then hold the left mouse button down and move > the button. OK. And also checked "Don't move or size with cells".
> You said: > [quoted text clipped - 5 lines] > Format Control. That shows you everything that you can do with the button. > Not much. Right.
> You said: > [quoted text clipped - 6 lines] > will have no effect whatsoever on the formatting currently present in Column > E. OK.
> You said: > [quoted text clipped - 8 lines] > from running on a wrong sheet, if you wish, but you will first have to tell > me what constitutes a right and wrong sheet. I just removed the "original" sheet. There will be only one. If the end user adds one, that's their mistake, and I'll deal with that.
> It is common for people to have more than one sheet in a workbook. In your > case you would have a sheet like you described and that is the sheet that [quoted text clipped - 13 lines] > is the right sheet and what isn't. Again, this code is written to work on > the active sheet. Let me know of any changes you want. See above.
> A good VBA book is "Microsoft Office Excel XXXX Power > Programming With VBA" by John Walkenbach. The "XXXX" is the most recent > version (year) that the particular book covers. You want to buy the newest > book available since all the books cover every version up to and including > the year in the title. I found Amazon.com has the best prices. I'll take a look at it.
> Does the code do what you want on the file I sent you? If you > want to add/delete columns, do so on the Data sheet of that file and send it > back to me with an explanation of what changes you made/want. Yes it does, and thanks!
I'm working on the weekly totals now. and here's my plan. Please adjust as needed. I'll loop thru the date column, getting the week number for a row, and as long as the week number is the same, add the dollar amount to an accumulator, called, say, WkTot. When the week number changes, write WkTot to the last row for the prev week number, in the same col as the monthly totals, but not in bold font and zero WkTot.
Since I'm doing this after your macro, I'll have to deal with the blank lines (where the month name is).
|
|
|