chipsworld
Board Regular
- Joined
- May 23, 2019
- Messages
- 164
- Office Version
- 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:
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