I would like to identify the last row of formulas on the sheet titled "Term Loans". I'd then like to copy all those formulas and paste them one row below. For example, if the last row with formulas is row 15, I'd like to copy row 15 and paste formulas into row 16. In my code below, I get an error on the line Worksheets("Term Loans").Range(Worksheets("Term Loans").Cells(lRow, 1)).EntireRow.Copy
.Any advice on how to fix this would be much appreciated. The error is "Application-defined or object defined error"
Dim lRow As Long
Sheets("Term Loans").Select
'Find the last non-blank cell in column A(1)
lRow = Cells(Rows.Count, 1).End(xlUp).Row
' Now Copy the range:
Worksheets("Term Loans").Range(Worksheets("Term Loans").Cells(lRow, 1)).EntireRow.Copy
' And paste to first empty row
Worksheets("Term Loans").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
You error is caused by
Worksheets("Term Loans").Range(Worksheets("Term Loans").Cells(lRow, 1))
When you pass a single parameter to Range(...)
it is interpreted as a address string. When that parameter is a range itself, ( Worksheets("Term Loans").Cells(lRow, 1)
in this case) the value of that range is passed to Range
(so ... .Cells(lRow, 1).Value
is this case)
So, unless ... .Cells(lRow, 1).Value
returns a valid address in A1 style (or the name of a Named Range), you will get an error.
Here's how to correctly reference the range
Sub Demo
Dim lRow As Long
Dim ws As Worksheet
Set ws = Sheets("Term Loans")
'Find the last non-blank cell in column A(1)
lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Now Copy the range:
ws.Cells(lRow, 1).EntireRow.Copy
' And paste to first empty row
ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
End Sub
And an improved version
Sub Demo2
Dim wb as Workbook
Dim ws As Worksheet
Dim rng as Range
Set wb = Thisworkbook 'or Activeworkbook, or some other book you specify
Set ws = wb.Worksheets("Term Loans")
With ws
'Find the last non-blank cell in column A(1)
Set rng = .Cells(.Rows.Count, 1).End(xlUp).EntireRow
End With
' Now Copy the range:
rng.Copy
' And paste to first empty row
rng.Offset(1, 0).PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
' To set a specific cell in the new row to a value
Dim MyVal as Long
MyVal = 42
rng.Offset(1, 0).Cells(1, 3) = MyVal
End Sub
You are trying to reference lrow
as a range after declaring it as a Long
and that's where the error is coming from. Try this instead, I declared some other variables in order to make the code more simple. Hope this helps:
Sub copypaste1rowdown()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim lRow As Long
lRow = ws.Range("A3:Q3").CurrentRegion.Rows.Count
Dim lCol As Long
lCol = ws.Range("A3:Q3").CurrentRegion.Columns.Count
Dim i As Long
For i = 1 To lCol
Cells(lRow + 2, i).Copy
Cells(lRow + 2, i).Offset(1, 0).PasteSpecial xlPasteAll
Next i
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.