Accessing the text of a filename

Jacek

New Member
Joined
Jul 11, 2011
Messages
19
Hello everyone,

I come to you for some guidance regarding a task that I have been assigned to do. What I am trying to do is have my macro simply take the filename and generate it in a specific cell.

Specifically, the filenames are in the following format: "20100512_xxx.dat"

What I would like to do is write "2010-05-12" into a specific cell.

Thank you in advance.

Jacek
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
How can you get the filename? Does the user choose the filename? Is the file opened?

Yes, my macro requests the user to choose the file, and then I have the macro actually open the file. It is a .dat file.

My macro uses the Application.GetOpenFileName procedure and assigns a variable to the file.

I then use Workbooks.OpenText Filename:= "the variable name"

The .dat file is a CSV file...if that makes sense.

Hope this info is sufficient.

thanks.
 
Upvote 0
Ok so I have just read into the MID function...and the Cell("filename") utility. The only problem is it shows the entire filepath. What about having one cell show the filename alone, then I can use the MID function to specify what characters I want...then I have to figure out how to add the hyphens.
 
Upvote 0
If the format of the filename is consistent then just pick it apart from the right side. This should be able to be modified to meet your needs.

Code:
Sub test()
    Var = "C:\temp\test\123\20100512_xxx.dat"
    Debug.Print Mid(Var, Len(Var) - 15, 4) & "-" & Mid(Var, Len(Var) - 11, 2) & "-" & Mid(Var, Len(Var) - 9, 2)
End Sub
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,553
Members
452,928
Latest member
101blockchains

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