What is it about Selection.Paste that breaks my code?

rizzo93

Active Member
Joined
Jan 22, 2015
Messages
301
Office Version
  1. 365
I'm so close!

In short, when I find a value in Workbook B that isn't found in Workbook A, then I want to copy some cells in that row into the next available row in Workbook A.

The problem is the paste part. I can get the rows copied and the destination cell selected, but whenever I try to paste it using Selection.Paste, I get this error: Object doesn't support this property or method.

What is the solution, please?

Code:
Sub CopyFromCSV()
Application.ScreenUpdating = False

    Dim i, x As Integer
    Dim sourcebook As Workbook
    Dim alphabook As Workbook
    Dim expdata As Worksheet
    Dim legacy As Worksheet
    Dim sFileName As String
    Dim legacyTrans As String
    Dim searchRng As Range

    sFileName = Application.GetOpenFilename(, , "Select a CSV file for import")
    Set sourcebook = Workbooks.Open(sFileName)
    Set expdata = sourcebook.Worksheets("Export")
    Set alphabook = Application.ThisWorkbook
    Set legacy = ThisWorkbook.Worksheets("Legacy - Checking")
    
    'WHERE IS THE NEXT AVAILABLE ROW ON LEGACY PROPER
    For i = 1 To 5000
        If legacy.Cells(i, 17).Value = "" Then
            x = i
            Exit For
        End If
    Next i
    
    For i = 5 To 500
        Set searchRng = legacy.Range("Q:Q").Find(expdata.Cells(i, 1))
        If searchRng Is Nothing Then
            expdata.Range("A" & expdata.Cells(i, 1).Row & ":I" & expdata.Cells(i, 1).Row).Copy
            legacy.Activate
            legacy.Cells(x, 17).Select
            [COLOR=#ff0000]Selection.Paste[/COLOR]
            Exit For
        End If
    Next i
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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