View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
COM COM is offline
external usenet poster
 
Posts: 40
Default Summing undetermined colums

I may have been a little short sited too... If you would rather do the totalling on a separate sheet, or on a command of a button, then the problem would be easier to solve... Given that... Might be able to produce something that would work, though the way I would do it might be a little messy.

First thing would be to set up a sheet (Companysheet) that will get updated with each "company" title, as it is pulled from your stockroom guy's current spreadsheet, and the total for that "company". After each "grouping" for a company, you would have your total field, which would reference the total on the Companysheet. (I.e. "=Companysheet!B1")

Now, when anything is updated on your currently existing sheet, VBA code in the background, figures out what companies exist in your existing sheet. Stores those results in the Companysheet, by inserting or deleting companies as necessary, maintaining an alphabetically sorted list.

The total for each company is calculated and inserted in column B for the company in question on the CompanySheet, as the sheet is parsed. Ie. finds that Company1 exists... Ensures Company1 is in the CompanySheet. Then totals all values for Company1 and stores the result. Then because the "Total" Cell for Company1 is referenced as CompanySheet!B1, the total is up-to-date.

So we would need to use the following event to "catch" the change..

Under ThisWorkbook the SheetChange event.

This event would call the module/code to start determining the companies and the totals.

Given that the first company starts say at Cell A2, then one would need to know in what column the company name, and in what column the Order price amount is located.

while current cell < ""
Companies row = insert CompanyName(CompaniesName)
While the Current Cell is < ""
Total = Total + CurrentRow's Amount
Move down one row
wend

Set the total for the Company on the CompanySheet

if the row on the currently existing spreadsheet does not have a total, then create the total and the reference to it.

Move down 2 rows 'One row is a blank, and the following row should be a company if one exists.
wend

This will have updated each company..

The insertcompany name feature would be rather simple.. Look in Column A, if the company is not found, then move down either till the end of the list, or the company to insert is before the next "bigger" and after the next "smaller" company. If the inserted company is less than the first, insert at the beginning. Progress through each company until the next company is smaller than the inserting company. If reach the end of the list, insert at the end.
If the company is in the list or is added to the list, then return the row of the new company.

That's a way that this could be accomplished, (though in pseudo code.) I might have some time tomorrow to refer back to this, and actually create it for you, or someone else might be able to do it.. :) Good luck, again sorry..