VBA Extracting Date from String

tabletopjoe

New Member
Joined
Jan 10, 2018
Messages
6
Hello, I am creating a macro that regularizes file names and I'm having difficulty assigning the dates within filenames to a variable. The file names look something like this:

"Grants Report S-17, First Response, dated 3-14-2015"

Alternatively they might look like this:

"3-14-2015 1st Response from Mike to Grants Report S-17"

And so forth. The only piece I'm missing is how to dynamically extract dates from the filenames. Their format varies a bit, but I can work around those variations and I am just looking for ways to identify MMDDYYYY out of any particular string and assign that date to a variable. The resulting DateVariable is concatenated along with the rest of the filename's pieces like this:

Code:
ResultName = ReportNum & ResponseNum & " " & ResponseSource & " " & DateVariable
Any tips & tricks are most appreciated!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,493
Office Version
365
Platform
Windows
Welcome to the Board!

So, will the date always appear at the very beginning or the very end?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
Is the date always in the same format?
 

Leith Ross

Well-known Member
Joined
Mar 17, 2008
Messages
1,869
Office Version
2010, 2007
Platform
Windows
Hello tabletopjoe,

This VBA macro will return the first occurrence of date string in the format you have shown from a string of text.

Code:
Function ParseDate(ByVal Text As String) As Variant


    Dim Match  As Object
    Dim RegExp As Object
    
        Set RegExp = CreateObject("VBScript.RegExp")
        
        RegExp.Pattern = "(\d{1,2}\-\d{1,2}\-\d{4})"
        Set Match = RegExp.Execute(Text)
        
        If Match.Count > 0 Then ParseDate = Match(0).SubMatches(0)
        
End Function
 

tabletopjoe

New Member
Joined
Jan 10, 2018
Messages
6
Welcome to the Board!

So, will the date always appear at the very beginning or the very end?
Thanks Joe4! Sadly not, basically any element of the filename may occur in any position.
 

tabletopjoe

New Member
Joined
Jan 10, 2018
Messages
6
Is the date always in the same format?
Hi Norie, there is some variation with the date formats but I am assuming I can work around those variations once I have a method in place for the standard MMDDYYYY format.
 

tabletopjoe

New Member
Joined
Jan 10, 2018
Messages
6
Hello tabletopjoe,

This VBA macro will return the first occurrence of date string in the format you have shown from a string of text.

Code:
Function ParseDate(ByVal Text As String) As Variant


    Dim Match  As Object
    Dim RegExp As Object
    
        Set RegExp = CreateObject("VBScript.RegExp")
        
        RegExp.Pattern = "(\d{1,2}\-\d{1,2}\-\d{4})"
        Set Match = RegExp.Execute(Text)
        
        If Match.Count > 0 Then ParseDate = Match(0).SubMatches(0)
        
End Function
Hi Leith, thank you very much for your macro example. I will be able to dig into this tomorrow and I'll be sure to let you know if that was the solution I needed.

Best regards
 

Leith Ross

Well-known Member
Joined
Mar 17, 2008
Messages
1,869
Office Version
2010, 2007
Platform
Windows
Hello tabletopjoe,

You're welcome.
 

tabletopjoe

New Member
Joined
Jan 10, 2018
Messages
6
Hi Leith, thank you very much for your macro example. I will be able to dig into this tomorrow and I'll be sure to let you know if that was the solution I needed.

Best regards
It looks like this Regular Expression pattern matching is a very powerful toolset, and precisely what I was looking for - in this context as well as another problem I was tinkering with. Thanks again!
 

Leith Ross

Well-known Member
Joined
Mar 17, 2008
Messages
1,869
Office Version
2010, 2007
Platform
Windows
Hello tabletopjoe,

Glad I could help.
 

Forum statistics

Threads
1,082,167
Messages
5,363,528
Members
400,747
Latest member
monty_gl

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top