0

I am working on a VBA code to copy data from the second cell "A2" down to the last cell with data in it (this range will change). I then want to paste that data starting at the last row of column A on another sheet.

My VBA code as of now seems to do nothing. Can someone help me figure out why?

Option Explicit

Public Sub AddNotes()

Dim lastRow1 As String
Dim lastRow2 As String

lastRow1 = Sheets("PropertiesToMatch").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("PropertiesToMatch").Range("A2" & lastRow1).Copy

lastRow2 = Sheets("PropertyNotesToUpload").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("PropertyNotesToUpload").Range("A" & lastRow2).PasteSpecial Paste:=xlPasteValues

End Sub
CESBoston
  • 83
  • 1
  • 10

2 Answers2

3

Change

.Range("A2" & lastRow1)

to

.Range("A2:A" & lastRow1)

The first is only one cell, the second is the range you want.

Also, change

Dim lastRow1 As String
Dim lastRow2 As String

to

Dim lastRow1 As Long
Dim lastRow2 As Long

because Range.Row is a Long, not a String.

BigBen
  • 46,229
  • 7
  • 24
  • 40
-2
Option Explicit

Sub CopyPaste()
Dim i, j As Integer
Dim LastRow As Integer

With Sheets("PropertiesToMatch")
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
j = 2

For i = LastRow To 2 Step -1
Sheets("PropertiesToMatch").Cells(i, 1).Copy
Sheets("PropertyNotesToUpload").Cells(j, 1).PasteSpecial Paste:=xlPasteValues

j = j + 1
Next i

End Sub
q0mlm
  • 313
  • 1
  • 3
  • 10
  • There are lots of things wrong with this answer. Use `Long` instead of `Integer`, no loop is needed, and you're looping from the bottom to the top, among others. – BigBen Nov 14 '19 at 19:27
  • Thanks for your corrections. Genuinely curious, why use Long instead of Integer? As for the reversed loop I interpreted the "I then want to paste that data starting at the last row of column A on another sheet" as I want the last cell of column of A in the first sheet to be the first cell of column A in the second sheet. – q0mlm Nov 14 '19 at 20:02
  • I can see how you might take that away from the question, but OP's code is pretty clear what the goal is, and there's an accepted answer already. – BigBen Nov 14 '19 at 21:01
  • See [this question](https://stackoverflow.com/questions/26409117/why-use-integer-instead-of-long) for why not to use Integer. Main reason is that there's a possible Overflow error - Excel has more rows than Integer can handle. – BigBen Nov 14 '19 at 21:02
  • Apologies if my first comment came off as rude in any way, I could have been more gentle about it. – BigBen Nov 14 '19 at 23:05