Searching and matching characters between 2 column

2019-09-07 02:23发布

I have 2 worksheets (Worksheet 1 & Worksheet 2). I am trying to searching the characters in column A on Worksheet 1 and see if those characters exist in Column A on Worksheet 2. Please note that these characters are not fixed, so characters may be added, removed, or changed. If they exists in Worksheet 2, then I would bold the characters in Worksheet 1 to the corresponding cell.

Worksheet 1

Column A

Jack
James
Peter
Angel

Worksheet 2

Column A

Jack ate an apple.
Peter flies a kite.
Henry wakes up for work.
James went to school.

My approach to this so far is using For loops and if-else statements. I was thinking about using InStr to find the characters within those sentence in Worksheet 2.

Dim StartNum As Integer
Dim EndNum As Integer
Dim rng1 As Range
Dim rng2 As Range

EndNum =

For StartNum = 1 To EndNum
    If rng1.Value = InStr(rng2.Value) Then _
        rng1.font.Bold = True
End If

2条回答
啃猪蹄的小仙女
2楼-- · 2019-09-07 02:45

A simple macro would do the work for you. Suppose you have a Sheet with Names and another with Sentences. So here is a small code:

Sub HighlightFound()
Dim searchRange As Range
Set searchRange = ActiveWorkbook.Sheets("Sentences").Range("A:A")

ActiveWorkbook.Sheets("Names").Activate
Range("A1").Select

Do While Not IsEmpty(ActiveCell)
    If Not searchRange.Find(ActiveCell.FormulaR1C1, LookIn:=xlValues) Is Nothing Then
        ActiveCell.Font.Bold = True
    End If
    ActiveCell.Offset(RowOffset:=1).Select
Loop

End Sub

HAPPY VBA CODING!!!

查看更多
Ridiculous、
3楼-- · 2019-09-07 03:01

Select all of column A on worksheet 1 with A1 as the active cell. Choose Home ► Conditional Formatting ► New Rule* and when the **New Formatting Rule* dialog appears, choose Use a formula to determine which cells to format then supply the following in the Format values where this formula is true: text box.

 =isnumber(match("*"&A1&"*", 'Worksheet 2'!A:A, 0))

Click Format and select Bold from the Font tab. Click OK once to accept the formatting and then OK again to create the new rule.

I added a few non-matching names to Worksheet 1 and topped up the formatting change with a bold blue font for these results.

        cONDITIONAL fORMATTING WITH tEXT WITHIN tEXT

查看更多
登录 后发表回答