如何比较excel值的输入值并使用VBA突出显示它?

时间:2014-03-19 04:22:34

标签: vba

我有一个名为data的变量,其输入值为=" Vince" ..

然后我想将数据与Excel中的值进行比较,如果有相同的值,则会突出显示excel值。

总结。 data =" Vince&#34 ;;

如果cells.rows.value = data则

cell.rows.value.highlight。

如果

结束

1 个答案:

答案 0 :(得分:0)

我在这个答案中包含了两个选项:

选项1 - 查找并突出显示包含" Vince"如果它们尚未突出显示,则单元格中的任何位置。例如,这也将突出显示包含"我是Vince","省"等的单元格。

Sub FindAndHighlight1()

Dim FindCount   As Long
Dim curCell     As Range
Dim i           As Long

With Worksheets("Sheet1").UsedRange
    FindCount = Application.WorksheetFunction.CountIf(.Cells, "*Vince*")
    Set curCell = .Cells.Find(What:="Vince")
    For i = 1 To FindCount
        If curCell.Interior.Color <> 65535 Then curCell.Interior.Color = 65535
        Set curCell = .FindNext(curCell)
    Next i
End With

End Sub

选项2 - 查找并突出显示仅包含&#34; Vince&#34;如果他们没有突出显示

Sub FindAndHighlight2()

Dim FindCount   As Long
Dim curCell     As Range
Dim i           As Long

With Worksheets("Sheet1").UsedRange
    FindCount = Application.WorksheetFunction.CountIf(.Cells, "Vince")
    Set curCell = .Cells.Find(What:="Vince")
    For i = 1 To FindCount
        If curCell.Value <> "Vince" Then
            i = i - 1
            Set curCell = .FindNext(curCell)
        ElseIf curCell.Interior.Color <> 65535 Then
            curCell.Interior.Color = 65535
            Set curCell = .FindNext(curCell)
        End If
    Next i
End With

End Sub

如有必要,请更改"Vince"What:="Vince"以引用您的变量,并在必要时更改Worksheets("Sheet1")