gpt4 book ai didi

vba - 检查值是否在范围内而不循环

转载 作者:行者123 更新时间:2023-12-03 08:37:53 25 4
gpt4 key购买 nike

我习惯于在python语法中检查7是否在list1中,您只需键入7 in list1并返回一个 bool(boolean) 值。如何在vba中执行类似的操作?

我目前正在大范围浏览。我想偶尔检查我循环遍历的值是否在其他范围内。如果我必须在循环中嵌套更多的循环,则速度可能会变慢。解决此问题的最快方法是什么?

For i = 400 To 1 Step -1:
'doing other things

'here's some psuedo-code of what I want to do
If Sheets("Sheet2").Cells(i, 2).Value In Sheets("Sheet1").Range("NamedRange")
Sheets("Sheet2").Cells(i, 2).EntireRow.Delete
End If

Next i

最佳答案

使用一个countif,如果它大于零,那么您知道它存在于列表中:

If Application.WorksheetFunction.CountIf(Sheets("Sheet1").Range("NamedRange"), Sheets("Sheet2").Cells(i, 2).Value) > 0 Then
Sheets("Sheet2").Cells(i, 2).EntireRow.Delete
End If

关于vba - 检查值是否在范围内而不循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64975435/

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