I want to populate the total amount summary after the last row generated by the loop, as per below:
where all the rows are based on the amount of the cell located at the different sheet ("Frontsheet").
The initial code for the loop with copy looks as follows:
Dim i As Long
Dim rg As Range, rg2 As Range
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Frontsheet")
Set ws2 = ThisWorkbook.Sheets("Fibre drop release sheet")
Set rg = ws1.Range("D32")
Set rg2 = ws2.Range("B3:N3")
For i = 1 To rg
rg2.Copy Destination:=ws2.Range("B4").Resize(RowSize:=rg.Value)
Next i
The code, which I built including the option which I want looks as follows:
Sub Address()
Dim i As Long, LastRow As Long
Dim rg As Range, rg2 As Range, rg3 As Range, rg4 As Range
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Frontsheet")
Set ws2 = ThisWorkbook.Sheets("Fibre drop release sheet")
Set rg = ws1.Range("D32")
Set rg2 = ws2.Range("B3:N3")
Set rg3 = Cells("I" & .Rows.Count).End(xlUp).Row
Set rg4 = Cells("J" & .Rows.Count).End(xlUp).Row
Set LastRow = Cells("J" & .Rows.Count).End(xlUp).Row - 1
For i = 1 To rg
rg2.Copy Destination:=ws2.Range("B4").Resize(RowSize:=rg.Value)
Next i
rg3.Value = "Total"
rg4.Value = "=SUM(J3: & Lastrow)"
End Sub
It was based on the hints here:
counting a range (rows) begingin in B3
Better way to find last used row
Excel VBA: Loop InputBox and add values to cell range
in order to find the last row.
Unfortunately I am getting an error: Invalid or unqualified reference where the debugger points the following line:
Set rg3 = Cells("I" & .Rows.Count).End(xlUp).Row, especially .Rows
As per the explanation above I should have the With
statement
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/invalid-or-unqualified-reference
But it looks like it complicates my situation because I set it as the range.
How can I add some stuff beyond the last row, which was populated by the loop?