【问题标题】:Excel VBA compare strings with spaces, percentage signs, and commasExcel VBA 将字符串与空格、百分号和逗号进行比较
【发布时间】:2014-08-29 18:14:10
【问题描述】:

更新:我将我的示例 html 文件从这里复制并粘贴到一个新文件中,以确保人们可以使用它进行测试,然后我的代码就可以工作了!我认为这是一个编码问题,因为原始文件是从 Linux 机器自动生成的。我尝试保存一个新文件并从脚本中重新打开该文件,但这不起作用。我希望我仍然可以仅使用一个 VBA 宏来解决此问题,方法是以某种方式保存一个新文件并使用该文件中的数据或确保编码匹配。


我似乎无法正确比较以下字符串:

line, %

我一直在尝试使用 StrComp 函数并且只使用 equal 符号,但两者都没有奏效。我还在要比较的字符串上使用了 Trim 函数,以确保两端没有空格。我可以通过调试器跟踪并看到我正在比较的值都是“line, %”,但表达式永远不会计算为 true。

此字符串中的任何字符是否导致我必须执行一些特殊操作才能进行比较?

编辑:这确实有效(我在上次编辑时打错了字):

Dim percentString As String
percentString = "line, %"

Dim test As String
test = percentString
If StrComp(percentString, test) = 0 Then
    MsgBox "They are equal"
End If

但是,我正在从 html 文件中读取输入,但这是行不通的。请参阅下面的代码,您可以尝试。

也许它与 html 输入中的 charset=ISO-8859-1 有关?字符集重要吗?这是开头的内容:<HTML><HEAD><META CONTENT="text/html; charset=ISO-8859-1" HTTP-EQUIV="Content-Type"/>

这是一个您可以尝试的示例

testFile.html

<HTML><HEAD><META CONTENT="text/html; charset=ISO-8859-1" HTTP-EQUIV="Content-Type"/><TITLE>title</TITLE><STYLE> 

TABLE,TD,TH {border-style:solid; border-color:black;} TD,TH {background:white;margin:0;line-height:100%;padding-

left:0.5em;padding-right:0.5em;} TD {border-width:0 1px 0 0;} TH {border-width:1px 1px 1px 0;} TR TD.h {color:red;} TABLE 

{border-spacing:0; border-collapse:collapse;border-width:0 0 1px 1px;} P,H1,H2,H3,TH {font-family:verdana,arial,sans-

serif;font-size:10pt;} TD {font-family:courier,monospace;font-size:10pt;} TABLE.hdft {border-spacing:0;border-

collapse:collapse;border-style:none;} TABLE.hdft TH,TABLE.hdft TD {border-style:none;line-height:normal;} TABLE.hdft 

TH.tl,TABLE.hdft TD.tl {background:#6699CC;color:white;} TABLE.hdft TD.nv {background:#6633DD;color:white;} .nv A:link 

{color:white;} .nv A:visited {color:white;} .nv A:active {color:yellow;} TABLE.hdft A:link {color:white;} TABLE.hdft 

A:visited {color:white;} TABLE.hdft A:active {color:yellow;} .in {color:#356085;} TABLE.s TD {padding-left:0.25em;padding-

right:0.25em;} TABLE.s TD.l {padding-left:0.25em;padding-right:0.25em;text-align:right;background:#F0F0F0;} TABLE.s TR.z TD 

{background:#FF9999;} TABLE.s TR.p TD {background:#FFFF88;} TABLE.s TR.c TD {background:#CCFFCC;} A:link 

{color:#0000EE;text-decoration:none;} A:visited {color:#0000EE;text-decoration:none;} A:hover {color:#0000EE;text-

decoration:underline;} TABLE.cn {border-width:0 0 1px 0;} TABLE.s {border-width:1px 0 1px 1px;} TD.h {color:red;border-

width:0 1px 0 0;} TD.f {border-width:0 1px 0 1px;} TD.hf {color:red;border-width:0 1px 0 1px;} TH.f {border-width:1px 1px 

1px 1px;} TR.cis TD {background:#F0F0F0;} TR.cis TD {border-width:1px 1px 1px 0;} TR.cis TD.h {color:red;border-width:1px 

1px 1px 0;} TR.cis TD.f {border-width:1px 1px 1px 1px;} TR.cis TD.hf {color:red;border-width:1px 1px 1px 1px;} TD.b 

{border-style:none;background:transparent;line-height:50%;}  TD.bt {border-width:1px 0 0 0;background:transparent;line-

height:50%;} TR.o TD {background:#F0F0F0;}TABLE.it {border-style:none;}TABLE.it TD,TABLE.it TH {border-style:none;}

</STYLE></HEAD><BODY><TABLECELLSPACING="0" WIDTH="100%"><TR><TH>Report</TH></TR><TR><TD></TD></TR></TABLE><H2></H2><TABLE 

CELLSPACING="0" WIDTH="100%"><TR><TH>line, %</TH></TR><TR><TD>Name</TD></TR></TABLE><H3></H3><TABLE CELLSPACING="0" 

WIDTH="100%"><TR><TH>line, %</TH></TR><TR><TD>test</TD></TR></TABLE><P></P><TABLE CELLSPACING="0" 

WIDTH="100%"><TR><TD></TD></TR><TR><TD></TD></TR></TABLE></BODY></HTML>

使用此 VBA 代码并稍作修改以指定 testFile.html 的位置

Option Explicit
Sub percent()

    Dim MyFile As String
    MyFile = "testFile.html"

    Dim percentString As String
    percentString = CStr("line, %")

    Dim ws As Worksheet

    Workbooks.Open Filename:= _
    "path\to\testFile" & MyFile

    Dim stringVal As String
    Dim val As Variant
    Set ws = Worksheets("testFile")
    For Each val In ws.UsedRange.Value
        stringVal = CStr(val)
        stringVal = Replace(stringVal, vbLf, "")
        stringVal = Replace(stringVal, vbTab, "")
        stringVal = Trim(Application.Clean(stringVal))
        percentString = Replace(percentString, vbLf, "")
        percentString = Replace(percentString, vbTab, "")
        percentString = Trim(Application.Clean(percentString))
        Dim res As Integer
        res = StrComp(stringVal, percentString)
        If res = 0 Then
            MsgBox "Found the percent string"
        End If
    Next val
    Workbooks(MyFile).Close
End Sub

【问题讨论】:

  • 如何将字符串分配给您的变量以进行比较?我做了一个简单的测试,当我使用各种方法比较它时,“line, %”没有任何问题...
  • @sous2817 我正在使用 Workbooks.Open 打开一个 html 文件。然后我得到工作表并使用 For Each 循环遍历 theSheet.UsedRange.Value
  • 好吧,我猜是因为 HTML 的缘故,会出现一些垃圾/隐藏字符。试试下面 Dan 的建议,看看是否有帮助...
  • 如果您可以在某处发布您的数据样本以供我们尝试不同的事情,这也会有所帮助。
  • @sous2817 查看我的编辑。即使我将它们设置为相等,我仍然无法得到一个 if 语句来评估为真。

标签: vba excel string-comparison


【解决方案1】:

原来存在编码问题。基本上,只有在我编写了一个将所有 line, % 行更改为其他内容的脚本之后,调试器中才会出现 字符。因此,下面去掉了 Â 字符,而这个字符是我无法得到比较评估为 true 的原因:

stringVal = Replace(stringVal, Chr(194), vbNullString)

对所有试图提供帮助的人深表歉意。事实证明,如果我可以上传输入文件本身,这样的问题只有在尝试回答时才有用,这样编码就会保留下来。

【讨论】:

    【解决方案2】:

    当我过去遇到这个问题时,我做了两件事:

    1) 通过创建具有以下内容的单元格来查看字符串是什么:

    B1= "|" & A1 & "|"
    

    我在两个单元格上都使用它,看看是否有什么奇怪的地方。有时会有标签或新行没有显示出来。

    2) 我用奇怪的东西代替。如果你找到一个新行,你可以使用 find replace 将它全部删除,或者你可以使用 substr() 替换。

    希望这会有所帮助。如果您发布 1 的结果,我可能会提供更多帮助。

    【讨论】:

    • 谢谢,我把“|”在单元格中,没有空格。我还尝试使用以下函数来删除任何字符:percentString = Replace(percentString, vbLf, "") percentString = Replace(percentString, vbTab, "") percentString = Trim(Application.Clean(percentString))
    【解决方案3】:

    如果字符串相等,StrComp 函数将返回 0。

    http://msdn.microsoft.com/pt-br/library/9s233cfc(v=vs.90).aspx

    亲切的问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2016-05-28
      相关资源
      最近更新 更多