VBA code that copies rows of data from 1 workbook to another.

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am trying to copy the first 250 rows of data from 1 workbook to another so the daily file I use doesn't get too big.

What I want to happen is the following:

1. Select and copy or cut the first 250 rows. Filename PO Cover
2. Paste onto file name Book1 but looking for the first blank row.
3. Save Book1 and close the file.
4. Delete the selected 250 rows from file name PO Cover.
5. Save PO Cover

This is the code I have so far:
Code:
Option Explicit

Sub Macro1()


Dim lrcd As Long
Dim wb As Workbook
Dim ws As Worksheet


 With Application
.ScreenUpdating = False




    Application.Run "'PO COVER 2019.xlsm'!UnlockandDisable"
    Rows("3:253").Select
    Application.CutCopyMode = False
    Selection.Cut
'    Selection.Delete Shift:=xlUp
    ActiveWorkbook.Save


    
Set wb = Workbooks.Open(Filename:="C:\Users\Dell\Desktop\Book1.xlsx")
Set ws = wb.Worksheets("copy")


   lrcd = Sheets("copy").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("copy").Cells(lrcd + 1, "A").Select


[COLOR=#ff0000]Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _[/COLOR]
[COLOR=#ff0000]    :=False, Transpose:=False[/COLOR]
    
Sheets("copy").Cells(lrcd + 1, "A").Select


wb.Worksheets("copy").Save
wb.Worksheets("copy").Close


With Application
.ScreenUpdating = True


End With
End With


End Sub

I know I haven't got all the code there at the moment but that's because I keep getting this error Pastespecial method of range class failed and I can't get past it.

Any help would be appreciated.

Thanks

Dan
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Place the following macro in a standard module in the PO Cover workbook and run it from there. Change the source sheet name (highlighted in red in the code) to suit your needs.

Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim srcWB As Workbook, desWB As Workbook, srcWs As Worksheet
    Set srcWB = ThisWorkbook
    Set srcWs = srcWB.Sheets("[COLOR="#FF0000"]Sheet1[/COLOR]")
    Set desWB = Workbooks.Open(Filename:="C:\Users\Dell\Desktop\Book1.xlsx")
    srcWs.Rows("3:252").EntireRow.Copy
    Sheets("copy").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
    desWB.Close True
    srcWs.Rows("3:252").EntireRow.Delete
    srcWB.Save
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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