gpt4 book ai didi

excel - 如何搜索多个模式以删除行?

转载 作者:行者123 更新时间:2023-12-04 21:49:47 24 4
gpt4 key购买 nike

我想创建一个宏来删除包含某些值的 Excel 文档的所有行,比如“红色”、“蓝色”和“黄色”。

我发现了一些适用于单个值的代码。我尝试进行一些更改,但无法使其适用于多个值。

Sub Colors() 

Dim pattern As String
pattern = "red"
RowCount = ActiveSheet.UsedRange.Rows.Count

Dim i As Integer
For i = 1 To RowCount
Dim j As Integer
For j = 1 To 1
If Cells(i, j) = pattern Then
Cells(i, j).EntireRow.Delete
End If
Next j
Next i

End Sub

你如何列出更多的模式?

最佳答案

像这样的东西应该工作。您可以使用集合来保存所有模式并对其进行迭代。或者,您可以只在 If 语句上使用一系列 or 语句。

做了一些调整来改善这一点。您可能还想明确声明一个工作表,您正在与我没有做的单元格交互。另外,我删除了 For j循环,因为它不需要。

Option Explicit

Sub Colors()

Dim i As Long
Dim j As Long
Dim RowCount As Long
Dim patterns As Collection: Set patterns = New Collection
Dim pattern As Variant

patterns.Add "red"
patterns.Add "blue"
patterns.Add "yellow"

RowCount = ActiveSheet.UsedRange.Rows.Count

For i = RowCount To 1 Step -1
For Each pattern In patterns
If Cells(i, 1) = pattern Then
Cells(i, 1).EntireRow.Delete
exit for
end if
Next
Next

End Sub

关于excel - 如何搜索多个模式以删除行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56903841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com