Hi all,
I'm looking for a way for a userForm to allow a user to browse to a file and then have excel save the selected file to a particular path.
I've puzzled out the following so far:
My problem is thus:Sub getAndSaveFile() Dim userFilePath As String Dim libraryFilePath As String Dim typeSelect As Integer userFilePath = Application.GetOpenFilename If typeSelect = 1 Then libraryFilePath = "H:\TEMP\Lib1\" ElseIf typeSelect = 2 Then libraryFilePath = "H:\TEMP\Lib2\" ElseIf typeSelect = 3 Then libraryFilePath = "H:\TEMP\Lib3\" Else libraryFilePath = "H:\TEMP\" End If 'insert code to save the selected file to the selected file path FileCopy userFilePath, libraryFilePath end sub
The Application.GetOpenFilename method gets the full file path of the selected file, which is great for the FileCopy userFilePath variable.
But how do I truncate the filePath string to include only the file name so I can put it in the expression for the copy destination, i.e.:
FileCopy userFilePath, libraryFilePath & fileName
Must I directly modify the string to remove everything (and including) the right-most "/" character in the userFilePath string, or is there a way to modify the Application.GetOpenFilename method to return just the selected file name and not the full file path?
Thanks!
-o
Last edited by Ouka; 06-28-2011 at 01:54 PM.
Hi Ouka,
Read http://www.ozgrid.com/VBA/GetExcelFileNameFromPath.htm
One test is worth a thousand opinions.
Click the * below to say thanks.
Ah, so string truncation it is.
Was hoping for a pre-existing method like the one that is available for returning just the file name of the currently open excel file.
eg:
ThisWorkbook.Name
vs
ThisWorkbook.FullName
Thanks for reply.
Hello Ouka,
here is a much easier way using the Dir Statement.
Sub getAndSaveFile() Dim FileName As String Dim userFilePath As String Dim libraryFilePath As String Dim typeSelect As Integer userFilePath = Application.GetOpenFilename If userFilePath = "False" Then Exit Sub If typeSelect = 1 Then libraryFilePath = "H:\TEMP\Lib1\" ElseIf typeSelect = 2 Then libraryFilePath = "H:\TEMP\Lib2\" ElseIf typeSelect = 3 Then libraryFilePath = "H:\TEMP\Lib3\" Else libraryFilePath = "H:\TEMP\" End If 'insert code to save the selected file to the selected file path FileName = Dir(userFilePath) FileCopy userFilePath, libraryFilePath & FileName End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
perfect, that was exactly what I was looking for. Both methods work but I'll go with the simple one!
Thanks!
Does
help any?ThisWorkbook.Path
One test is worth a thousand opinions.
Click the * below to say thanks.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks