Extracting part of file name.

Slade

New Member
Joined
Aug 5, 2011
Messages
26
Hi

I would like to know how do i extract part of a file name or the complete file name to a cell. The path is on a server so it will be like "\\server\folder\" and then the file name "ed_scenario_validation_20110810.csv" the part with out the date "ed_scenario_validation_" always stay the same then the date "20110810" changes everyday.

Tnx
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I came across this as well but this is for the current workbook file name. The file name i am trying to import is from another source and nothing to do with the current work book. just need the file name in a cell to compare with other ones.
 
Upvote 0
I am trying to use Application.Filesearch but read just now that it is not available in excel 2007. The code i tried was as follow:

Code:
Function FileName()
With Application.FileSearch
   .NewSearch
   .LookIn = "C:\"
   .FileName = "dir*.txt"
   .Execute

   For i = 1 To .FoundFiles.Count
       Debug.Print .FoundFiles(i)
   Next

End With
End Function

and then what i had in the cell was: =IF(B10=1,Filename,"Good")

What code can i use now to replace the Filesearch?
 
Upvote 0
I have done this but still does not work.

Code:
Function FileName()
Dim oWbk As Workbook
Dim sFil As String
Dim sPath As String

sPath = "C:\" 'location of files
ChDir sPath
sFil = Dir("*.txt") 'change or add formats
Do While sFil <> "" 'will start LOOP until all files in folder sPath have been looped through Set oWbk = Workbooks.Open(sPath & "\" & sFil) 'opens the file

' do something
'oWbk.Close True 'close the workbook, saving changes
sFil = Dir
Loop ' End of LOOP
'End Sub

End Function
 
Upvote 0
Ok wait i got it to work with this.

Code:
Function SitFileName()
  Dim oWbk As Workbook
  Dim sFil As String
  Dim sPath As String

  sPath = "C:\" 'location of files
  ChDir sPath
  sFil = Dir("dir*.txt") 'change or add formats
  Do While sFil <> "" 
  Sheet1.Range("A12") = sFil
  sFil = Dir
  Loop ' End of LOOP

End Function

But how do i run this function as a formula in a call. here is what i tried: =IF(B10=1,SitFileName,"Good")
But it does not work. If i run it out of the vb script directly it does work.
 
Upvote 0
Do you mean that you put the function in a cell?

You should then assign the result of the formula to your formula name, like in:

Rich (BB code):
Function SitFileName()
  Dim oWbk As Workbook
  Dim sFil As String
  Dim sPath As String

  sPath = "C:\" 'location of files
  ChDir sPath
  sFil = Dir("dir*.txt") 'change or add formats
  Do While sFil <> "" 
  SitFileName = sFil
  sFil = Dir
  Loop ' End of LOOP

End Function

This is how it should be syntax-wise, but I am not sure this is what you mean for YOUR function. Now, SitFileName will receive the name of the last file in the loop.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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