比较 VBA 中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24370665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:22:32  来源:igfitidea点击:

Comparing Strings in VBA

stringvbacompare

提问by user3768212

I have a basic programming background and have been self sufficient for many years but this problem I can't seem to solve. I have a program in VBA and I need to compare two strings. I have tried using the following methods to compare my strings below but to no avail:

我有基本的编程背景,多年来一直自给自足,但这个问题我似乎无法解决。我在 VBA 中有一个程序,我需要比较两个字符串。我尝试使用以下方法来比较下面的字符串,但无济于事:

//Assume Cells(1, 1).Value = "Cat"

Dim A As String, B As String

A="Cat"

B=Cell(1, 1).Value

If A=B Then...

If A Like B Then...

If StrCmp(A=B, 1)=0 Then...

I've even tried inputting the Strings straight into the code to see if it would work:

If "Cat" = "Cat" Then...

If "Cat" Like "Cat" Then...

If StrCmp("Cat" = "Cat", 1) Then...

VBA for some reason does not recognize these strings as equals. When going through Debugger it shows that StrComp returns 1. Do my strings have different Char lengths? Thanks for any help.

由于某种原因,VBA 无法将这些字符串识别为相等。当通过调试器时,它显示 StrComp 返回 1。我的字符串有不同的字符长度吗?谢谢你的帮助。

回答by David Zemens

Posting as answer because it doesn't fit in the comments:

发布为答案,因为它不适合评论:

I find it hard to believe that something like:

我发现很难相信这样的事情:

MsgBox "Cat" = "Cat"

Would not display Trueon your machine. Please verify.

不会显示True在您的机器上。请核实。

However, I do observe that you are most certainly using StrCompfunction incorrectly.

但是,我确实观察到您肯定StrComp错误地使用了函数。

The proper use is StrComp(string, anotherstring, [comparison type optional])

正确的用法是 StrComp(string, anotherstring, [comparison type optional])

When you do StrComp(A=B, 1)you are essentially asking it to compare whether a boolean (A=Bwill either evaluate to True or False) is equivalent to the integer 1. It is not, nor will it ever be.

当您这样做时,StrComp(A=B, 1)您实际上是在要求它比较 boolean (A=B将评估为 True 或 False)是否等效于 integer 1。它不是,也永远不会。

When I run the following code, all four message boxes confirm that each statement evaluates to True.

当我运行以下代码时,所有四个消息框都确认每个语句的计算结果为 True。

Sub CompareStrings()
Dim A As String, B As String
A = "Cat"
B = Cells(1, 1).Value


MsgBox A = B

MsgBox A Like B

MsgBox StrComp(A, B) = 0 

MsgBox "Cat" = "Cat" 


End Sub

Update from comments

从评论更新

I don't see anything odd happening if I use an array, just FYI. Example data used in the array:

如果我使用数组,我看不到任何奇怪的事情发生,仅供参考。数组中使用的示例数据:

enter image description here

在此处输入图片说明

Modified routine to use an array:

修改例程以使用数组:

Sub CompareStrings()
Dim A As String, B() As Variant

A = "Cat"
B = Application.Transpose(Range("A1:A8").Value)

For i = 1 To 8

    MsgBox A = B(i)

    MsgBox A Like B(i)

    MsgBox StrComp(A, B(i)) = 0

    MsgBox "Cat" = B(i)

Next

End Sub

What I would check is how you're instantiating the array. Range arrays (as per my example) are base 1. If it assigned some other way, it is most likely base 0, so check to make sure that you're comparing the correct array index.

我要检查的是您如何实例化数组。范围数组(根据我的示例)以 1 为基数。如果它以其他方式分配,则很可能以 0 为基数,因此请检查以确保您正在比较正确的数组索引。