VBA to insert formula based on data in another column

KML2630

New Member
Joined
May 16, 2018
Messages
1
Hi

I am struggling with a part of a project I am working on and have been unable to find a solution. Hoping someone here can assist.

I need to be able to insert formulas into columns AA-AG, based on data (that is variable) in column H

I am calculating capital gains based on tax lots and the formulas are specific to that. There could be one tax lot per security or many. The first row of formula would need to correspond with the change in value (tickers) in column H. The second line would then be copied down for the remaining rows, until the value in col H changes.

H
AA
AB
AE
AF
AG

AH
XXX
=IF($Z4<0,$K4+$Z4,0)
=IF(AA4<0, P4,0)
=IF(AA4<0,Q4,0)
=IF(AA4>0,(J4-AA4)/J4*Q4,0)
=SUM(AB4,AD4)
=SUM(AC4,AE4)
XXX
=IF(AA4<0,AA4+K5,0)
=IF(AA5<0, P5,0)
=IF(AA5<0,Q5,0)
=IF(AA5>0,(J5-AA5)/J5*Q5,0)
=SUM(AB5,AD5)
=SUM(AC5,AE5)
XXX
=IF(AA5<0,AA5+K6,0)
=IF(AA6<0, P6,0)
=IF(AA6<0,Q6,0)
=IF(AA6>0,(J6-AA6)/J6*Q6,0)
=SUM(AB6,AD6)
=SUM(AC6,AE6)
YYY
=IF($Z4<0,$K4+$Z4,0)
=IF(AA4<0, P4,0)
=IF(AA4<0,Q4,0)
=IF(AA4>0,(J4-AA4)/J4*Q4,0)
=SUM(AB4,AD4)
=SUM(AC4,AE4)
YYY
=IF(AA4<0,AA4+K5,0)
=IF(AA5<0, P5,0)
=IF(AA5<0,Q5,0)
=IF(AA5>0,(J5-AA5)/J5*Q5,0)
=SUM(AB5,AD5)
=SUM(AC5,AE5)
YYY
=IF(AA5<0,AA5+K6,0)
=IF(AA6<0, P6,0)
=IF(AA6<0,Q6,0)
=IF(AA6>0,(J6-AA6)/J6*Q6,0)
=SUM(AB6,AD6)
=SUM(AC6,AE6)
YYY
=IF(AA4<0,AA4+K5,0)
=IF(AA5<0, P5,0)
=IF(AA5<0,Q5,0)
=IF(AA5>0,(J5-AA5)/J5*Q5,0)
=SUM(AB5,AD5)
=SUM(AC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0,(J5-AA5)/J5*P5,0)
[/TD]

[TD="colspan: 2"]=IF(AA6>0,(J6-AA6)/J6*P6,0)
[/TD]

[TD="colspan: 2"]=IF(AA6>0,(J6-AA6)/J6*P6,0)
[/TD]

[TD="colspan: 2"]=IF(AA4>0,(J4-AA4)/J4*P4,0)
[/TD]

[TD="colspan: 2"]=IF(AA5>0,(J5-AA5)/J5*P5,0)
[/TD]

[TD="colspan: 2"]=IF(AA6>0,(J6-AA6)/J6*P6,0)
[/TD]

[TD="colspan: 2"]=IF(AA5>0,(J5-AA5)/J5*P5,0)
[/TD]

</tbody>



All of the data in columns A-V is being copied from another source and then pasted into this sheet. There is quite a bit of formatting, calculating etc.

I basically need a vba that will look in col H and then paste the appropriate formulas into AA-AG. The first row is different--the second could simply copy down until the value in H changes and then the first/second row would need to be inserted (with 2nd row being copied down) until the next change in value.

Hope this makes sense!

Any help would be greatly appreciated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top