How to find multiple keywords in a cell from a worksheet and move it to another worksheet

systemx

New Member
Joined
Mar 27, 2013
Messages
4
Hello everyone.

I have an excel file that contains two sheets (Sheet 1 and Sheet 2)
- Sheet 1 contains the following data in A1 below:
specs:HDD=WD, Fan=CM, GFX=Leadtek, Mouse=Logitech
specs:HDD=Seagate, Fan=Shark, GFX=Nvidia, Mouse=Hp
specs:HDD=WD, Fan=Bay, GFX=Leadtek, Mouse=Microsoft
specs:HDD=WD, Fan=CM,GFX=Nvidia, Mouse=Dell
specs:HDD=Seagate, Fan= Bay, GFX=Asus, Mouse=Hp

I would like to use a macro to move the cells from A1 into Sheet2 if any of the following keywords match the below:
HDD=WD
GFX=Leadtek

Is it possible to have the following data below in Sheet2?
specs:HDD=WD, Fan=CM, GFX=Leadtek, Mouse=Logitech
specs:HDD=WD, Fan=Bay, GFX=Leadtek, Mouse=Microsoft
specs:HDD=WD, Fan=CM, GFX=Nvidia, Mouse=Dell

I've tried using Application.Match but it only works if I am passing a single keyword like Application.Match("HDD=WD", Range("Sheet1!$A$1"), 0). How would I be able to either match "HDD=WD" and/or "GFX=Leadtek" and then move the cell to Sheet2? I may be doing this the wrong way and am open to any suggestions, thanks in advance!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
if this text is in cell A1
specs:HDD=WD, Fan=CM, GFX=Leadtek, Mouse=Logitech
specs:HDD=Seagate, Fan=Shark, GFX=Nvidia, Mouse=Hp
specs:HDD=WD, Fan=Bay, GFX=Leadtek, Mouse=Microsoft
specs:HDD=WD, Fan=CM,GFX=Nvidia, Mouse=Dell
specs:HDD=Seagate, Fan= Bay, GFX=Asus, Mouse=Hp

then you could use the instr function
 
Upvote 0
if this text is in cell A1


then you could use the instr function

Thank you for the tip. I did use instr previously but I failed to understand how it actually worked. Below is the code that I've come up with, it does feel like it is working as it should .. are there any errors with it or do you have a better way of doing it? Thanks!

Code:
Sub Button1_Click()
    For Each Cell In ActiveSheet.UsedRange.Cells
    
    If InStr(Cell.Value, "HDD=WD") > 0 Or InStr(Cell.Value, "GFX=Leadtek") > 0 Then
            Cell.Copy
            Sheets("Sheet2").Activate
            
            With ActiveSheet
                lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                
                If (ActiveSheet.Range("A1").Select = True And ActiveSheet.Range("A1").Value <> "") Then
                    Range("A" & lastRow + 1).Select
                End If
            End With
            
            ActiveSheet.Paste
        End If
    Next
End Sub
 
Upvote 0
Thank you for the tip. I did use instr previously but I failed to understand how it actually worked. Below is the code that I've come up with, it does feel like it is working as it should .. are there any errors with it or do you have a better way of doing it? Thanks!

Code:
Sub Button1_Click()
    For Each Cell In ActiveSheet.UsedRange.Cells
    
    If InStr(Cell.Value, "HDD=WD") > 0 Or InStr(Cell.Value, "GFX=Leadtek") > 0 Then
            Cell.Copy
            Sheets("Sheet2").Activate
            
            With ActiveSheet
                lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                
                If (ActiveSheet.Range("A1").Select = True And ActiveSheet.Range("A1").Value <> "") Then
                    Range("A" & lastRow + 1).Select
                End If
            End With
            
            ActiveSheet.Paste
        End If
    Next
End Sub

you don't need to activate the sheet for copy paste you can do it like

Code:
Sub Button1_Click()
    For Each Cell In ActiveSheet.UsedRange.Cells
    
    If InStr(Cell.Value, "HDD=WD") > 0 Or InStr(Cell.Value, "GFX=Leadtek") > 0 Then
      Cell.Copy Sheets("Sheet2").Cells(Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1, "A")
     End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,605
Messages
6,056,269
Members
444,853
Latest member
sam69

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