VBA StrComp 从不 returns 0
VBA StrComp never returns 0
我在使用 VBA 中的 StrComp
函数比较两个字符串时遇到问题。
Public Function upStrEQ(ByVal ps1 As String, ByVal ps2 As String) As Boolean
upStrEQ = False
If StrComp(ps1, ps2, vbTextCompare) = 0 Then
upStrEQ = True
End If
If Len(ps1) = Len(ps2) Then
Debug.Print ps1 & vbNewLine & ps2 & vbNewLine & upStrEQ
End If
End Function
调试输出:
Technischer Name
Technischer Name
Falsch
如您所见,这两个字符串具有相同的长度和相同的文本,但 upStrEQ
是 False
而 StrComp
不是 return 0.
任何帮助都会很好。谢谢。
更新:
由于传递给函数的字符串之一是在我制作示例文档之前从单元格中读取的,因此您可以重现我的错误:https://www.dropbox.com/s/6yh6d4h8zxz533a/strcompareTest.xlsm?dl=0
StrComp()
效果很好。问题出在您的输入上,可能是您隐藏了 space 或换行。
像这样测试你的代码:
Public Function upStrEQ(ByVal ps1 As String, ByVal ps2 As String) As Boolean
If StrComp(ps1, ps2, vbTextCompare) = 0 Then
upStrEQ = True
End If
If Len(ps1) = Len(ps2) Then
Debug.Print ps1 & vbNewLine & ps2 & vbNewLine & upStrEQ
End If
End Function
Public Sub TestMe()
Debug.Print upStrEQ("a", "a")
End Sub
另外,布尔函数的默认值为false,不需要一开始就设置。
为了清理您的输入,仅输入字母和数字,您可以使用自定义 RegEx
函数。因此,像这样的东西总是 return 个字母和数字:
Public Function removeInvisibleThings(s As String) As String
Dim regEx As Object
Dim inputMatches As Object
Dim regExString As String
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.pattern = "[^a-zA-Z0-9]"
.IgnoreCase = True
.Global = True
Set inputMatches = .Execute(s)
If regEx.test(s) Then
removeInvisibleThings = .Replace(s, vbNullString)
Else
removeInvisibleThings = s
End If
End With
End Function
Public Sub TestMe()
Debug.Print removeInvisibleThings("aa1 Abc 67 ( *^ 45 ")
Debug.Print removeInvisibleThings("aa1 ???!")
Debug.Print removeInvisibleThings(" aa1 Abc 1267 ( *^ 45 ")
End Sub
在您的代码中,当您将参数 ps1
和 ps2
传递给 upStrEQ
时使用它。
我在使用 VBA 中的 StrComp
函数比较两个字符串时遇到问题。
Public Function upStrEQ(ByVal ps1 As String, ByVal ps2 As String) As Boolean
upStrEQ = False
If StrComp(ps1, ps2, vbTextCompare) = 0 Then
upStrEQ = True
End If
If Len(ps1) = Len(ps2) Then
Debug.Print ps1 & vbNewLine & ps2 & vbNewLine & upStrEQ
End If
End Function
调试输出:
Technischer Name
Technischer Name
Falsch
如您所见,这两个字符串具有相同的长度和相同的文本,但 upStrEQ
是 False
而 StrComp
不是 return 0.
任何帮助都会很好。谢谢。
更新: 由于传递给函数的字符串之一是在我制作示例文档之前从单元格中读取的,因此您可以重现我的错误:https://www.dropbox.com/s/6yh6d4h8zxz533a/strcompareTest.xlsm?dl=0
StrComp()
效果很好。问题出在您的输入上,可能是您隐藏了 space 或换行。
像这样测试你的代码:
Public Function upStrEQ(ByVal ps1 As String, ByVal ps2 As String) As Boolean
If StrComp(ps1, ps2, vbTextCompare) = 0 Then
upStrEQ = True
End If
If Len(ps1) = Len(ps2) Then
Debug.Print ps1 & vbNewLine & ps2 & vbNewLine & upStrEQ
End If
End Function
Public Sub TestMe()
Debug.Print upStrEQ("a", "a")
End Sub
另外,布尔函数的默认值为false,不需要一开始就设置。
为了清理您的输入,仅输入字母和数字,您可以使用自定义 RegEx
函数。因此,像这样的东西总是 return 个字母和数字:
Public Function removeInvisibleThings(s As String) As String
Dim regEx As Object
Dim inputMatches As Object
Dim regExString As String
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.pattern = "[^a-zA-Z0-9]"
.IgnoreCase = True
.Global = True
Set inputMatches = .Execute(s)
If regEx.test(s) Then
removeInvisibleThings = .Replace(s, vbNullString)
Else
removeInvisibleThings = s
End If
End With
End Function
Public Sub TestMe()
Debug.Print removeInvisibleThings("aa1 Abc 67 ( *^ 45 ")
Debug.Print removeInvisibleThings("aa1 ???!")
Debug.Print removeInvisibleThings(" aa1 Abc 1267 ( *^ 45 ")
End Sub
在您的代码中,当您将参数 ps1
和 ps2
传递给 upStrEQ
时使用它。