如果多个数据在范围内匹配,如何清空单元格内容?

时间:2019-05-24 09:26:17

标签: excel vba

我有一个Excel工作表,如果在一个范围内找到多个匹配项,我想在其中清除单元格数据。

示例图片

enter image description here

如果找到匹配项,例如 SUP AL ,则清除内容并为与找到的匹配项相对应的单元格上色(例如SUP-黄色,AL-红色)。我用下面的代码无法正常工作。

Dim l As Long, lRow As Long
With Sheets("Sheet1")
    lRow = .Range("A" & Rows.Count).End(xlUp).Row
    For l = 1 To lRow
    For Each c In Range("A:AE")
    If c.Value = "SUP" Then
    c.Value = ""
    End If
    Next l
End With

2 个答案:

答案 0 :(得分:0)

这应该工作

Option Explicit

Sub test()

Dim c As Range, arr As Range

Set arr = Range("A7:AE7")

For Each c In arr
    If InStr(1, c, "SUP") Or InStr(1, c, "AL") Then
       c.Clear
    End If
Next

End Sub

答案 1 :(得分:0)

尝试一下:

Dim r As Range, c As Range
Dim sh As Worksheet

For Each sh In Thisworbook.Worksheets '// loop on all worksheet //
  With sh

    Set r = .Range("A1:AE7") '// adjust to your actual range //
    For Each c In r
      With c
        Select Case UCase(.Value2) '// non case sensitive search //
        Case "SUP": .ClearContents: .Interior.Color = RGB(255, 255, 0)
        Case "AL": .ClearContents: .Interior.Color = RGB(255, 0, 0)
        End Select
      End With
    Next

  End With
Next

Edit1:添加了遍历所有工作表和不区分大小写的搜索的循环。没有经过测试,但是应该让您知道如何做。