[英]What is VBA's String Comparison Algorithm?
我试图加快我的程序搜索数组中的字符串的方式。 因此,例如,我正在编写一个程序,该程序在1000个随机单词列中搜索单词“test”。
目前我的程序很简单:
If Cells(x,1).Value = "test" Then
...
End If
但是,这是我的想法,
If Left(Cells(x,1).Value,1) = "t" Then
If Left(Cells(x,1).Value,2) = "te" Then
... and so on ...
End If
但后来我开始怀疑,当我要求VBA
测试以查看value = "test"
,它是否经历了我在第二个代码中概述的过程? 基本上,我的问题是,第二个代码是多余的吗? 我不熟悉VBA本身如何寻找匹配的整个字符串。 如果有人能够了解VBA在我要求它寻找Value = "string"
时所经历的事情,它可能真的有帮助。 谢谢!
Range.Find
方法查找某个范围内是否存在给定值: Dim rng as Range
Dim found as Range
Dim val As String
val = "test" '## Modify as needed
Set rng = Range("A1:A1000") '## Modify as needed
Set found = rng.Find(val)
'The Range.Find method will return a Nothing if the value is not found
If found Is Nothing Then
MsgBox "Not found!"
Else
'do something with it...
End If
Range.Find
方法具有可选参数,可用于指定整体或部分匹配,区分大小写等。
Match
功能在范围内查找给定值。 注意: Application.Match
函数类似于WorksheetFunction.Match
除非如果找不到匹配, Application
类将返回一个错误类型(因此需要将其返回值定义为Variant
,而WorksheetFunction
类将引发错误) )。
Dim rng as Range
Dim found as Variant
Dim val as String
val = "test" '## Modify as needed
Set rng = Range("A1:A1000") '## Modify as needed
found = Application.Match("test", rng, False)
'The Application.Match function will return an Error Type if the val is not found
If IsError(found) Then
MsgBox "Not found!"
Else
'do something with it
End If
Match
函数的限制: Match
函数仅适用于精确匹配(使用False
参数)或近似匹配(使用True
, msoTrue
或msoCTrue
参数),其中内置了一些其他假设,即数据已排序)。 Match
功能只能用于单列范围或单行范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.