VBA : Find a string in Sheet1/ColA, move that whole row to Sheet2

ExcelJohn

Board Regular
Joined
Mar 29, 2011
Messages
52
Dear All,

I have the following code (see below) that prompts the user for a Text string, then it searches for this string in Sheet1/ColA, and then moves that cell to Sheet2.

The problem is that I would like to move the whole row where the matched cell is contained, not only the cell itself.

How would you ammend the code to do that ?

Any help would be appreciated.

Thanks.


Code:
    Dim myString As String
    Dim foundCell As Variant
    
    myString = Trim(UserForm2.TextBox1.Value)
    If myString = vbNullString Then
        Exit Sub
    End If
    
    On Error GoTo ErrorOut
        With ThisWorkbook.Sheets("Sheet1").Range("A:A")
            .Find(what:=myString, After:=.Cells(1, 1), lookat:=xlWhole).Delete shift:=xlUp
        End With
    
        With ThisWorkbook.Sheets("Sheet2")
            .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = myString
        End With
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Code:
With ThisWorkbook.Sheets("Sheet1").Range("A:A")
            .Find(what:=myString, After:=.Cells(1, 1), lookat:=xlWhole).EntireRow.Delete
        End With
 
Upvote 0
Hi Sektor, and thanks.

This indeed removes the whole line in Sheet1, but only copies the cell value to Sheet2. I would like to "move" the whole line, not to "remove".

Do you know how to do that ?

Best,
Marc
 
Upvote 0
Move IS deleting! Say, when you MOVE some file from one location to another, the OS first makes copy of that file and then deletes it. The same process is in Excel.
If you have some other meaning of "move", please be more specific. :)
 
Upvote 0
Hey Sektor!

You are right, but :

the OS first makes copy of that file and then deletes it. The same process is in Excel.
So we agree that MOVE is first copy, and then remove.

The code does remove the whole Row from Sheet1 (ColA, ColB, ColC, etc...), but only copies to Sheet2 the ColA value, thus, missing ColB, ColC, etc...

This is what i mean.
 
Upvote 0
Try this:
Code:
    Dim myString As String
    Dim foundCell As Range
    
    myString = Trim(UserForm2.TextBox1.Value)
    If myString = vbNullString Then
        Exit Sub
    End If
    
    On Error GoTo ErrorOut
        With Sheets("Sheet1").Range("A:A")
            Set foundCell = .Find(What:=myString, After:=.Cells(1, 1), LookAt:=xlWhole)
        End With
        
    
        With Sheets("Sheet2")
            .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = Sheets("Sheet1").Range( _
                Sheets("Sheet1").Cells(foundCell.Row, "A"), Sheets("Sheet1").Cells(foundCell.Row, Columns.Count))
        End With
 
Upvote 0
Hi Sektor,

Thanks.

It detects if it finds the value or not, but unfortunately the code is not working. It doesn't copy not remove the rows.
 
Upvote 0
Then try this:

Code:
    Dim myString As String
    Dim foundCell As Range
    
    myString = Trim(UserForm2.TextBox1.Value)
    If myString = vbNullString Then
        Exit Sub
    End If
    
    On Error GoTo ErrorOut
    
    ' Find and copy entire row.
    With Sheets("Sheet1").Range("A:A")
        Set foundCell = .Find(What:=myString, After:=.Cells(1, 1), LookAt:=xlWhole)
        foundCell.EntireRow.Copy
    End With
    
    ' Paste copied cell.
    Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
    
    ' Delete entire row of found range.
    foundCell.EntireRow.Delete
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,544
Members
452,925
Latest member
duyvmex

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