Adjust Cell References for Formulas

Sherifa

New Member
Joined
Oct 23, 2017
Messages
45
I've copied a row from a spreadsheet, I then get the code to find the last row in each sheet and paste the summary row, which includes sum and count formulas, but the row number changes for each sheet.

How do I get the pasted formula to adjust to the number of rows in the sheet.

Code:
Sub SummaryRow()Dim Wkb As Excel.Workbook
Dim ws As Worksheets
Dim ws_count As Integer
Dim i As Integer
Dim LastRow As Long




Dim TargetRow As Range






    Sheet4.Range("a183:M183").Copy
    
        Set Wkb = Workbooks("VBA1.xlsm")
        ws_count = Wkb.Worksheets.Count
                
                     
        'Begin loop
        For i = 4 To ws_count
              
       
       Set TargetRow = Wkb.Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        
         'Paste Formulas
        TargetRow.PasteSpecial xlPasteFormulas
        TargetRow.PasteSpecial xlPasteFormats
        
        Next i
        


End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Runtime Error 91

Code:
Sub SummaryRow()Dim Wkb As Excel.Workbook
Dim ws As Worksheets
Dim ws_count As Integer
Dim i As Integer
Dim LastRow As Long




    Sheet4.Range("a183:M183").Copy
    
        Set Wkb = ThisWorkbook
        ws_count = Wkb.Worksheets.Count
                
                     
        'Begin loop
        For i = 4 To ws_count
              
       
       Set TargetRow = Wkb.Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        
        LastRow = Wkb.Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Row


         'Paste Formulas
        TargetRow.PasteSpecial xlPasteFormulas
        [B]ws(i).Cells(LastRow + 1, 7).FormulaR1C1 = "=SUM(R4C7:R" & LastRow & "C7)"
        ws(i).Cells(LastRow + 1, 8).FormulaR1C1 = "=COUNT(R4C8:R" & LastRow & "C8)"
        ws(i).Cells(LastRow + 1, 9).FormulaR1C1 = "=SUM(R4C9:R" & LastRow & "C9)"[/B]
        TargetRow.PasteSpecial xlPasteFormats
        
        Next i
        
End Sub

Having problems making the formula dynamic
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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