Cut row to new sheet if text is found within multiple columns

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,655
Office Version
2010
Platform
Windows
Hi Rick,

This copies across the results to columns A to I in sheet 2 rather than to AE to AM.
I need the entire row copied across if keyword is found as there is data in the other columns that needs to move across too.
Okay, give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub Copy_Rows_v2()
  Dim LastRow1 As Long, LastRow2 As Long, BlankCol As Long, strToFind As String, WS1 As Worksheet, WS2 As Worksheet
  Set WS1 = Sheets("[B][COLOR="#0000FF"]Sheet1[/COLOR][/B]")
  Set WS2 = Sheets("[B][COLOR="#FF0000"]Sheet2[/COLOR][/B]")
  BlankCol = WS1.Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column + 1
  LastRow1 = WS1.Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  On Error Resume Next
  LastRow2 = WS2.Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  If Err.Number Then LastRow2 = 1
  On Error GoTo 0
  strToFind = InputBox("Enter Keyword to be found")
  With WS1.Range(Cells(2, BlankCol), Cells(LastRow1, BlankCol))
    .Formula = "=MATCH(""" & strToFind & "*"",'" & WS1.Name & "'!AE2:AM2,0)"
    On Error Resume Next
    Intersect(WS1.Columns("A"), .SpecialCells(xlFormulas, xlNumbers).EntireRow).EntireRow.Copy WS2.Cells(LastRow2 + 1, "A")
    On Error GoTo 0
  End With
  WS1.Columns(BlankCol).Clear
  WS2.Columns(BlankCol).Clear
End Sub[/td]
[/tr]
[/table]
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Mike___

New Member
Joined
Jul 18, 2019
Messages
12
Hi Peter,

It was my turn for a mistake - I had been using the old code. It works a treat! Thanks for your time on this.

Rick - I haven't had a chance to test your new code yet - I shall give it a go!
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,655
Office Version
2010
Platform
Windows
Rick - I haven't had a chance to test your new code yet - I shall give it a go!
I would be interested to know if the code I posted worked or not. Also, if it works, your sense how it compared speedwise to Peter's code.
 
Last edited:

Mike___

New Member
Joined
Jul 18, 2019
Messages
12
Hi Rick,

Both codes work perfectly and both ran 10k of rows instantaneously.

Thanks once again to you both!
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,655
Office Version
2010
Platform
Windows
Hi Rick,

Both codes work perfectly and both ran 10k of rows instantaneously.

Thanks once again to you both!
Thanks for getting back to me on that... much appreciated. And, of course, you are quite welcome.
 

Watch MrExcel Video

Forum statistics

Threads
1,089,867
Messages
5,410,880
Members
403,332
Latest member
ffoott

This Week's Hot Topics

Top