How to search for 2 or more words in a column and if equals - move to different sheet

danacton

New Member
Joined
Dec 6, 2006
Messages
32
Hi, it's been a long time and my credentials still work. (and I still remembered them).

I have a spreadsheet that works (a hack job because I don't understand code very well)
I found this code online, some of it I get, other parts I don't.
In my test code I search for a value in column 'C' and if that value = john, the entire row is moved to sheet2 (the job is done)

My goal is to be able to search for more than one value. I would to search for john and dan in column see and if either of those conditions are met I would like the entirerow to move to sheet2. I've spent a lot of time repeating functions and looking for solutions - thought it was time to ask the experts.

Here's what I have - and please excuse me if I have extraneous in this code, but it does work with one name: john. I would also like it to NOT be case sensitive if possible...

Code:
Sub Cheezy()
'Updated by Kutools for Excel 2017/8/28
    Dim xRg As Range
    Dim xCell As Range
    Dim I As Long
    Dim J As Long
    Dim K As Long
    I = Worksheets("Sheet1").UsedRange.Rows.Count
    J = Worksheets("Sheet2").UsedRange.Rows.Count
    If J = 1 Then
       If Application.WorksheetFunction.CountA(Worksheets("Sheet2").UsedRange) = 0 Then J = 0
    End If
    Set xRg = Worksheets("Sheet1").Range("C1:C" & I)
    On Error Resume Next
    Application.ScreenUpdating = False
    For K = 1 To xRg.Count
        If CStr(xRg(K).Value) = "john" Then
            xRg(K).EntireRow.Copy Destination:=Worksheets("Sheet2").Range("A" & J + 1)
            xRg(K).EntireRow.Delete
            If CStr(xRg(K).Value) = "john" Then
                K = K - 1
            End If
            J = J + 1
        End If
    Next
    Application.ScreenUpdating = True
End Sub

This sub is called in sheet1 where you right click the bottom tab and add code (General)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C2:C100")) Is Nothing Then
    If Target.Value = "Done" Then
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        'Target.ClearContents
        Call Cheezy
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    Else
    End If
    End If
End Sub

Thanks ahead for any help in getting 2 or more names I can search for.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Many thanks! Works perfectly - and i mean perfectly! I thought I thanked you last night, maybe I'm not following protocol, anyway, thanks again!
 
Upvote 0
Yes you thanked me last night. Glad this worked for you. Take care
Many thanks! Works perfectly - and i mean perfectly! I thought I thanked you last night, maybe I'm not following protocol, anyway, thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,992
Messages
6,128,165
Members
449,428
Latest member
d4vew

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