I use the following to open a dialog box to select files.
fnameListCurr = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv),*.csv", Title:="Choose CSV files to import", MultiSelect:=True)
However, when I do this, nothing is selected by default. Is it possible to have the file selector dialog open with certain files or all files that meet the filter criteria already selected?
If you need to set an initial Fine name, use FileDialog instead. Try the next code, please:
Sub filedialogSelectSomething()
Dim fileName
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Title = "Choose CSV files to import"
.InitialFileName = "Your Path\Test*.csv"
.Filters.Add "CSV Files", "*.csv", 1
If .Show = -1 Then
fileName = .SelectedItems(1)
'or use them all...
End If
End With
Debug.Print fileName
End Sub
Look here to better understand how InitialFileName overrides the file filter settings...
Related
I have a macro through which I can select a folder by double clicking. The path of this folder is later used to open some files (in the folder).
The code for the macro to select the folder is:
Sub GetFolder()
Dim folderselected As String
Set myFolder = Application.FileDialog(msoFileDialogFolderPicker)
With myFolder
.Title = "Choose Folder"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
folderselected = .SelectedItems(1)
End With
DataPath = folderselected & "\"
End Sub
Now when I click the folder, the explorer interface shows ."No items match your search", although there are files in the folder.
Is there a way to extend this code, where I can select the folder, retrieve the datapath and also get to view the files in the explorer display (but of course not select it) ??
I need to capture the filename and file path of where Project exports to Excel,
I use the code;
FileSaveAs FormatID:="MSProject.ACE", Map:="Map 1"
but can't work out how to capture where the user has exported the file to (the excel version)
Thanks
Ronan
Try to use your own dialog box and then save the file:
Dim SavePath As Variant
With Application.FileDialog(msoFileDialogSaveAs)
.Title = "Please select location to save file"
If .Show = True Then
SavePath = .SelectedItems(1)
Else
SavePath = False
End If
End With
FileSaveAs Name:= SavePath, FormatID:="MSProject.ACE", Map:="Map 1"
I need the browse button in input box to find file - VB A - EXCEL Macro][1]
need to find the folder path via browse button instead of typing in input box
is it possible?
|-------------------|
|-------------------| Browse
by clicking a cell it should ask for file browse.
should not be edited manually. i mean , i want to lock the particular cell locked. and only able to edit via macro.
Alternately:
Sub tgr()
Dim strFilePath As String
strFilePath = Application.GetOpenFilename
If strFilePath = "False" Then Exit Sub 'Pressed cancel
MsgBox strFilePath
End Sub
You can use this to find a file. Modify the filter if you need to. the variable fldr will have your data. Then you can set your textbox to that value.
Sub File_Picker()
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "Text", "*.txt", 1
.InitialFileName = ActiveWorkbook.Path & "\"
.Show
If .SelectedItems.Count = 0 Then GoTo 1
fldr = .SelectedItems(1)
End With
End Sub
or:
Sub Folder_Picker()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ActiveWorkbook.Path & "\"
.Show
If .SelectedItems.Count = 0 Then GoTo 1
fldr = .SelectedItems(1)
End With
End Sub
I have more helpful pieces of code like this at My GitHub
I'm using the next code in order to select a folder from a certain path and import all the files inside it:
Function GetFolder()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.Title = "Select Excel Workbook(s) Folder"
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
GetFolder = vrtSelectedItem
Next vrtSelectedItem
Else
End If
End With
Set fd = Nothing
End Function
When the Folder Picker window opens it start on the desktop. Is there a way to make it go to a specific path upon opening? or open where the excel file itself is located?
You would update the InitialFileName property, and you could set it to use the ActiveWorkbook.Path
You'll need to make sure that you include the ending slash, or it will only display the previous folder instead of the folder you want.
Also, there is no reason to loop through the .SelectedItems collection because the FolderPicker FileDialog doesn't support mutliple selections.
In summary, I think this is the code you're looking for:
Function GetFolder()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ActiveWorkbook.Path & Application.PathSeparator
.Title = "Select Excel Workbook(s) Folder"
If .Show = True Then
GetFolder = .SelectedItems(1)
Else
GetFolder = False
End If
End With
End Function
add a line like this before .Show:
fd.InitialFileName = "c:\whateverInitialDirectory"
I want to write a macro that will open 30 excel files which all have the same structure.
The macro should do operation on all files, and take the results from every file and put it in another excel file.That means: all Results (Values) will be copied into the destination file.
How do I write VBA code to open multiple files?
How do I take the results from every file and place them in my destination.xls file?
Try checking out FileSearch.LookIn Property
modify that example to something similar. (This would require that all your 30 files are in one folder though)
Dim WrkBook as Workbook
Set fs = Application.FileSearch
With fs
.SearchSubFolders = False
.LookIn = "C:\My Documents" 'Path of folder to search
.FileName = "*.xls" 'Limit to excel files
If .Execute > 0 Then
Debug.Print "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
WrkBook = Workbooks.Open(Filename:=.FoundFiles(i))
WrkBook.Worksheets(1).Select
ThisWorkbook.Worksheets(1).Cells(DestinationRange) = WrkBook.Worksheets(1).Cells(SourceRange).Value
Next i
Else
Debug.print "There were no files found."
End If
End With
You could also try
Workbooks("Destination.xls").Worksheets("Sheet1").Cells(DestinationRange) = WrkBook.Worksheets(1).Cells(SourceRange).Value
I think you're after a solution, that requires the user to choose the files to open and process?
Try this...
Option Explicit
Sub opening_multiple_file()
Dim i As Integer
'Opening File dialog box
With Application.FileDialog(msoFileDialogFilePicker)
'Enabling multiple files select
.AllowMultiSelect = True
.Filters.Clear
'Only Excel files can be selected
.Filters.Add "Excel Files", "*.xls*"
If .Show = True Then
For i = 1 To .SelectedItems.Count
'Opening selected file
Workbooks.Open .SelectedItems(i)
'etc do other things with it
Next i
End If
End With
End Sub