Edit VBA Code to Paste Data in 1st empty row

PRS25

New Member
Joined
May 30, 2017
Messages
5
Hello Experts -

The following code copies and pastes data between workbook sheets. Can someone please modify so it pastes in the first empty row? It currently pastes in the 2nd row.

Code:
Set zDest1 = sht01.Cells(2, 1) 
zSource.Sheets(4).[a1].CurrentRegion.Offset(2).Copy zDest1
Set zDest1 = sht01.Cells(Rows.Count, 1).End(xlUp).Offset(1)


'Preciate everyone on this forum helping others :beerchug:
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
That is because it is looking for the last row with data, and then moving down 1 row.
However, if there is no data at all in column A, it goes to the top (row 1) and then moves down 1 row.
You can simply add a line after the copy/paste step that will delete the top blank line, if one exists, i.e.
Code:
[COLOR=#ff0000]If sht01.Cells(1, 1) = "" Then Rows(1).Delete[/COLOR]
 
Last edited:
Upvote 0
Hi Joe,

Thanks for your response. I added your line below the copy/paste step, however the import still pasted over the values in the first 3 rows.
 
Upvote 0
Try changing your code like this:

Code:
Set zDest1 = sht01.Cells(Rows.Count, 1).End(xlUp).Offset(1)
zSource.Sheets(4).[a1].CurrentRegion.Offset(2).Copy zDest1
If sht01.Cells(1, 1) = "" Then Rows(1).Delete
 
Upvote 0
Thanks for your help, Joe. Happy to report I figured it out by adjusting the first line to:

Code:
Set zDest1 = sht01.Cells(1, 1).End(xlDown).Offset(1)
 
Upvote 0
Thanks for your help, Joe. Happy to report I figured it out by adjusting the first line to:

Set zDest1 = sht01.Cells(1, 1).End(xlDown).Offset(1)
Note that there are limitations to that!
If there are no entries below row 1 in column A, this will return errors!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top