【问题标题】:how to check if a string contains only numeric numbers in vba如何检查字符串是否仅包含vba中的数字
【发布时间】:2014-06-04 08:09:22
【问题描述】:

我想从这样的字符串中解析出年份信息

8995 美元 2008 年 4 月 18 日本田思域混合动力车 8995 美元(Orem)图片地图汽车和卡车 - 所有者

由于我在网上检索到这个字符串,有时年份元素不在同一个地方。我这样做的方法是使用 split 函数将字符串按空格拆分,然后检查数组的每个节点是否仅包含数字。

但是,当我使用函数 IsNumeric 时,它也会返回“$8995”节点为真。

什么是检查字符串是否只包含数字、没有“$”、没有“.”、而不是其他任何东西的好方法?

或者在我的情况下,有没有更好的方法来检索年份信息?

谢谢。

【问题讨论】:

  • 您可以检查 1998 年和当前年份之间的 Isnumeric 和值...
  • 我在想如果他们只输入 06 或 07 会怎样,但我想它可以通过检查值是否在 00 到 14 之间或值是否在 60 到 99 之间来工作
  • @Sparky 但它仍然不能解决如果一辆车只有 1999 美元并且我无法分辨年份之间的价格的问题。
  • 我认为正则表达式可能是要走的路。您可能需要按可能性顺序检查多个模式,然后选择第一个匹配的模式。这可能有用:stackoverflow.com/questions/19481175/…
  • 如果它们不包含 $ 符号,您也可能会遇到困难。您可以像现在一样隔离所有数字,他们编写一些业务逻辑来“猜测”哪个是年份,哪个是价格。该业务逻辑可能是几个规则,首先是其中一个数字字段是否包含 $ 符号。很难仅从奥迪 500 中提取年份?

标签: string vba excel isnumeric


【解决方案1】:

您可以使用 RegEx 解析年份:

Public Function GetYear(someText As String) As Integer
    With CreateObject("VBScript.RegExp")
        .Global = False
        .MultiLine = False
        .IgnoreCase = True
        .Pattern = " [\d]{4} "
        If .Test(testString) Then
            GetYear = CInt(.Execute(testString)(0))
        Else
            GetYear = 9999
        End If
    End With
End Function

示例代码:

Public Const testString As String = "$8995 Apr 18 2008 Honda Civic Hybrid $8995 (Orem) pic map cars & trucks - by owner "

Public Function GetYear(someText As String) As Integer
    With CreateObject("VBScript.RegExp")
        .Global = False
        .MultiLine = False
        .IgnoreCase = True
        .Pattern = " [\d]{4} "
        If .Test(testString) Then
            GetYear = CInt(.Execute(testString)(0))
        Else
            GetYear = 9999
        End If
    End With
End Function

Sub Foo()
    Debug.Print GetYear(testString) '// "2008"
End Sub

【讨论】:

    【解决方案2】:

    这可以使用Like 运算符作为单行代码来完成

    Function StringIsDigits(ByVal s As String) As Boolean
        StringIsDigits = Len(s) And (s Like String(Len(s), "#"))
    End Function
    

    【讨论】:

      【解决方案3】:

      会不会是所有带有“年”的字符串都有看起来像日期的子字符串?如果是这种情况,您可以在字符串中循环查找看起来像日期的第一组三个,从中提取年份:

      Option Explicit
      Function FindYear(S As String) As Long
          Dim SS As Variant
          Dim sDate As String
          Dim I As Long, J As Long
      
      SS = Split(S, " ")
      For I = 0 To UBound(SS) - 2
          sDate = ""
          For J = 0 To 2
              sDate = " " & sDate & " " & SS(I + J)
          Next J
          sDate = Trim(sDate)
          If IsDate(sDate) Then
              FindYear = Year(sDate)
              Exit Function
          End If
      Next I
      End Function
      

      【讨论】:

      • 应该是For I = 0 To UBound(SS) - 2
      • @chrisneilsen 谢谢!我会改的。
      【解决方案4】:

      不使用正则表达式或一些非常复杂的逻辑,很难做到完美。

      此代码将返回纯数字子字符串,但在您的示例中,它将返回“18”和“2008”。您显然可以尝试添加更多逻辑来禁止“18”(但允许“13”或“09”等,但就像我说的那样开始变得复杂。我很乐意提供帮助,但不知道具体是什么你想要,我认为现在最好由你来决定。

      Const str$ = "$8995 Apr 18 2008 Honda Civic Hybrid $8995 (Orem) pic map cars & trucks - by owner"
      Option Explicit
      Sub FindNumericValues()
      
      Dim var() As String
      Dim numbers As Variant
      
      var = Split(str, " ")
      
      numbers = GetNumerics(var)
      
      MsgBox Join(numbers, ",")
      
      End Sub
      
      Function GetNumerics(words() As String) As Variant
      Dim tmp() As Variant
      Dim i As Integer
      Dim n As Integer
      Dim word As Variant
      Dim bNumeric As Boolean
      
      For Each word In words
          n = 0
          bNumeric = True
          Do While n < Len(word)
          n = n + 1
              If Not IsNumeric(Mid(word, n, 1)) Then
                  bNumeric = False
                  Exit Do
              End If
          Loop
          If bNumeric Then
              ReDim Preserve tmp(i)
              tmp(i) = word
              i = i + 1
          End If
      Next
      
      GetNumerics = tmp
      End Function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 1970-01-01
        相关资源
        最近更新 更多