Share via

Copy paste range

Anonymous
2022-03-01T20:21:34+00:00

The macro below matches the contents in Sheet1 A2 with a row in Sheet2 Column A.

For the final step I want to also make it select the row below the matching cell in Sheet2 Column A down to the last row with data then over to Column M, cut that range, and paste it up one row so instead of deleteing the matching row it just gets pasted over. Can you help?

Sub Moveup()

Dim ws1 As Worksheet

Dim ws2 As Worksheet

Dim SearchRow As Range

Dim SearchValue As Range

Dim lngRow As Long

Set ws1 = ThisWorkbook.Worksheets("Sheet1")

Set ws2 = ThisWorkbook.Worksheets("Sheet2")

Set SearchValue = ws1.Range("A2") 'Needs to be a range object or last 2 lines will error

Set SearchRow = ws2.Range("A:A").Find(What:=SearchValue.Value, _

LookIn:=xlValues, _

LookAt:=xlWhole, _

SearchOrder:=xlByRows, _

SearchDirection:=xlNext, _

MatchCase:=False, _

SearchFormat:=False)

If Not SearchRow Is Nothing Then

lngRow = SearchRow.Row

'Final Step

End If

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2022-03-01T21:03:23+00:00

Hi Amana

Try the following code

''''****************************************************************************

Sub Moveup()

Dim ws1 As Worksheet

Dim ws2 As Worksheet

Dim MatchCell As Range

Dim SearchValue As Range

Dim NextRow As Long

Dim LastRow As Long

Set ws1 = ThisWorkbook.Worksheets("Sheet1")

Set ws2 = ThisWorkbook.Worksheets("Sheet2")

Set SearchValue = ws1.Range("A2") '''Needs to be a range object or last 2 lines will error

With ws2

    ''' Find the value/cell 

    Set MatchCell = .Range("A:A").Find(What:=SearchValue.Value, \_ 

    LookIn:=xlValues, \_ 

    LookAt:=xlWhole, \_ 

    SearchOrder:=xlByRows, \_ 

    SearchDirection:=xlNext, \_ 

    MatchCase:=False, \_ 

    SearchFormat:=False) 

    If Not MatchCell Is Nothing Then  ''' If value is found 

            NextRow = MatchCell.Row + 1    ''' the row below the matching cell 

            LastRow = Cells(Rows.Count, "A").End(xlUp).Row  '' the last row with data in column A 

            .Range(Cells(NextRow, "A"), Cells(LastRow, "M")).Cut MatchCell   ''' Cut and Paste over the Range 

    End If 

End With

End Sub

'''''''''**********************************************************************

I hope this helps you and gives a solution to your problem

Do let me know if you need more help

Regards

Jeovany

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2022-03-01T20:45:01+00:00

    There is absolutely no difference between deleting part of a row and and shifting the range below it up by one row.

    Just use this in place of 'Final Step

    ws2.Range("A" & SearchRow).Resize(1,13).Delete Shift:=xlUp

    Was this answer helpful?

    0 comments No comments