0

I'm trying to paste a row that I've copied earlier in another worksheet. This is my current code:

Sub Paste() Dim cellSearch As Range, rng As Range, prodID As Range, rngHdrFound As Range, ws As Worksheet, cell As Range Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) ws.Select Set rng = ThisWorkbook.Sheets(1).UsedRange Set rngHdrFound = ThisWorkbook.Sheets(2).UsedRange For Each cellSearch In rng For Each prodID In rngHdrFound 'If the cell value is equal to the PROD ID value then paste the entire row to the new workbook If (prodID.Value = cellSeach.Value) Then For Each cell In ws.Columns(1).Cells.Address If IsEmpty(cell) = True Then ThisWorkbook.Sheets(1).prodID.Rows.Copy _ ThisWorkbook.ws.Range("cell").Rows End If Next cell End If Next prodID Next cellSearch End Sub 

In this code I create a new worksheet, set ranges for my first two sheets with data in them, and then cycle through a ForEach loop in order to compare the data of the first two sheets together. When it finds a match, then it pastes the data in the new worksheet that we created at the beginning.

I'm not sure that I'm writing my copy + paste statements right, as I'm getting a "runtime error 1004: method range of object _worksheet failed" on my copy function. If anyone could help me get this code working, that would be fantastic. Thank you all for your help in advance!

1 Answer 1

1

You want EntireRow not Rows and Cell is a variable range it should not be wrapped in Range():

 prodID.EntireRow.Copy cell 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. Why is it that I don't have to specify the worksheet that cell is in?
Because the variable already has the workbook and worksheet as part of the variable object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.