Don,
I see how that works for you. If you wanted to, though, you could clean
it
up by extracting the file name in VBA and then putting it into the cell
you
need, in stead of putting it in empty cell and parsing it in XL.
Something like this should work:
Sub FindFilename()
Dim LastCol As Long
Dim MyStr As String
Dim pstrFileName as string
'Stores the file selected (including path) in a string variable
pstrFileName = Application.GetOpenFilename
'Should trim off the whole path leaving only the file
'name and extension and then stores it in the same string variable.
pstrFileName = Right(pstrFileName, InStrRev(pstrFileName,"\"))
'I Think that should work, but if it is 1 character off, add/subtract
'1 from the InStrRev() function inside the right function (put a "+1"
'or a "-1" between the closing paren. of InStrRev() and the closing
'paren. of Right()). Like so:
'pstrFileName = Right(pstrFileName, InStrRev(pstrFileName,"\") + 1)
'pstrFileName = Right(pstrFileName, InStrRev(pstrFileName,"\") - 1)
'Make sure you check to see what is being stored in the variable with
a...
'Debug.Print pstrFileName
'...or a...
'MsgBox pstrFileName
'Trims off the extention from the file name (if that extension is a
period/decimal point
'with a 3 character extention)
pstrFileName = Left(pstrFileName,Len(pstrFileName)-4)
'if you might have extensions of varying lengths, then you could use
'InStrRev() with Left() to look for the period/decimal point.
'Puts the file name in the exact location you need it in
Workbooks("[your workbook name]").Worksheets("[desired sheet
neme]").Range("[desired cell address]") = pstrFileName
'Any other code necessary:
...
...
...
End Sub
(this has not been tested. I typed this in "freehand" into this post.
Did
not copy/paste from the VBE. Typos are possible...hopefully you are
using
"Option Explicit" so any would be caught)
HTH,
Conan
Don said:
OK...this threads done, I think....figured it out, cleaned it up a bit,
added
a couple of safeguards and here's the final product.....and amazingly,
IT
WORK!!...LOL
Dim LastCol As Long
Dim MyStr As String
Sub FindFilename()
On Error Resume Next
' clear contents of rows 1 through 5
Rows("1:5").ClearContents
Sheet1.Range("A1").Select
ActiveCell = Application.GetOpenFilename
' Use text to columns to seperate path into sub-folders
Selection.TextToColumns Destination:=Range("A1"),
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False,
Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True,
OtherChar _
:="\", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1),
Array(4, 1), Array(5, _
1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
' LastCol = Last column in row 1 that has data in it.
LastCol = ActiveSheet.UsedRange.Columns.Count
MyStr = ActiveSheet.Cells(1, LastCol)
' Cut the extension off the file name
Range("A2") = Left(MyStr, Len(MyStr) - 4)
Columns("A:A").EntireColumn.AutoFit
MsgBox Range("A2")
' Then I can move contents of A2 to the sheet and/or cell required
End Sub
Ya'll have a great day now.....and thanks again,
Don
:
Don,
Do you only need one file returned? How about
application.getopenfilename?
Activecell = application.GetOpenFilename
But, if I remember correctly, that will return the the whole path and
filename. You could put code in there to trim out the path and return
just
the file name.
Do you need more than one file name? Lookup GetOpenFilename in VBA
Help.
There is a "MultiSelect" argument that will allow you to select more
than
one file. But once again, it will return one string with files
(including
paths) separated by commas (I think). You could then have a string
array
and separate each file name into an element of the string array,
trimming
out path info in the process.
HTH,
Conan
This might turn out to be a double post and if so, I
apologize...something
went wrong in attempting to post this the first time.
I'd like to, using VBA in Excel 2003, open a browser window and
locate
a
file, then highlight that file and have the file name entered into a
WS.
I have a code that opens a browser window and also record all the
file
names
in a particular folder but will not allow me to selectively choose
which
file
name I want recorded.
Again I apologize if this hits the board twice but something went
wrong
on
the first try......TIA,
Don