VBA Copy and Paste

EvonS

Board Regular
Joined
Jun 1, 2016
Messages
111
Office Version
  1. 365
Platform
  1. Windows
  2. Web
I get a this error "Runtime error 1004: application defined error or object defined error"

My code

VBA Code:
Sub Import_Data()

    Dim src As Workbook
    Dim dest As Workbook
    'Dim rng As Range
    
    Set src = Workbooks.Open([A3])
    Set dest = Workbooks.Open("T:\Datawarehouse_FOCUSToronto\Source\Source.xlsx")
    'rng = Workbooks("Discussion Notes - BC 2022.xlsm").Worksheets("Sheet1").Cells(17, 2).Value
    
    'Copy range to clipboard
    Workbooks("Discussion Notes - BC 2022.xlsm").Worksheets("Data Fields").Range(Cells(17, 2).Value).Copy
 
    'PasteSpecial to paste values, formulas, formats, etc.
    Workbooks("Source.xlsm").Worksheets("Sheet1").Range("B2").PasteSpecial Paste:=xlPasteValues

 
End Sub

Any ideas of what the problem is?
 
You do realize that you're not closing these wbs after opening them? That's not the same code I offerred? I missed the .Name part of the wbs. Trial this. Dave
Code:
Sub Import_Data()
Dim src As Workbook
Dim dest As Workbook
Set src = Workbooks.Open(CStr([Sheet1!A3]))
Set dest = Workbooks.Open("T:\Datawarehouse_FOCUSToronto\Source\Source.xlsx")
'copy from source wb
With Workbooks(src.Name).Worksheets("Data Fields")
.Range(.Cells(9583, 1), .Cells(10337, 1)).Copy
'.Range("A9583:A10337").Copy
End With
'paste to destination wb
'PasteSpecial to paste values, formulas, formats, etc.
Workbooks(dest.Name).Worksheets("Sheet1").Range("B2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'******CLOSE BOTH WBs HERE
End Sub
I really appreciate you help with this. The problem ended up being that the dest and src were reversed in the code you initially gave me. I made that change and now it works perfectly. Thanks. Now I have to figure out how to get it not paste into the first empty cell :)
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I initially stated that it was odd that U were copying from the destination file and pasting to the source file, but I left the code as U had it. You seemed to have changed this, so my previous post followed your lead. To avoid the use of the clipboard you can use the following. It places the transferred range to the next available row based on the "B" column. Dave
Code:
Sub Import_Data()
Dim src As Workbook, Rng As Range
Dim dest As Workbook, LastRow As Integer
Set src = Workbooks.Open(CStr([Sheet1!A3]))
Set dest = Workbooks.Open("T:\Datawarehouse_FOCUSToronto\Source\Source.xlsx")
With Workbooks(src.Name).Worksheets("Data Fields")
Set Rng = .Range(.Cells(9583, 1), .Cells(10337, 1))
End With
With Workbooks(dest.Name).Worksheets("Sheet1")
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("B" & LastRow + 1).Resize(Rng.Rows.Count, _
                       Rng.Columns.Count).Cells.Value = Rng.Cells.Value
End With

'******CLOSE BOTH WBs HERE
End Sub
 
Upvote 0
I initially stated that it was odd that U were copying from the destination file and pasting to the source file, but I left the code as U had it. You seemed to have changed this, so my previous post followed your lead. To avoid the use of the clipboard you can use the following. It places the transferred range to the next available row based on the "B" column. Dave
Code:
Sub Import_Data()
Dim src As Workbook, Rng As Range
Dim dest As Workbook, LastRow As Integer
Set src = Workbooks.Open(CStr([Sheet1!A3]))
Set dest = Workbooks.Open("T:\Datawarehouse_FOCUSToronto\Source\Source.xlsx")
With Workbooks(src.Name).Worksheets("Data Fields")
Set Rng = .Range(.Cells(9583, 1), .Cells(10337, 1))
End With
With Workbooks(dest.Name).Worksheets("Sheet1")
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("B" & LastRow + 1).Resize(Rng.Rows.Count, _
                       Rng.Columns.Count).Cells.Value = Rng.Cells.Value
End With

'******CLOSE BOTH WBs HERE
End Sub
Thank you so much. Have an awesome day.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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