比较 VBA 中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16950904/
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-11 21:29:38  来源:igfitidea点击:

Comparing Variable in VBA

excelvbaexcel-vbams-office

提问by Zach B

I am looking for a method of comparing two string variables in VBA. The current statement I am using (which is not producing the desired result) is:

我正在寻找一种在 VBA 中比较两个字符串变量的方法。我正在使用的当前语句(没有产生预期的结果)是:

  • If Variable1 = Variable2 Then...
  • If Variable1 = Variable2 Then...

I have a VBA macro that assigns two variables in the first part of my code:

我有一个 VBA 宏,它在我的代码的第一部分中分配了两个变量:

  • Dim Variable1 as String Variable1 = Left(Range("$F$3").Value, 4)
  • Dim Variable2 as String Variable2 = ("SheetName.B15")
  • Dim Variable1 as String Variable1 = Left(Range("$F$3").Value, 4)
  • Dim Variable2 as String Variable2 = ("SheetName.B15")

I have tried using the following statement, but it still does not work correctly:

我已经尝试使用以下语句,但它仍然无法正常工作:

  • If Variable1 Like Variable2 Then...
  • If Variable1 Like Variable2 Then...

Previously, the statement was working with a string comparison to the first variable, but it required that I hard-coded the "ABC" string. It looked like the below statement:

以前,该语句使用与第一个变量的字符串比较,但它要求我对“ABC”字符串进行硬编码。它看起来像下面的语句:

  • If Variable1 = "ABC" Then...
  • If Variable1 = "ABC" Then...

Does anybody know the syntax of a statement to compare two string variables, or could recommend a method for comparing them in a sub and return a binary result, which I could then compare?

有没有人知道比较两个字符串变量的语句的语法,或者可以推荐一种在子中比较它们并返回二进制结果的方法,然后我可以进行比较?

Thanks!

谢谢!



Running VBA for Excel 2013 on Windows 8

在 Windows 8 上运行 Excel 2013 的 VBA

回答by isJustMe

You can use StrComp

您可以使用 StrComp

 Sub compare()
        MsgBox StrComp("Stack", "stack") 'alerts -1 
    End Sub

Sub compare()
    MsgBox StrComp("Stack", "stack", 1) 'Compare as text while ignoring case
End Sub

The first one is case sensitive where the 2nd is not, of course you can replace the hardcoded values with variables

第一个区分大小写,第二个不区分大小写,当然您可以用变量替换硬编码值

回答by Zach B

Here is the solution:

这是解决方案:

I wasn't correctly assigning Variable2 and therefore, the function wasn't producing the desired result. Thanks everyone for their contributions, through the reply's - here's what we've come up with:

我没有正确分配 Variable2,因此,该函数没有产生预期的结果。感谢大家的贡献,通过回复 - 这是我们提出的:

To compare two string variables in an existing sub, use:

要比较现有子中的两个字符串变量,请使用:

If Variable1 = Variable 2 Then...

To compare two string variable in it's own sub, use:

要比较它自己的子中的两个字符串变量,请使用:

Sub compare()
    MsgBox StrComp("Stack", "stack") 'alerts -1 
End Sub

Sub compare()
    MsgBox StrComp("Stack", "stack", 1) 'Compare as text while ignoring case
End Sub