Copy formulas from row above after adding rows - both actions using VBA

srinimtech

New Member
Joined
Mar 29, 2017
Messages
3
Hei guys,

I need help in copying formulas from one row to rows below it using VBA.

The text in blue color shall be considered as row and column headers of a worksheet.

Input list in 'Sheet1'

Row noColumn AColumn BColumn C
1Item nameUnit rateQuantity
2Item 11015
3Item 2516
4Item 3813
5Item 4916

<tbody>
</tbody>



Calculations in 'Sheet2'


Row noColumn AColumn BColumn CColumn DColumn EColumn F
1Item nameUnit rateQuantityBasic priceTaxesTotal price
2Item 1101515015165
3Item 251680888
4Item 381310410.4114.4
5Item 491614414.4158.4
6Total47847.8525.8

<tbody>
</tbody>

The columns A, B, C in 'Sheet2' are copied from 'Sheet1'.
The values in columns D, E & F are calculated within 'Sheet2'
So, if the formulas are shown in the above table, they would be



Row noColumn AColumn BColumn CColumn DColumn EColumn F
1Item nameUnit rateQuantityBasic priceTaxesTotal price
2=Sheet1!A2=Sheet1!B2=Sheet1!C2=B2*C2=D2*10%=D2+E2
3=Sheet1!A3=Sheet1!B3=Sheet1!C3=B3*C3=D3*10%=D3+E3
4=Sheet1!A4=Sheet1!B4=Sheet1!C4=B4*C4=D4*10%=D4+E4
5=Sheet1!A5=Sheet1!B5=Sheet1!C5=B5*C5=D5*10%=D5+E5
6Total=Sum(D2:D5)=Sum(E2:E5)=Sum(F2:F5)

<tbody>
</tbody>

THE PROBLEM:

The workbook would be like this, initially.


Input list in 'Sheet1'

Row noColumn AColumn BColumn C
1Item nameUnit rateQuantity

<tbody>
</tbody>


'Sheet2'


Row noColumn AColumn BColumn CColumn DColumn EColumn F
1Item nameUnit rateQuantityBasic priceTaxesTotal price
2=Sheet1!A2=Sheet1!B2=Sheet1!C2=B2*C2=D2*10%=D2+E2
3Total=SUM(INDIRECT("D2:D"&ROW()-1))=SUM(INDIRECT("E2:E"&ROW()-1))=SUM(INDIRECT("F2:F"&ROW()-1))

<tbody>
</tbody>


Once I run the macro,
1. it should add rows between row 2 & 3 in 'Sheet2'. The number of rows would be equal to number of items in 'Sheet1' minus 1
2. Copy formulas from row 2 to end of the list.
3. The formula for 'Total' updates itself to calculate total up to the row above.

The code I used to add rows is as below:

Code:
Sub AddRows()

    Dim num_rows As Long


    Set s1 = Worksheets("Sheet3")

    Sheets("Sheet2").Select

    num_rows = s1.Cells(12, 5)

    ActiveSheet.Rows(10 & ":" & num_rows + 8).Insert Shift:=xlDown
    
End Sub

In the code above, the number of rows to be added is calculated in 'Sheet3'.

I need help in copying formulas from row 2 to other rows.

PS: The code given above to add rows was taken from some post in this form and modified to me requirement.

Regards,

Srini

 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Make your table on Sheet2 a listobject type Table
Creating an Excel Table

This will add rows to that table above the last row. The formulas will autofill.

Code:
[color=darkblue]Sub[/color] AddRows()
    [color=darkblue]Dim[/color] LastRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    [color=darkblue]With[/color] Sheets("Sheet2").ListObjects(1)
        [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] LastRow - .Range.Rows.Count + 1
            .ListRows.Add .Range.Rows.Count - 1
        [color=darkblue]Next[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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