VBA:从字符串中的列表中查找变量

时间:2022-09-26 20:26:47

I am trying to see if a certain word (or number) from a list is in a certain string.

我试图查看列表中的某个单词(或数字)是否在某个字符串中。

For example I have the following phrase: "January 20, 2012 and 2011".
And I am trying to see if the month is in the sentence, however it doesn't matter what month is present in the sentence as long as there is a month. (So "February 20, 2012 and 2011" will pass aswell)

例如,我有以下短语:“2012年1月20日和2011年”。我试图看看月份是否在句子中,但只要有一个月,句子中的月份无关紧要。 (所以“2012年2月20日和2011年”将通过)

I was thinking about something like:

我在想的是:

Sub Run_Find()
Dim Month As String, Number, Year, Splitmonth As Variant
Dim ii As Integer

Month = "January, February, March, April, May, June, July, August, September, October, November, December"
Number = "1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 28, 29, 30, 31"
Year = "2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015"

Splitmonth = VBA.Split(Month, ",")
For ii = 0 To 12
    If VBA.InStr(1, "January 30, 2012 and 2011", Splitmonth(ii)) > 0 Then
        MsgBox "Found it!"
    Else
        MsgBox "Nop!"
    End If
Next ii

End Sub

This works. But is there an alternative? Look through a list and if any of the words in the list is present in the string, it should pass.

这很有效。但是有替代方案吗?查看列表,如果列表中的任何单词出现在字符串中,它应该通过。

Eventually I am trying to see If it contains a month, And a day (number), And a year, Then ..
Using this method it seems to get "over complicated".

最终我试图看看它是否包含一个月,一天(数字)和一年,然后..使用这种方法似乎变得“过于复杂”。

Thanks in advance, R

谢谢你,R

2 个解决方案

#1


6  

Regular expressions might be helpful here. Regular expressions are a broad topic (), but here are a couple examples along the lines of your question.

正则表达式在这里可能会有用。正则表达式是一个广泛的主题(正则表达式),但这里有几个问题的例子。

Public Sub example1()
  Dim re As Object
  Set re = CreateObject("vbscript.regexp")
  re.Pattern = "January|February|March|April|May|June|July|August|September|November|December"
  If re.test("January 30, 2012 and 2011") Then
      Debug.Print "match found"
  End If
End Sub

'=> match found

Public Sub example2()
  Dim re As Object
  Dim matches As Object, match As Object, submatch

  Set re = CreateObject("vbscript.regexp")
  re.Pattern = "(January|February|March|April|May|June|July|August|September|November|December) (\d+), (\d{4})"
  Set matches = re.Execute("January 30, 2012 and 2011")
  For Each match In matches
    Debug.Print "match: " & match
    Debug.Print "submatches: ",
    For Each submatch In match.submatches
        Debug.Print submatch,
    Next
    Debug.Print ""
  Next
End Sub

'=> match: January 30, 2012
'=> submatches:   January       30            2012          

#2


2  

Not better than regex, but here's another way using the Filter function

并不比正则表达式好,但这是使用Filter函数的另一种方式

Sub Run_Find()

    Dim sMonth As String
    Dim vaMonth As Variant
    Dim vaDate As Variant
    Dim i As Long

    sMonth = "January, February, March, April, May, June, July, August, September, October, November, December"

    vaMonth = Split(sMonth, "," & Space(1))
    vaDate = Split("July 30, 2012 and 2011", Space(1))

    For i = LBound(vaMonth) To UBound(vaMonth)
        If UBound(Filter(vaDate, vaMonth(i))) >= 0 Then
            Debug.Print vaMonth(i)
            Exit For
        End If
    Next i

End Sub

#1


6  

Regular expressions might be helpful here. Regular expressions are a broad topic (), but here are a couple examples along the lines of your question.

正则表达式在这里可能会有用。正则表达式是一个广泛的主题(正则表达式),但这里有几个问题的例子。

Public Sub example1()
  Dim re As Object
  Set re = CreateObject("vbscript.regexp")
  re.Pattern = "January|February|March|April|May|June|July|August|September|November|December"
  If re.test("January 30, 2012 and 2011") Then
      Debug.Print "match found"
  End If
End Sub

'=> match found

Public Sub example2()
  Dim re As Object
  Dim matches As Object, match As Object, submatch

  Set re = CreateObject("vbscript.regexp")
  re.Pattern = "(January|February|March|April|May|June|July|August|September|November|December) (\d+), (\d{4})"
  Set matches = re.Execute("January 30, 2012 and 2011")
  For Each match In matches
    Debug.Print "match: " & match
    Debug.Print "submatches: ",
    For Each submatch In match.submatches
        Debug.Print submatch,
    Next
    Debug.Print ""
  Next
End Sub

'=> match: January 30, 2012
'=> submatches:   January       30            2012          

#2


2  

Not better than regex, but here's another way using the Filter function

并不比正则表达式好,但这是使用Filter函数的另一种方式

Sub Run_Find()

    Dim sMonth As String
    Dim vaMonth As Variant
    Dim vaDate As Variant
    Dim i As Long

    sMonth = "January, February, March, April, May, June, July, August, September, October, November, December"

    vaMonth = Split(sMonth, "," & Space(1))
    vaDate = Split("July 30, 2012 and 2011", Space(1))

    For i = LBound(vaMonth) To UBound(vaMonth)
        If UBound(Filter(vaDate, vaMonth(i))) >= 0 Then
            Debug.Print vaMonth(i)
            Exit For
        End If
    Next i

End Sub