Retrieving Variable dates from Filenames

Bobbo421

New Member
Joined
Aug 15, 2019
Messages
2
Hello all,

Long time lurker, first time user....

So I am trying to retrieve dates from a lot of filenames. The dates and file names can be variable and I am at a loss... Ive been messing around with right() mid() Left() split(), but no luck... Here are a few examples for file names.

text - 12 jun 19.pdf (this is the standard for years, but people make mistakes with the naming convention)
text - 12 jun 19 .pdf
text - 12 jun 19.pdf
text - 12 june 19.pdf
text 12 jun 19.pdf
text - 12 jun 19 1.pdf
text - 12 jun 19 A.pdf

I know i might not be able to hit all of them, but id like to hit most of them. Let me know what you guys think.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hello Bobbo421,

Where do these file names come from?

Will they always be PDF files?
 
Upvote 0
Is the date always in the format dd mmm(m) yy?
 
Upvote 0
The file names are locally generated. I'm going through each folder/subfolder with a criteria of "Test*.pdf"

Forgot to add that sometimes there's yyyy variable in the date as well.
 
Upvote 0
Hello Bobbo421,

Add a new VBA module to your workbook. Copy the code below and paste it into the module. You can the use this like an Excel formula, i.e. = ExtractDate(A2)

Code:
Global RegExp As Object


Function ExtractDate(ByVal TextDate As String) As Variant


    Dim RegExp  As Object
    
        Application.Volatile
        
        If RegExp Is Nothing Then
            Set RegExp = CreateObject("VBScript.RegExp")
            RegExp.IgnoreCase = True
            RegExp.Pattern = "Text\s*\-?\s*(\d{1,2})\s+([a-z]{3,9})\s+(\d{2,4}).*"
        End If
        
        If RegExp.Test(TextDate) Then
            ExtractDate = RegExp.Replace(TextDate, "$1-$2-$3")
        End If
        
End Function

How to add a new VBA Module to your workbook

  • Copy the macro with Ctrl+C.
  • Open the workbook and use Alt+F11 to open the VB Editor.
  • Use ALT+I to display the Insert Menu.
  • Press the M key to add a new Module.
  • Paste the macro into the Module with Ctrl+V.
  • Save the macro using Ctrl+S
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,202
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top