VBBlindman
New Member
- Joined
- Sep 21, 2012
- Messages
- 22
Hi, I'm new to this forum and this is my first post, however I have been reading threads from here for many months and finally got the nerve to try and join in. I may have gone way overboard with what I'm asking help with here, but I'm at my wits end and feel like a very stupid person.
You can skip this next paragraph if you want, I wanted to tell you that I have tried to do this on my own and what I have been doing to get to that end. I have spent the last past 3 or more months reading Excel 2002 VBA Programmers Reference, Access 97 Bible, Excel Version 5, Visual Basic 6, hundreds (perhaps thousands) of google searches, countless hours at Barnes & Nobles and at the public library reading books I can no longer remember. I've typed a ba-zillion cell formulas (okay maybe not a ba-zillion), many that even worked, but in just about every case none would work when I tried to write them into a VB module sheet as a sub() procedure. Any code or procedures I copied from the Web would work, but as soon as I made a modification (minor from what I thought) to fit my needs it would bomb.
I will attemp to explain what I'm trying to accomplish.
Below is a sample spreadsheet (provided I did the HTML maker correctly) with a small sample of data with an area that I'm trying to populate with results from what I think are simple straight forward calulations. Today I do this with pivot tables, cell formulas and brute force, but it takes weeks because the actual data I'm dealing with is 79,000 items and 750,000 lines of cost data.
The desired result is to write a VB sub macro titled ImpactCalc() that fills in cells E9 thru P11 with the cost impact based on the information in the table titled "Data." The calculation is done by comparing the "Item A" number and "Month A" to what is in "Data," when a match is found take the corresponding "Last Cost" minus the "New Cost" times the "Qty" for that month. When there is more than one set of data for a month for the item it must temporarily store the previous calculation result to be subsequently added to the next additional calculation for the next impact. However, the next calculation for the duplicate month does not use the value from "Last Cost", it takes the "New Cost" from the previous calculation minus the next new cost for the duplicate months data times that next corresponding qty. It keeps looping through and incrementing the new cost and qty for each duplicate month until the next different month. It again temporily saves the last new cost used for the start of the next different months calculations and loops through until all months are filled in for that "Item A" then resets to start the next "Item A."
Example for item 32-99954-00, Months 1 and 2 would calc to zero as there is no 1 or 2 in "Data." Month 3 however has duplicates so the first calc is (881 - 994) x 60, then (994 - 1,015) x 72, then (1,015 - 976) x 72 all added together and placed in cell G9. Month 4 is then (976 - 944) x 72 and placed in cell H9. There is no month 5 data so I9 would have zero placed in it. Month 6 would be (944 - 999) x 38 and value placed in cell J9, and so on. Once month 12 is filled in, reset and do the calculations for the next "Item A" using its "Last Cost" to start with.
Hopefully I've given enough to make sense and not to much to confuse.
Any assistance it truly appreciated and if this is way too much how about a recommendation on reference material I can go study.
Excel 2007
<COLGROUP><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL></COLGROUP><THEAD>
</THEAD><TBODY>
</TBODY>
<TBODY>
</TBODY>
<TBODY>
</TBODY>
You can skip this next paragraph if you want, I wanted to tell you that I have tried to do this on my own and what I have been doing to get to that end. I have spent the last past 3 or more months reading Excel 2002 VBA Programmers Reference, Access 97 Bible, Excel Version 5, Visual Basic 6, hundreds (perhaps thousands) of google searches, countless hours at Barnes & Nobles and at the public library reading books I can no longer remember. I've typed a ba-zillion cell formulas (okay maybe not a ba-zillion), many that even worked, but in just about every case none would work when I tried to write them into a VB module sheet as a sub() procedure. Any code or procedures I copied from the Web would work, but as soon as I made a modification (minor from what I thought) to fit my needs it would bomb.
I will attemp to explain what I'm trying to accomplish.
Below is a sample spreadsheet (provided I did the HTML maker correctly) with a small sample of data with an area that I'm trying to populate with results from what I think are simple straight forward calulations. Today I do this with pivot tables, cell formulas and brute force, but it takes weeks because the actual data I'm dealing with is 79,000 items and 750,000 lines of cost data.
The desired result is to write a VB sub macro titled ImpactCalc() that fills in cells E9 thru P11 with the cost impact based on the information in the table titled "Data." The calculation is done by comparing the "Item A" number and "Month A" to what is in "Data," when a match is found take the corresponding "Last Cost" minus the "New Cost" times the "Qty" for that month. When there is more than one set of data for a month for the item it must temporarily store the previous calculation result to be subsequently added to the next additional calculation for the next impact. However, the next calculation for the duplicate month does not use the value from "Last Cost", it takes the "New Cost" from the previous calculation minus the next new cost for the duplicate months data times that next corresponding qty. It keeps looping through and incrementing the new cost and qty for each duplicate month until the next different month. It again temporily saves the last new cost used for the start of the next different months calculations and loops through until all months are filled in for that "Item A" then resets to start the next "Item A."
Example for item 32-99954-00, Months 1 and 2 would calc to zero as there is no 1 or 2 in "Data." Month 3 however has duplicates so the first calc is (881 - 994) x 60, then (994 - 1,015) x 72, then (1,015 - 976) x 72 all added together and placed in cell G9. Month 4 is then (976 - 944) x 72 and placed in cell H9. There is no month 5 data so I9 would have zero placed in it. Month 6 would be (944 - 999) x 38 and value placed in cell J9, and so on. Once month 12 is filled in, reset and do the calculations for the next "Item A" using its "Last Cost" to start with.
Hopefully I've given enough to make sense and not to much to confuse.
Any assistance it truly appreciated and if this is way too much how about a recommendation on reference material I can go study.
Excel 2007
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | A | C | D | E | F | G | H | I | J | K | L | M | N | O | P | |
2 | ||||||||||||||||
3 | ||||||||||||||||
4 | ||||||||||||||||
5 | ||||||||||||||||
6 | ||||||||||||||||
7 | Month A | |||||||||||||||
8 | 8 | Item A | Last Cost | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | |
9 | 9 | 32-99954-00 | 881 | |||||||||||||
10 | 10 | 32-99954-01 | 19 | |||||||||||||
11 | 11 | 32-99954-02 | 31 | |||||||||||||
12 | ||||||||||||||||
13 | ||||||||||||||||
14 | ||||||||||||||||
15 | ||||||||||||||||
16 | ||||||||||||||||
17 | Data | |||||||||||||||
18 | 18 | Item | New Cost | Qty | Month | |||||||||||
19 | 19 | 32-99954-00 | 994 | 60 | 3 | |||||||||||
20 | 20 | 32-99954-00 | 1,015 | 72 | 3 | |||||||||||
21 | 21 | 32-99954-00 | 976 | 72 | 3 | Cell formula testing examples | ||||||||||
22 | 22 | 32-99954-00 | 944 | 72 | 4 | 60 | direct range references | |||||||||
23 | 23 | 32-99954-00 | 999 | 38 | 6 | 60 | Named ranges | |||||||||
24 | 24 | 32-99954-00 | 987 | 72 | 11 | |||||||||||
25 | 25 | 32-99954-00 | 996 | 72 | 11 | Find duplicates | ||||||||||
26 | 26 | 32-99954-00 | 996 | 48 | 11 | 32-99954-00 | 996 | |||||||||
27 | 27 | 32-99954-01 | 24 | 3 | 6 | 32-99954-01 | 24 | |||||||||
28 | 28 | 32-99954-01 | 19 | 15 | 7 | 32-99954-01 | 19 | |||||||||
29 | 29 | 32-99954-01 | 19 | 10 | 7 | 32-99954-01 | 19 | |||||||||
30 | 30 | 32-99954-01 | 20 | 13 | 8 | 32-99954-01 | 20 | |||||||||
31 | 31 | 32-99954-01 | 20 | 7 | 9 | |||||||||||
32 | 32 | 32-99954-01 | 21 | 17 | 11 | |||||||||||
33 | 33 | 32-99954-02 | 31 | 18 | 1 | |||||||||||
34 | 34 | 32-99954-02 | 31 | 20 | 2 | |||||||||||
35 | 35 | 32-99954-02 | 31 | 16 | 4 | |||||||||||
36 | 36 | 32-99954-02 | 32 | 14 | 4 | |||||||||||
37 | 37 | 32-99954-02 | 33 | 16 | 6 | |||||||||||
38 | 38 | 32-99954-02 | 46 | 11 | 6 | |||||||||||
39 |
<COLGROUP><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL></COLGROUP><THEAD>
</THEAD><TBODY>
</TBODY>
Data
Array Formulas
<THEAD> </THEAD><TBODY> </TBODY> Note: Do not try and enter the {} manually yourself |
<TBODY>
</TBODY>
Worksheet Defined Names
<THEAD> </THEAD><TBODY> </TBODY> |
<TBODY>
</TBODY>