Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Copy and paste a cell value on the last row of a column

I’m facing a small issue, i need to write a code that copy a cell value and paste in in the last row found of a column (column L in my case), here is what i wrote:

lastRow = ThisWorkbook.Sheets("RecordAccordo").Range("L" & ThisWorkbook.Sheets("RecordAccordo").Rows.Count).End(xlUp).Row

        Range("B36").Copy
        
        ThisWorkbook.Sheets("RecordAccordo").Range("L2:L" & lastRow).PasteSpecial Paste:=xlPasteValues

My issue is that the value of cell B36 is pasted on all the column L rows while i need it only in the last row found

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your error is basically that you are referencing the whole column when pasting the value: .Sheets("RecordAccordo").Range("L2:L" & lastRow).

Correct would be .Sheets("RecordAccordo").Range("L" & lastRow)

But I refactored your code a bit to be more precise and readable plus showing you that copy/paste is not necessary – as you can use the value properties of source and target range

Public Sub copyValue()

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("RecordAccordo")

Dim cSource As Range
Set cSource = ws.Range("B36")

Dim lastRow As Long
With ws
    lastRow = .Range("L" & .Rows.Count).End(xlUp).Row
End With

Dim cTarget As Range
Set cTarget = ws.Range("L" & lastRow)

cTarget.Value = cSource.Value

End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading