简体   繁体   English

VBA字符串比较失败

[英]VBA string comparison failure

i met interesting issue when im comparing two strings. 我在比较两个字符串时遇到了一个有趣的问题。 Im reading data from file and everything works well. 我从文件中读取数据,一切正常。 But then co-worker send me input file, which is just CTRL+C and CTRL+V of working file. 但是随后同事给我发送了输入文件,它只是工作文件的CTRL + C和CTRL + V。 And then miracle happend! 然后奇迹发生了! VBA is so confused, that cant compare two simple strings and i fell of chair. VBA是如此混乱,以至于无法比较两个简单的字符串,而我跌倒了。

If you take a look at image you can see that comparison passed if condition where are two same strings, but it should not. 如果看一下图像,可以看到如果条件where是两个相同的字符串,则该比较通过了,但事实并非如此。 Im a bit confused how this can happen. 我有点困惑这是怎么发生的。

在此处输入图片说明

So met someone something like this? 所以遇到这样的人吗? Im realy start thinking about something like machine revolution from Terminator. 我真的开始考虑诸如《终结者》中的机器革命之类的事情。 (files are both saved in notepad++ and there are no strange characters or something like that) (文件都保存在记事本++中,没有奇怪的字符或类似的东西)

Progress update 进度更新
So i tried hints from guys in comments below. 所以我在下面的评论中尝试了来自伙计们的提示。 and ended with something like this 最后是这样的

If CStr(Trim(rowArray(4))) <> (CStr("N/A")) Then

Contentent of rowArray(4) is still "N/A" string as on picture above and excel still thinks this strings arent same. rowArray(4)的内容仍然是“ N / A”字符串,如上图所示,excel仍然认为该字符串相同。 I also saved file in pspad, netbeans, and normal notepad and issue is still same. 我也将文件保存在pspad,netbeans和普通记事本中,问题仍然相同。

Use the immediate window to test the contents of the variable: 使用立即窗口来测试变量的内容:

For i = 1 To Len(rowArray(4)): Print Asc(Mid(rowArray(4), i, 1)): Next

This will print the ASCII value of each character in the string - you can use this to determine what the extra character(s) are causing the issue. 这将打印字符串中每个字符的ASCII值-您可以使用它来确定导致此问题的其他字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM