Excel VBA to copy, find last empty row, paste

techgirl

Board Regular
Joined
Sep 16, 2002
Messages
178
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I need the vba code to copy multiple worksheets into another worksheet (Main) without overwriting the first copy/paste.

I have this started, it works, to copy from sheet 1 into MAIN. But I need to find the last empty cell in "Main"; then go to sheet2, sheet3, sheet4, copy without having to change the code each time.
worksheets("Sheet1").Range("A2:F50").Copy Worksheets("Main").Range("A2")

I have this for the Find last empty cell; but don't know how to place it inside of the first code.
Sub findnextblankrow()
Range("A2").End(xlDown).Offset(1, 0).Select

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This is the code i use to copy something and paste in the row after the last one that is filled. The value of 12 is not something you'd probably want. I have one cell value pasting into 12 different locations
Code:
Sub Open_Workbook()


    Dim srcWB As Workbook
    Dim destWB As Workbook
    Dim fName As String
    Dim lastRow As Long
    
'   Capture current workbook as source workbook
    Set srcWB = ActiveWorkbook


'   Open destination workbook and capture it as destination workbook
    Workbooks.Open "C:\Users\jdavis\Dropbox\Quality Control\Asphalt\Misc\Yearly HMA Charts.xlsx"
    Set destWB = ActiveWorkbook
    


'   Find last row of Sieve data in destination workbook
    lastRow = destWB.Sheets("Sieves").Cells(Rows.Count, "A").End(xlUp).Row + 1
    
'   Copy Sieve data from source workbook to destination workbook
    srcWB.Sheets("A").Range("G20").Copy
    destWB.Sheets("Sieves").Range("A" & lastRow).Resize(12, 1).PasteSpecial xlPasteValues
 
Last edited:
Upvote 0
Try this

Code:
Sub Copy_data()
    
    Worksheets("Sheet1").Range("A2:F50").Copy Worksheets("Main").Range("A" & Rows.Count).End(xlUp)(2)
    Worksheets("Sheet2").Range("A2:F50").Copy Worksheets("Main").Range("A" & Rows.Count).End(xlUp)(2)
    Worksheets("Sheet3").Range("A2:F50").Copy Worksheets("Main").Range("A" & Rows.Count).End(xlUp)(2)


End Sub

Note:
The above is possible if in column A of each sheet you always have data.
 
Upvote 0
That worked. Thank you. Can you explain the last part..."& Rows.Count).end(xlUP) (2)?
 
Upvote 0
That worked. Thank you. Can you explain the last part..."& Rows.Count).end(xlUP) (2)?

I explain it with an example:

The following selects the last cell with data
Worksheets("Main").Range("A" & Rows.Count).End(xlUp).Select

The same does the following
Worksheets("Main").Range("A" & Rows.Count).End(xlUp)(1).Select

Then to find out which is the next empty cell after the last cell with data:
Worksheets("Main").Range("A" & Rows.Count).End(xlUp)(2).Select

Then to paste in the next cell:
Code:
[COLOR=#333333]Worksheets("Sheet1").Range("A2:F50").Copy [/COLOR][COLOR=#0000ff]Worksheets("Main").Range("A" & Rows.Count).End(xlUp)[/COLOR][COLOR=#ff0000](2)[/COLOR][COLOR=#0000ff][/COLOR][COLOR=#333333][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,549
Messages
6,120,149
Members
448,948
Latest member
spamiki

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