Partial Filename Extract Not Returning Expected Results

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
701
Office Version
  1. 365
Platform
  1. Windows
I have some files that have data I'm importing into a master file. These files have variable names, with the last 8 characters being a date (e.g., 01.06.23). When I try to extract the date and put it in a range of cells, I'm getting a completely different date (12/30/1999). What adjustments to the code do I need to make so that I can populate the range with 01.06.23? Note...Ideally, the date would come over as 01/06/23. Here's what I've tried so far:

VBA Code:
'mD.Range("H" & mDNR & ":H" & mDNLR).Value = Format(Mid$(s.Name, 14, 8), "MM/DD/YY")
'mD.Range("H" & mDNR & ":H" & mDNLR).Value = Format(Mid$(s.Name, 14, 8), "@")
mD.Range("H" & mDNR & ":H" & mDNLR).Value = Mid$(s.Name, 14, 8)

VBA Code:
Dim m As Workbook, s As Workbook
Dim mD As Worksheet, sD As Worksheet
Dim FP As String, FN As String
Dim mDLR As Long, mDNR As Long, mDNLR As Long, sDLR As Long

Set m = ThisWorkbook
Set mD = m.Sheets("Data")

'Sets the Last Row and Next Row on the Data tab.
mDLR = mD.Range("A" & Rows.Count).End(xlUp).Row
mDNR = mD.Range("A" & Rows.Count).End(xlUp).Row + 1

'Opens the target Source File.

FP = "Blah Blah\"
FN = "Rob-Tom Work"
FN = Dir(FP & FN & "*.xlsx")

Set s = Workbooks.Open(FP & FN)

Set sD = s.Sheets("Accounts")

sD.Activate

'Removes filters if any exist.
If sD.AutoFilterMode Then sD.AutoFilterMode = False

'Sets the Last Row on the Source File.
sDLR = sD.Range("A" & Rows.Count).End(xlUp).Row

mDNLR = mD.Range("E" & Rows.Count).End(xlUp).Row
    
'Copies the File Date and pastes it into the Data tab.
'mD.Range("H" & mDNR & ":H" & mDNLR).Value = Format(Mid$(s.Name, 14, 8), "MM/DD/YY")
'mD.Range("H" & mDNR & ":H" & mDNLR).Value = Format(Mid$(s.Name, 14, 8), "@")
mD.Range("H" & mDNR & ":H" & mDNLR).Value = Mid$(s.Name, 14, 8)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Please post a few examples of the FULL file names, so we can see exactly what you are working with.
 
Upvote 0
Or, it might even be more to add a new line of code after this line:
VBA Code:
Set s = Workbooks.Open(FP & FN)
that looks like this:
VBA Code:
MsgBox s.Name
and tell us what it returns.

That will help ensure that the previous part of the code is working, and show us what it thinks that value is at that point in time.
 
Upvote 0
Or, it might even be more to add a new line of code after this line:
VBA Code:
Set s = Workbooks.Open(FP & FN)
that looks like this:
VBA Code:
MsgBox s.Name
and tell us what it returns.

That will help ensure that the previous part of the code is working, and show us what it thinks that value is at that point in time.
@Joe4 The msgbox returned the true file name of Rob-Tom Work 01.06.23.xlsx
 
Upvote 0
Try this for your calculation:
VBA Code:
mD.Range("H" & mDNR & ":H" & mDNLR).Value = Replace(Left(Right(s, 13), 8), ".", "/")
 
Upvote 0
Try this for your calculation:
VBA Code:
mD.Range("H" & mDNR & ":H" & mDNLR).Value = Replace(Left(Right(s, 13), 8), ".", "/")
I got the Object doesn't support this property or method error at the line you gave me.
 
Upvote 0
Whoops, it should be "s.Name", not just "s":
VBA Code:
mD.Range("H" & mDNR & ":H" & mDNLR).Value = Replace(Left(Right(s.Name, 13), 8), ".", "/")
 
Upvote 0
Whoops, it should be "s.Name", not just "s":
VBA Code:
mD.Range("H" & mDNR & ":H" & mDNLR).Value = Replace(Left(Right(s.Name, 13), 8), ".", "/")
No change. I'm still getting the 12/30/1999 date.
 
Upvote 0
Just for kicks, temporarily change it to this and see what you get:
VBA Code:
mD.Range("H" & mDNR & ":H" & mDNLR).Value = "Date is " & Replace(Left(Right(s.Name, 13), 8), ".", "/")
 
Upvote 0
Just for kicks, temporarily change it to this and see what you get:
VBA Code:
mD.Range("H" & mDNR & ":H" & mDNLR).Value = "Date is " & Replace(Left(Right(s.Name, 13), 8), ".", "/")
Literally just got the 12/30/1999. There was no "Date Is" in cells. LOL
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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