I am trying to show the path of a file I select from a dialog box, in a text box.
Sub SelectMonthlyFile()
Dim Data As Office.FileDialog
Set Data = Application.FileDialog(msoFileDialogFilePicker)
With Data
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "All Files", "*.*"
If .Show = True Then
Path = .SelectedItems(1)
End If
End With
End Sub
I show an error at Path = .SelectedItems(1).
variable is not defined
I named my cell Path and assumed that this would get the file path copied and pasted here.
I suggest you rename Data to something more meaningful. I've changed it to just FileDialog. The reason for your error is that the code has no idea what Path is. Note that I omitted the clearing of the filter, it is not needed.
Option Explicit 'always have this
Dim strPath As String 'explicitly declare your variable
Sub SelectMonthlyFile()
Dim FileDialog As Office.FileDialog
Set FileDialog = Application.FileDialog(msoFileDialogFilePicker)
With FileDialog
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "All Files", "*.*"
'was anything selected?
If .Show <> 0 Then
strPath = .SelectedItems(1)
Debug.Print strPath
End If
End With
End Sub
Related
I would like to write a program in VBA.
I would like a window to pop up when the program starts, where I can bring up the file manager with a button and the elements what are (multiple)selected here, would be placed in a textbox under each other and excel would open them all at once.
Now I finished the useform with one button and one textbox, and have a code for the button, to open the file manager and enabled the multiselect. Whats next? Anybody can help me?
Private Sub CommandButton1_Click()
Dim fldlg As FileDialog
Set fldlg = Application.FileDialog(msoFileDialogOpen)
With fldlg
.AllowMultiSelect = True
.Title = "Find"
.InitialFileName = "C:\"
.Filters.Clear
.Filters.Add "Excel", "*.xls,*.xlsx"
End With
rv = fldlg.Show
If rv Then
MsgBox fldlg.SelectedItems(1)
End If
End Sub
To open all the files (or build a string listing them all), you need to cycle through the SelectedItems collection and use each in turn:
Dim fldlg As FileDialog
Set fldlg = Application.FileDialog(msoFileDialogOpen)
Dim strItems
With fldlg
.AllowMultiSelect = True
.Title = "Find"
.InitialFileName = "C:\"
.Filters.Clear
.Filters.Add "Excel", "*.xls,*.xlsx"
rv = .Show
If rv Then
'create a string of text for output using MsgBox
For Each SelectedItem In .SelectedItems
strItems = strItems & SelectedItem & Chr(13)
Next
'display string
MsgBox strItems
'open the files
For Each SelectedItem In .SelectedItems
Workbooks.Open SelectedItem
Next
End If
End With
I use the VBA code below to open a workbook but I get a user-prompt when the workbook is already-opened by another user asking if I would like to open in read-only mode.
I'd like to open the workbook in read-only mode directly without the read-only user prompt.
Sub Report()
Dim fd As FileDialog
Dim Filechosen As Boolean
Dim Savebutton As Boolean
Dim sh As Worksheet
Dim book As Workbook
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
fd.Filters.Add "Old Excel Files", "*.xls"
fd.Filters.Add "New Excel Files", "*.xlsx"
fd.Filters.Add "macro Excel Files", "*.xlsm"
fd.Filters.Add "any Excel Files", "*.xl*"
fd.FilterIndex = 4
fd.AllowMultiSelect = False
fd.InitialFileName = "https:sharepoint address/BFs/"
Filechosen = fd.Show
If Not Filechosen Then
MsgBox " No File Selected"
Exit Sub
End If
fd.Execute
For Each sh In Worksheets: sh.Visible = True
Next sh
End Sub
When you are giving the command to open the file, it has an optional command to open it in readonly
Just mention it as Readonly:=True
It will not prompt the user
Let me know if you need further clarification
You can open the workbook yourself using the VBA workbooks.open method once you get the filename from the dialog.
Replace in your code as needed. You'll get the idea
Dim xlFileName As String
Dim wb as Workbook
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
If .Show Then
xlFileName = .SelectedItems(1)
Else
MsgBox " No File Selected"
Exit Sub
End If
End With
wb.Workbooks.Open (xlFileName), ReadOnly:=True
You can then copy the worksheets in that workbook from the wb.worksheets collection.
I have a function in a macro that allows me to select a file using inputbox, or is it possible to always start showing files at the same time, so that I don't have to look in folders. For example, when the selectFile window appears, the folder should be \banksl\dfs\BH\BPR\
strPath1 = selectFile
If strPath1 = "" Then Exit Function
Set bilans = Workbooks.Open(strPath1, False, True)
Private Function selectFile1()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.InitialFileName = ActiveWorkbook.path
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel", "*.xlsm"
If .Show = True Then selectFile = .SelectedItems(1)
End With
I am trying to give users the option to 'set' a default folder location, this would only be used to prefill further userform text boxes to save time having to go through the msoFileDialogFolderPicker
I did use chdir, but that was written into the vba script, this location will differ from user to user and should only need to be set once upon addon installation
This is the code for the master folder selector:
Private Sub cmdfoldsel_Click()
On Error GoTo err
Dim fileExplorer As FileDialog
Set fileExplorer = Application.FileDialog(msoFileDialogFolderPicker)
'To allow or disable to multi select
fileExplorer.AllowMultiSelect = False
With fileExplorer
If .Show = -1 Then 'Any folder is selected
tbmasterloc.text = .SelectedItems.Item(1)
Else ' else dialog is cancelled
MsgBox "You have cancelled the dialogue"
[folderPath] = "" ' when cancelled set blank as file path.
End If
End With
err:
Exit Sub
End Sub
It is letting me choose the folder, but obviously the text disappears when I close ( I was using a close button with unload me but i thought that was deleting the text
An example of the second userform which will call that folder location is here:
Private Sub UserForm_Initialize()
copyfromtb.Value = mfs.tbmasterloc.text
End Sub
Private Sub copyfromcmd_Click()
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
'.InitialFileName = Application.GetSaveAsFilename()
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
copyfromtb.Value = sItem
Set fldr = Nothing
End Sub
I have the follow macro for a button click. When I run it, I hit a snag at the Label caption line with an 'object required' error. The names of the objects are correct.
Sub Button1_Click()
Dim objFileDialog As Object
Dim objSelectedFile As Variant
Set objFileDialog = Application.FileDialog(3)
With objFileDialog
.ButtonName = "Select"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel File", "*.*", 1
.Title = "Select Input file"
.Show
For Each objSelectedFile In .SelectedItems
Label2.Caption = objSelectedFile
Next
End With
End Sub