PDA

View Full Version : [SOLVED:] Copy to the latest blank row



parscon
12-28-2013, 08:30 AM
I have this VBA code for copy my data from sheet2 to Sheet 1 , but the problem is on

lastrowDest = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1
that mean if i do not have any data on sheet1 ,it will be add a blank row on sheet1 but if i have data on sheet1 it will not have any problem , so i need to add something to the below code that if the sheet1 is empty do not paste the data with a one blank row.

Please modify the below code.

Thank you very much




Dim LastRow As Long

lastrowSrc = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row

'Get first blank row (last row of data +1)
lastrowDest = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1

'Copy row
Sheets("Sheet2").Range("A1:E" & lastrowSrc).EntireRow.Copy Sheets("Sheet1").Range("A" & lastrowDest)

ashleyuk1984
12-28-2013, 08:40 AM
Loads of various ways around this... You could do a IF statement.


Dim LastRow As Long
lastrowSrc = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row

'Get first blank row (last row of data +1)
lastrowDest = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1

'Copy row
If Sheets("Sheet1").Range("A1").Value = "" Then
Sheets("Sheet2").Range("A1:E" & lastrowSrc).EntireRow.Copy Sheets("Sheet1").Range("A1")
Else
Sheets("Sheet2").Range("A1:E" & lastrowSrc).EntireRow.Copy Sheets("Sheet1").Range("A" & lastrowDest)
End If

parscon
12-30-2013, 12:20 AM
Thank you very much