-
Hi there, welcome to the board!
Take a look at this code...
[vba]Private Sub YourCommandButton_Click()
Dim vName As Variant
Dim sName As String
Dim sPath As String
Dim sFilter As String
sFilter = "Excel File's (*.xlsm), *.xlsm"
vName = Application.GetSaveAsFilename(, sFilter)
If TypeName(vName) = "Boolean" Then
'user pressed cancel
Else
sName = Right(vName, VBA.Len(vName) - VBA.InStrRev(vName, Application.PathSeparator))
sPath = VBA.Left(vName, VBA.Len(vName) - VBA.Len(sName))
Me.YourTextBox.Value = sPath & sName
End If
End Sub[/vba]
Some things to note about this code. The routine is tied to the userform object named "YourCommandButton". Change as desired. The filter is fully customizable, it's just important you leave the comma between the file extensions in the parenthesis and those that follow - adding other file extensions can be done but must be semi-colon separated and done in unison. For example if you wanted to add xlsb file formats the filter would then become "Excel File's (*.xlsm; *.xlsb), *.xlsm; *.xlsb". If, however, you have files which are read-only, you'll get an error generated.
You will also notice I have two variables set to grab the file name and another to grab the file path. These would be useful if you wanted to open the file, as you should first check if the file is open (using the name), and if it isn't open then go ahead and open it (using the path and name).
Edit: Also, the text box in the code I named "YourTextBox", you would need to change that to the name of your textbox.
HTH
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules