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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,504
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]
 

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,504
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,504
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.
 

Forum statistics

Threads
1,081,657
Messages
5,360,291
Members
400,580
Latest member
Drexl88

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top