gpt4 book ai didi

excel - 在 Excel VBA 中删除包含 "xxx"的单元格(的行)的优雅方法

转载 作者:行者123 更新时间:2023-12-04 01:05:34 24 4
gpt4 key购买 nike

我有以下表格:

enter image description here

我想删除城市列中包含“美国”的行。前任。所需的输出如下:

enter image description here

我的方法是使用 =isnumber(search("USA",B2))C2:C4 处创建另一列。然后

for i = lastrow to 2 step -1
If cells(i, "C") then
Else
rows(i).delete
end if
next

这个方法可以,但是感觉很笨,在大数据集上运行很慢。对优雅地实现这一点有什么想法吗?

最佳答案

下面应该做你想做的 - 假设数据在 AB 列中的 Sheet1 上。

Sub Del_USA()
Dim c As Range

With Sheet1.Range("B:B")
Set c = .Find("*USA*", LookIn:=xlValues)
If Not c Is Nothing Then

With Sheet1.Cells(1, 1).CurrentRegion
.AutoFilter 2, "*USA*", 7
.Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
.AutoFilter
End With

Else
MsgBox "USA Not Found"
End If
End With

End Sub

关于excel - 在 Excel VBA 中删除包含 "xxx"的单元格(的行)的优雅方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66592353/

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