VBA – 在string中search数组中的所有值

这里只是一个效率问题。 我基本上循环通过列中的单元格,以查看它们是否包含特定的string,但是那个单元格也不需要包含14个不同的string中的任何一个。 我目前的解决scheme是findstring,然后使用instr在单元格中循环访问数组。 但是,这个macros在运行时可能会发生数百次。 我很好奇,如果有更好的方法。

例如:

NotArr = Array("blah1", "blah2", "blah3", "etc") For r = 1 to 10 'Let's assume I've already found the first string For i = 1 to 4 If Not InStr(Cells(r, 1).value, NotArr(i)) > 0 Then 'Do things End If Next i Next r 

注意:我知道我可能已经过时了,或者只是错过了明显的。 我已经在VBA中被埋在了大约6个星期@ 10个小时的时间里,可以感觉到我的大脑正在融化。

谢谢大家!

为了避免循环遍历单元格,这需要VBA与Excel范围进行数百次交互,为什么不尝试将数据存储在数组中呢? 这应该会大大加速。

 Dim arr() as Variant arr = Range("A1:G10") For each rng in arr For i = 1 to 4 If Not InStr(rng.value, NotArr(i)) > 0 Then 'Do things End If Next i Next