首页 > 解决方案 > 如果单元格的文本中有特定单词,则突出显示该单元格

问题描述

我想红色突出显示包含“颜色”和“宽度”一词的所有单元格。

使用我拥有的代码,我输入单元格的值。我想输入单词“颜色”和“宽度”。

Sub test()    

aaa = InputBox("Enter value 1:")
bbb = InputBox("Enter value 2:")

Dim myrange As Range
Set myrange = ThisWorkbook.Worksheets("Tabell").UsedRange

For Each cell In myrange.Cells

    If cell.Value = aaa Or cell.Value = bbb Then
        cell.Interior.Color = 255
    End If

Next

End Sub

在此处输入图像描述

标签: excelvba

解决方案


您可以使用Like.

编辑- 更新以允许输入多个术语(未经测试)

dim terms as new collection, term, deleteMe as boolean

do
    term = Trim(InputBox("Enter value (leave blank to end entry)"))
    if len(term)>0 then
        terms.add term
    else
        exit do
    end if
loop

if terms.count=0 then exit sub '<< no terms to check for

For Each cell In myrange.Cells

    deleteMe = true

    for each term in terms
        if not cell.value like term & "*" then
            deleteMe = false 
            exit for
        end if
    next term

    if deleteMe then cell.value = ""
next cell

推荐阅读