【问题标题】:VBA compare string ( check if string one contains string 2)VBA 比较字符串(检查字符串 1 是否包含字符串 2)
【发布时间】:2015-12-08 09:26:50
【问题描述】:

我在比较两个字符串时遇到问题。该方法似乎无法以某种方式工作。我尝试了以下功能:

  1. 方法:

    If StrComp(logic_string, RSTA_ISIN_clean) Then
        rng.Offset(0, 16) = "OK(ISIN in RSTA)"
        rng.Offset(0, 16).Interior.Color = 5296274
    Else
        rng.Offset(0, 16) = "NG(ISIN not RSTA or check RSTA)"
        rng.Offset(0, 16).Interior.Color = 255
    End If
    
  2. 方法:

     If InStr(1, RSTA_ISIN_clean, logic_string, vbTextCompare) Then
         rng.Offset(0, 16) = "OK(ISIN in RSTA)"
         rng.Offset(0, 16).Interior.Color = 5296274
     Else
         rng.Offset(0, 16) = "NG(ISIN not RSTA or check RSTA)"
         rng.Offset(0, 16).Interior.Color = 255
     End If
    

在 logic_string 中我的值为“FR0012915700”,在 RSTA_ISIN 中我的值为 =“Old ISIN: FR0012915700”

我要做的就是检查 RSTA_ISIN 是否在 logic_string 中,如果是,我想在单元格中写入 OK。 (试图在这里获取一个 contains 方法)

可能是 logic_string 有问题,因为它有时会给我空格 -> 所以 logic_string 在调试模式“FR0004052561”下看起来像这样 -> 我尝试使用 Trim 修剪空格,但这也不起作用。

我也尝试过 InStr 函数,但也没有用

有人可以帮忙吗

这是我在调试模式下得到的:

【问题讨论】:

  • 在我的系统上尝试代码时,我得到了与函数匹配的结果,并且 TRIM 看起来也可以正常工作。能否告知 RSTA_ISIN_clean 和 logic_String 的值是如何设置的?
  • 代码对我来说也很好
  • 嗨,很抱歉,我想检查 RSTA_ISIN 是否在 logic_string 中,或者换句话说,logic_string 是否包含 RSTA_ISIN

标签: string excel vba compare string-comparison


【解决方案1】:

在我看来,您根本不想使用StrCmp function;听起来您的 find-string-within-another-string 应该由InStr function 处理。请单击这些链接并阅读 MSDN 文档以做出决定。

Dim logic_string as String, RSTA_ISIN_clean as String

logic_string = "FR0012915700"
RSTA_ISIN_clean = " Old ISIN: FR0012915700"

If CBool(InStr(1, RSTA_ISIN_clean, logic_string, vbTextCompare)) Then
    rng.Offset(0, 16) = "OK(ISIN in RSTA)"
    rng.Offset(0, 16).Interior.Color = 5296274
Else
    rng.Offset(0, 16) = "NG(ISIN not RSTA or check RSTA)"
    rng.Offset(0, 16).Interior.Color = 255
End If

【讨论】:

    【解决方案2】:

    请检查您的变量。 “RSTA_ISIN_clean”或“RSTA_ISIN”作为变量

    Sub test1()
    
    Dim RSTA_ISIN_clean As String
    Dim logic_string As String
    Dim Rng As Range
    
    
    Set Rng = ActiveSheet.Cells(1, 30)
    
    logic_string = "FR0012915700"
    
    RSTA_ISIN_clean = "Old ISIN: FR0012915700"
    
    logic_string = Trim(logic_string)
    RSTA_ISIN_clean = Trim(RSTA_ISIN_clean)
    
    
    If StrComp(logic_string, RSTA_ISIN_clean) Then
        Rng.Offset(0, 16) = "OK(ISIN in RSTA)"
        Rng.Offset(0, 16).Interior.Color = 5296274
    Else
        Rng.Offset(0, 16) = "NG(ISIN not RSTA or check RSTA)"
        Rng.Offset(0, 16).Interior.Color = 255
    End If
    
    
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      尝试使用 Like 运算符:

      Dim logic_string As String, RSTA_ISIN_clean As String
      
        logic_string = " FR0012915700"
        RSTA_ISIN_clean = " Old ISIN: FR0012915700"
      
      
       If RSTA_ISIN_clean Like "*" & Trim(logic_string) & "*" Then
          Rng.Offset(0, 16) = "OK(ISIN in RSTA)"
          Rng.Offset(0, 16).Interior.Color = 5296274
       Else
          Rng.Offset(0, 16) = "NG(ISIN not RSTA or check RSTA)"
          Rng.Offset(0, 16).Interior.Color = 255
       End If`
      

      【讨论】:

        猜你喜欢
        • 2011-11-09
        • 2013-05-18
        • 1970-01-01
        • 2021-12-20
        • 2014-09-17
        • 2012-05-16
        • 1970-01-01
        • 2013-10-26
        • 1970-01-01
        相关资源
        最近更新 更多