Searching and matching characters between 2 columns on 2 worksheets

拥有回忆 提交于 2019-12-25 03:33:03

问题


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

回答1:


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.

        




回答2:


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!!!



来源:https://stackoverflow.com/questions/30630716/searching-and-matching-characters-between-2-columns-on-2-worksheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!