Hide rows that contain first two characters of a cell in adjacent cell

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I am challenged here and need a tech support team:

In column C I want to compare it's first two characters in each cell with the contents of cells in column D to see if those characters exist in the cell content then hide all rows that don't have a match.

So say I have "only" in C2 and "Money" in D2, the "on" can be found in "money" so the match is found. The characters in column D should follow each other. "on" <>"no"

For the same "only" from column C, if there is the word "column" in column D, there should be no match since the "o" and "n" are not consecutive.

Then move down to C3 and compare it's first two characters to D3 etc

I am looking for a macro to do that for me.

Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
See how this goes.
Code:
Sub CopyIfFirstTwoMatch()
  Dim rCrit As Range
  
  Sheets("Sheet3").UsedRange.ClearContents
  With ActiveSheet.UsedRange
    Set rCrit = .Offset(, .Columns.Count).Resize(2, 1)
    rCrit.Cells(2).Formula = "=SEARCH(TEXT(C2,""dd""),D2)"
    .AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=Sheets("Sheet3").Range("A1"), Unique:=False
  End With
  rCrit.ClearContents
End Sub
 
Upvote 0
See how this goes.
Code:
Sub CopyIfFirstTwoMatch()
  Dim rCrit As Range
  
  Sheets("Sheet3").UsedRange.ClearContents
  With ActiveSheet.UsedRange
    Set rCrit = .Offset(, .Columns.Count).Resize(2, 1)
    rCrit.Cells(2).Formula = "=SEARCH(TEXT(C2,""dd""),D2)"
    .AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=Sheets("Sheet3").Range("A1"), Unique:=False
  End With
  rCrit.ClearContents
End Sub


It went well - exactly what I wanted.
I am very grateful
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,332
Members
449,155
Latest member
ravioli44

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