Simple Copy and Paste from one workbook to another....

chipsworld

Board Regular
Joined
May 23, 2019
Messages
164
Office Version
  1. 365
Good morning everyone...
I have some code that seems to work fine except for the paste portion... I am sure it is something stupid that I am overlooking, but can't figure it out...

Am getting error: Run time error '1004': Application-defined or object defined error on this line

targetsheet.Cells(rw, "A:H").PasteSpecial xlPasteValues



Here is full code:

VBA Code:
Private Sub cmdimport_Click()
Dim customerBook As Workbook
Dim filter As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim targetsheet As Worksheet
Dim sourceSheet As Worksheet
Dim response As String
Dim lstrw As Double
Dim rw As Double
Dim range1 As Range, range2 As Range, multipleRange As Range


'active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "*.xl* (*.xls*),*.xls*"
caption = "Please Select file to import "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' copy data from customer to target workbook
Set targetsheet = targetWorkbook.Worksheets("UC Details")
Set sourceSheet = customerWorkbook.Worksheets("Sheet1")
With sourceSheet
    Set range1 = .Range("A2:G" & .Range("G" & Rows.Count).End(xlUp).Row)
    Set range2 = .Range("N2:N" & .Range("N" & Rows.Count).End(xlUp).Row)
    Set multipleRange = Union(range1, range2)
End With
multipleRange.Select
multipleRange.Copy
With targetsheet
    lstrow = .Cells(.Rows.Count, "A").End(xlUp).Row
                    rw = lstrow + 1
    targetsheet.Cells(rw, "A:H").PasteSpecial xlPasteValues
End With


' Close customer workbook
customerWorkbook.Close (False)
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try
VBA Code:
targetsheet.Cells(rw, "A").PasteSpecial xlPasteValues
 
Upvote 0
Solution
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,969
Messages
6,175,690
Members
452,667
Latest member
vanessavalentino83

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