user2102869
user2102869

Reputation:

VBA: Find variable from list in string

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)

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

Upvotes: 3

Views: 4110

Answers (2)

Dick Kusleika
Dick Kusleika

Reputation: 33145

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

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

Upvotes: 2

A. Webb
A. Webb

Reputation: 26446

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          

Upvotes: 6

Related Questions