Re: VBA Inserting columns and keeping formulas in Table Ranges
Hi Prisch,
I think i have some final codes for you and will drop them off for Today or early tomorrow..
But in the mean time I think we may have overlooked something....
Please review again carefully the screen shots I did for you in Post #2. I think they may be in error.
Looking at the “
After” shots, I think the formulas are in error as they are missing some first rows and and first columns..
As example...
When data columns extend to G, the sum of say
=SUM(C5:F5)
Changes to
=SUM(
D5:G5)
When in fact it should correctly be
=SUM(
C5:G5)
And so on.
I see this is the result of the relative range references. It would be quite complicated for the code to allow for that, ( i think
)
I suggest that the Spreadsheet should be modified to have a few absolute references such as the following
The before should be modified such
Using Excel 2007
[Table="width:, class:grid"][tr][td]Row\Col[/td][td]B
[/td][td]C
[/td][td]D
[/td][td]E
[/td][td]F
[/td][td]G
[/td][/tr][tr][td]4
[/td][td="bgcolor:#E2EFD9"]Salesperson[/td][td="bgcolor:#E2EFD9"]Apr 16
[/td][td="bgcolor:#E2EFD9"]=EDATE(C4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(D4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(E4,1)
[/td][td="bgcolor:#E2EFD9"]Total
[/td][/tr]
[tr][td]5
[/td][td]Staff 1[/td][td]500.00
[/td][td]200.00
[/td][td]100.00
[/td][td]700.00
[/td][td]=SUM($C5:F5)
[/td][/tr]
[tr][td]6
[/td][td]Staff 2[/td][td]450.00
[/td][td]500.00
[/td][td]600.00
[/td][td]200.00
[/td][td]=SUM($C6:F6)
[/td][/tr]
[tr][td]7
[/td][td]Staff 3[/td][td]300.00
[/td][td]150.00
[/td][td]300.00
[/td][td]400.00
[/td][td]=SUM($C7:F7)
[/td][/tr]
[tr][td]8
[/td][td]Total[/td][td]=SUM(C$5:C7)
[/td][td]=SUM(D$5:D7)
[/td][td]=SUM(E$5:E7)
[/td][td]=SUM(F$5:F7)
[/td][td]=SUM($C8:F8)
[/td][/tr]
[tr][td]9
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]10
[/td][td="bgcolor:#FFF3CB"]Expenses[/td][td="bgcolor:#FFF3CB"]Apr 16
[/td][td="bgcolor:#FFF3CB"]=EDATE(C10,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(D10,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(E10,1)
[/td][td="bgcolor:#FFF3CB"]Total
[/td][/tr]
[tr][td]11
[/td][td]category 1[/td][td]100.00
[/td][td]100.00
[/td][td]100.00
[/td][td]100.00
[/td][td]=SUM($C11:F11)
[/td][/tr]
[tr][td]12
[/td][td]category 2[/td][td]50.00
[/td][td]50.00
[/td][td]50.00
[/td][td]50.00
[/td][td]=SUM($C12:F12)
[/td][/tr]
[tr][td]13
[/td][td]category 3[/td][td]20.00
[/td][td]20.00
[/td][td]20.00
[/td][td]20.00
[/td][td]=SUM($C13:F13)
[/td][/tr]
[tr][td]14
[/td][td]Total[/td][td]=SUM(C$11:C13)
[/td][td]=SUM(D$11:D13)
[/td][td]=SUM(E$11:E13)
[/td][td]=SUM(F$11:F13)
[/td][td]=SUM($C14:F14)
[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
Budget[/td][/tr][/table]
Then running the codes i have done for you give results such as the following
For a row addition
Using Excel 2007
[Table="width:, class:grid"][tr][td]Row\Col[/td][td]B
[/td][td]C
[/td][td]D
[/td][td]E
[/td][td]F
[/td][td]G
[/td][/tr][tr][td]4
[/td][td="bgcolor:#E2EFD9"]Salesperson[/td][td="bgcolor:#E2EFD9"]Apr 16
[/td][td="bgcolor:#E2EFD9"]=EDATE(C4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(D4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(E4,1)
[/td][td="bgcolor:#E2EFD9"]Total
[/td][/tr]
[tr][td]5
[/td][td]Staff 1[/td][td]500.00
[/td][td]200.00
[/td][td]100.00
[/td][td]700.00
[/td][td]=SUM($C5:F5)
[/td][/tr]
[tr][td]6
[/td][td]Staff 2[/td][td]450.00
[/td][td]500.00
[/td][td]600.00
[/td][td]200.00
[/td][td]=SUM($C6:F6)
[/td][/tr]
[tr][td]7
[/td][td]Staff 3[/td][td]300.00
[/td][td]150.00
[/td][td]300.00
[/td][td]400.00
[/td][td]=SUM($C7:F7)
[/td][/tr]
[tr][td]8
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]=SUM($C8:F8)
[/td][/tr]
[tr][td]9
[/td][td]Total[/td][td]=SUM(C$5:C8)
[/td][td]=SUM(D$5:D8)
[/td][td]=SUM(E$5:E8)
[/td][td]=SUM(F$5:F8)
[/td][td]=SUM($C9:F9)
[/td][/tr]
[tr][td]10
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]11
[/td][td="bgcolor:#FFF3CB"]Expenses[/td][td="bgcolor:#FFF3CB"]Apr 16
[/td][td="bgcolor:#FFF3CB"]=EDATE(C11,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(D11,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(E11,1)
[/td][td="bgcolor:#FFF3CB"]Total
[/td][/tr]
[tr][td]12
[/td][td]category 1[/td][td]100.00
[/td][td]100.00
[/td][td]100.00
[/td][td]100.00
[/td][td]=SUM($C12:F12)
[/td][/tr]
[tr][td]13
[/td][td]category 2[/td][td]50.00
[/td][td]50.00
[/td][td]50.00
[/td][td]50.00
[/td][td]=SUM($C13:F13)
[/td][/tr]
[tr][td]14
[/td][td]category 3[/td][td]20.00
[/td][td]20.00
[/td][td]20.00
[/td][td]20.00
[/td][td]=SUM($C14:F14)
[/td][/tr]
[tr][td]15
[/td][td]Total[/td][td]=SUM(C$12:C14)
[/td][td]=SUM(D$12:D14)
[/td][td]=SUM(E$12:E14)
[/td][td]=SUM(F$12:F14)
[/td][td]=SUM($C15:F15)
[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
Budget[/td][/tr][/table]
For a column addition
Using Excel 2007
[Table="width:, class:grid"][tr][td]Row\Col[/td][td]B
[/td][td]C
[/td][td]D
[/td][td]E
[/td][td]F
[/td][td]G
[/td][td]H
[/td][/tr][tr][td]4
[/td][td="bgcolor:#E2EFD9"]Salesperson[/td][td="bgcolor:#E2EFD9"]Apr 16
[/td][td="bgcolor:#E2EFD9"]=EDATE(C4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(D4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(E4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(F4,1)
[/td][td="bgcolor:#E2EFD9"]Total
[/td][/tr]
[tr][td]5
[/td][td]Staff 1[/td][td]500.00
[/td][td]200.00
[/td][td]100.00
[/td][td]700.00
[/td][td][/td][td]=SUM($C5:G5)
[/td][/tr]
[tr][td]6
[/td][td]Staff 2[/td][td]450.00
[/td][td]500.00
[/td][td]600.00
[/td][td]200.00
[/td][td][/td][td]=SUM($C6:G6)
[/td][/tr]
[tr][td]7
[/td][td]Staff 3[/td][td]300.00
[/td][td]150.00
[/td][td]300.00
[/td][td]400.00
[/td][td][/td][td]=SUM($C7:G7)
[/td][/tr]
[tr][td]8
[/td][td]Total[/td][td]=SUM(C$5:C7)
[/td][td]=SUM(D$5:D7)
[/td][td]=SUM(E$5:E7)
[/td][td]=SUM(F$5:F7)
[/td][td]=SUM(G$5:G7)
[/td][td]=SUM($C8:G8)
[/td][/tr]
[tr][td]9
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]10
[/td][td="bgcolor:#FFF3CB"]Expenses[/td][td="bgcolor:#FFF3CB"]Apr 16
[/td][td="bgcolor:#FFF3CB"]=EDATE(C10,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(D10,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(E10,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(F10,1)
[/td][td="bgcolor:#FFF3CB"]Total
[/td][/tr]
[tr][td]11
[/td][td]category 1[/td][td]100.00
[/td][td]100.00
[/td][td]100.00
[/td][td]100.00
[/td][td][/td][td]=SUM($C11:G11)
[/td][/tr]
[tr][td]12
[/td][td]category 2[/td][td]50.00
[/td][td]50.00
[/td][td]50.00
[/td][td]50.00
[/td][td][/td][td]=SUM($C12:G12)
[/td][/tr]
[tr][td]13
[/td][td]category 3[/td][td]20.00
[/td][td]20.00
[/td][td]20.00
[/td][td]20.00
[/td][td][/td][td]=SUM($C13:G13)
[/td][/tr]
[tr][td]14
[/td][td]Total[/td][td]=SUM(C$11:C13)
[/td][td]=SUM(D$11:D13)
[/td][td]=SUM(E$11:E13)
[/td][td]=SUM(F$11:F13)
[/td][td]=SUM(G$11:G13)
[/td][td]=SUM($C14:G14)
[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
Budget[/td][/tr][/table]
And for both
Using Excel 2007
[Table="width:, class:grid"][tr][td]Row\Col[/td][td]B
[/td][td]C
[/td][td]D
[/td][td]E
[/td][td]F
[/td][td]G
[/td][td]H
[/td][/tr][tr][td]4
[/td][td="bgcolor:#E2EFD9"]Salesperson[/td][td="bgcolor:#E2EFD9"]Apr 16
[/td][td="bgcolor:#E2EFD9"]=EDATE(C4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(D4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(E4,1)
[/td][td="bgcolor:#E2EFD9"]=EDATE(F4,1)
[/td][td="bgcolor:#E2EFD9"]Total
[/td][/tr]
[tr][td]5
[/td][td]Staff 1[/td][td]500.00
[/td][td]200.00
[/td][td]100.00
[/td][td]700.00
[/td][td][/td][td]=SUM($C5:G5)
[/td][/tr]
[tr][td]6
[/td][td]Staff 2[/td][td]450.00
[/td][td]500.00
[/td][td]600.00
[/td][td]200.00
[/td][td][/td][td]=SUM($C6:G6)
[/td][/tr]
[tr][td]7
[/td][td]Staff 3[/td][td]300.00
[/td][td]150.00
[/td][td]300.00
[/td][td]400.00
[/td][td][/td][td]=SUM($C7:G7)
[/td][/tr]
[tr][td]8
[/td][td]Total[/td][td]=SUM(C$5:C7)
[/td][td]=SUM(D$5:D7)
[/td][td]=SUM(E$5:E7)
[/td][td]=SUM(F$5:F7)
[/td][td]=SUM(G$5:G7)
[/td][td]=SUM($C8:G8)
[/td][/tr]
[tr][td]9
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[tr][td]10
[/td][td="bgcolor:#FFF3CB"]Expenses[/td][td="bgcolor:#FFF3CB"]Apr 16
[/td][td="bgcolor:#FFF3CB"]=EDATE(C10,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(D10,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(E10,1)
[/td][td="bgcolor:#FFF3CB"]=EDATE(F10,1)
[/td][td="bgcolor:#FFF3CB"]Total
[/td][/tr]
[tr][td]11
[/td][td]category 1[/td][td]100.00
[/td][td]100.00
[/td][td]100.00
[/td][td]100.00
[/td][td][/td][td]=SUM($C11:G11)
[/td][/tr]
[tr][td]12
[/td][td]category 2[/td][td]50.00
[/td][td]50.00
[/td][td]50.00
[/td][td]50.00
[/td][td][/td][td]=SUM($C12:G12)
[/td][/tr]
[tr][td]13
[/td][td]category 3[/td][td]20.00
[/td][td]20.00
[/td][td]20.00
[/td][td]20.00
[/td][td][/td][td]=SUM($C13:G13)
[/td][/tr]
[tr][td]14
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]=SUM($C14:G14)
[/td][/tr]
[tr][td]15
[/td][td]Total[/td][td]=SUM(C$11:C14)
[/td][td]=SUM(D$11:D14)
[/td][td]=SUM(E$11:E14)
[/td][td]=SUM(F$11:F14)
[/td][td]=SUM(G$11:G14)
[/td][td]=SUM($C15:G15)
[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
Budget[/td][/tr][/table]
_........
Let me know if these changes to your initial Spreadsheet would be acceptable please ( and let me know that you agree the first set of screen shots in Post # 2 were in error as I have suggested ). Then i will drop the codes off as soon as i can..
Alan