0
votes

I have an excel sheet with data present in columns from column A to column V. I have a code which calculates the last row of the sheet. Now, I want to copy the last row i.e. for eg if the last row is 7 then I want to copy range A7 to V7 and paste it another worksheet.

'Here i is the LastRow
Workbooks("Co_insurer Sample File.xlsx").Worksheets("QUIDAM-INSURERS").Range("A:V" & i).Copy
Workbooks("Smart_Excel_BK.xlsm").Worksheets("Input").Range("I" & LastRowBK).Select
Workbooks("Smart_Excel_BK.xlsm").Worksheets("Input").Paste
2
And your question is?AJD
@AJD My code above is not working. It gives me application-defined or object-defined error. Can you correct it?Rithwik Sarma

2 Answers

0
votes

Try:

Dim lastRow As Long
lastRow = Workbooks("Co_insurer Sample File.xlsx").Worksheets("QUIDAM-INSURERS").Cells(Rows.Count, 1).End(xlUp).Row

Workbooks("Co_insurer Sample File.xlsx").Worksheets("QUIDAM-INSURERS").Range("A:V" & lastRow).Copy Workbooks("Smart_Excel_BK.xlsm").Worksheets("Input").Range("I" & lastRow)
0
votes

Something like this:

Dim wsSource As worksheet, wsDest As Worksheet, rngCopy As Range

Set wsSource = Workbooks("Co_insurer Sample File.xlsx").Worksheets("QUIDAM-INSURERS")
Set wsDest = Workbooks("Smart_Excel_BK.xlsm").Worksheets("Input")

Set rngCopy = wsSource.Range("A" & i & ":V" & i)

rngCopy.Copy wsDest.Cells(LastRowKS + 1, "I")

To copy only values:

wsDest.Cells(LastRowKS + 1, "I").Resize( _
     rngCopy.Rows.Count, rngCopy.columns.Count).Value = rngCopy.Value