VBA FileDialog compiled function / code below. Should you have any problem using the code Please let me know by commenting or emailing me the issue.
SeletedFile: pass 2 arguments PopUpDirNane (enter the default folder to show up when the code runs so that users can select the file) and msgstr (the message to show up in the dialog to guide the user to select the file). Please refer to test_SeletedFile_funt1 and test_SeletedFile_funt2 for sample subroutine on how to call this function. You can also add filters to show only specified files type by changing the code @ .Filters.Add "Templates", "*.*" line. For example if you want to show only excel files change .Filters.Add "Templates", "*.*" to .Filters.Add "Templates", "*.xls*".
SeletedFile: pass 2 arguments PopUpDirNane (enter the default folder to show up when the code runs so that users can select the file) and msgstr (the message to show up in the dialog to guide the user to select the file). Please refer to test_SeletedFile_funt1 and test_SeletedFile_funt2 for sample subroutine on how to call this function. You can also add filters to show only specified files type by changing the code @ .Filters.Add "Templates", "*.*" line. For example if you want to show only excel files change .Filters.Add "Templates", "*.*" to .Filters.Add "Templates", "*.xls*".
Option Explicit
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on this code.
'Feel free to update the code as per your need and also share with your friends.
'Channel: Youtube.com/vbaa2z
'Download free codes from http://vbaa2z.blogspot.com
'Autor: L Pamai (vbaa2z.team@gmail.com)
'-----------------------------
Public Function SeletedFile(PopUpDirNane As String, msgstr As String) As String
'this is function to browse the file var [PopUpDirNane] folder;
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFilePicker)
With fldr
.Title = msgstr
.AllowMultiSelect = False
.InitialFileName = PopUpDirNane
.Filters.Clear
.Filters.Add "Templates", "*.*"
.Filters.Add "All files", "*.*"
.FilterIndex = 1
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
SeletedFile = sItem
Set fldr = Nothing
End Function
Sub test_SeletedFile_funt()
MsgBox "Selected file is & " & SeletedFile("C:\", "Please select the file")
End Sub
Sub test_SeletedFile_funt2()
Dim xfile As String
Dim wb As Workbook
xfile = SeletedFile("C:\", "Please select the excel file to import data from ")
Set wb = Workbooks.Open(xfile)
'.......
End Sub
No comments:
Post a Comment
Note: only a member of this blog may post a comment.