Extract date from file name and return in a cell

rwm

New Member
Joined
Jun 6, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a file named "Project View 2024 02 05" where the integers are the date in the format "YYYY MM DD". I need a VBA script for a user defined function that when called, extracts the date from the filename and places it into the cell in date format. In this case, when calling the function, the cell shows "2/5/2024". I need the date to update as the filename updates to a different date as well.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Here's a VBA function as requested:
VBA Code:
Function FileDate() As Date
    Dim lngPos As Long
    Dim strDate As String
    lngPos = InStrRev(ThisWorkbook.Name, ".xls")
    strDate = Left(ThisWorkbook.Name, lngPos - 1)
    strDate = Right(strDate, 10)
    FileDate = CDate(strDate)
End Function
and here's a formula to do the same
Excel Formula:
=DATEVALUE(SUBSTITUTE(RIGHT(TEXTBEFORE(TEXTAFTER(CELL("filename"),"["),".xls"),10)," ","/"))
 
Upvote 0
Here's a VBA function as requested:
VBA Code:
Function FileDate() As Date
    Dim lngPos As Long
    Dim strDate As String
    lngPos = InStrRev(ThisWorkbook.Name, ".xls")
    strDate = Left(ThisWorkbook.Name, lngPos - 1)
    strDate = Right(strDate, 10)
    FileDate = CDate(strDate)
End Function
and here's a formula to do the same
Excel Formula:
=DATEVALUE(SUBSTITUTE(RIGHT(TEXTBEFORE(TEXTAFTER(CELL("filename"),"["),".xls"),10)," ","/"))
The formula is perfect thank you so much!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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