Find first empty row and copy/paste data

Faygin

New Member
Joined
May 12, 2020
Messages
23
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Looked around and tried to modify existing code but without success.

I have a workbook with a command button.
When i press the button i want to copy the data and paste it in another workbook.

But it has to be on the first available empty row, so that i dont destroy the old data.

So far the code looks like this:

Private Sub CommandButton1_Click()

'This is the data i want to copy
Range("E3:M8").Select
Selection.Copy

'Open the workbook which i want to paste into
Workbooks.Open "O:\abc\def\ghi.xlsx"

' Select the sheet and range A1
Application.Goto Workbooks("NPD Beläggningsprofil.xlsx").Sheets("DATA").Range("A1")

--
Now i want to find the first empty row on the sheet "DATA" and paste or copy the code from the other worksheet.
If the first cell in the row is empty, then i consider the whole row as empty and can be written data to.

Any suggestions and pointers are much appreciated!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Do you mean the first empty row or the first empty row after the data?
 
Upvote 0
The sheet "DATA" contains a lot of data.
I want to find the first empty row and paste additional data into the sheet.
 
Upvote 0
That didn't answer my question. With the below would want row 3 or row 6?

1591003830489.png
 
Upvote 0
OK, now i understand, sorry about that.
In the sheet there are never any "gaps".
All data are from top to bottom.

So in my case (referring to your example) row three would never exist.
So row three would contain data and i would like new data to appear in row 6.
 
Upvote 0
A bit confused why you are opening one workbook then going to another workbook to paste the data but anyway try the below (then if you want the other workbook open do it afterwards).
There is no need to go to the sheet or activate it.
VBA Code:
Range("E3:M8").Copy Workbooks("NPD Beläggningsprofil.xlsx").Sheets("DATA").Cells(Rows.Count, "A").End(xlUp)(2)
 
Last edited:
Upvote 0
Thank you, think this is very close to a solution.

When i first tried i got no error messages and maybe its because i had the two workbooks opened?
If i have the target workbook closed i get a error message:

Subscript out of range (Error 9)

VBA Code:
Private Sub CommandButton1_Click()

 
 Range("E3:M8").Copy Workbooks("NPD Beläggningsprofil.xlsx").Sheets("DATA").Cells(Rows.Count, "A").End(xlUp)(2)
    
    
End Sub
 
Upvote 0
You would get an error as to refer to a closed workbook you would have to use its full path.
Personally I would open and close it in the code as there are lots of things that you can't do with closed workbooks.
 
Upvote 0
Cant thank you enough, problem is solved and you made my day!
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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