Background:
I have written some VB to search through a directory for a file (the date of the file will be variable, hence the wildcard) the code will then open the file, copy a list of names to another spreadsheet, store the names in an array so I can return their relative position in the other spreadsheet - pos
, I need to do this as the next part of the code writes a link to that spreadsheet.
My filename search code is as follows:
Dim lcFound As String
lcFound = Dir("C:\Users\KDelaney\Desktop\*XYZ Select XYZ List.xlsm")
If lcFound <> "" Then
Workbooks.Open Filename:=lcFound
End If
Not going to add all the code as I don't think it's relative to the question. The writing a link formula to each cell in a f loop is as below, the below code was fine before I was using the file search, as I was testing it with the full file name and path:
Cells(f, 4).Formula = "=" & "'" & lcPath & "[" & lcFile & "]" & "Template" & "'!" & "AB" & pos
Question:
Basically the question is what would be the best way to convert C:\Users\KDelaney\Desktop\*XYZ Select XYZ List.xlsm
into lcPath and lcFile, or is there a way round separating the strings? (Have briefly tested it unseparated but it seems like the square brackets around the filename is required).
I'm guessing the best course of action would be using InStr
function? to return the position of the end of the file path? e.g. I know the filepath will always end with "Desktop\
" so if I use InStr
to return that position I will then know the filepath is between 1 and the returned value of InStr
. However this is where I have stalled, not sure where to go from here, in terms of separating the two strings. I also know the start of the file name will always begin with a number and end with the file extension. I am probably making a meal of this problem and someone will come back with a very simple solution (hopefully)
Any help would be appreciated. Apologies if the question was too in depth just wanted to give you the full picture. Thanks.