簡體   English   中英

什么是VBA的字符串比較算法?

[英]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參數)或近似匹配(使用TruemsoTruemsoCTrue參數),其中內置了一些其他假設,即數據已排序)。 Match功能只能用於單列范圍或單行范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM