系列文章目录
文章目录
前言
学会长大,学会承受!!
一、vba打开文件对话框
只需要修改初始路径即可!!
.InitialFileName = ThisWorkbook.Path & "\"
1.对话框路径为指定路径
代码如下(路径为当前文件所在路径):
Sub 汇总()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
With fd
.Filters.Clear
.Filters.Add "CSV", "*.CSV", 1
.InitialFileName = ThisWorkbook.Path & "\"
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
If vrtSelectedItem <> "" Then
InputData (vrtSelectedItem)
End If
Exit For
Next vrtSelectedItem
Else
End If
End With
Set fd = Nothing
End Sub
代码如下(路径为自定义路径):
Sub 汇总()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
With fd
.Filters.Clear
.Filters.Add "CSV", "*.CSV", 1
.InitialFileName = "\\192.168.177.27\研发" & "\"
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
If vrtSelectedItem <> "" Then
InputData (vrtSelectedItem)
End If
Exit For
Next vrtSelectedItem
Else
End If
End With
Set fd = Nothing
End Sub
2.对话框路径为上次操作使用路径
代码如下(最简略):
Sub OpenFile()
fileToOpen = Application _
.GetOpenFilename("Dat Files (*.DAT), *.dat, Text Files (*.txt), *.txt")
End Sub
代码如下(实例应用,删除路径也可以):
Sub 汇总()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
With fd
.Filters.Clear
.InitialFileName = "Dat Files (*.CSV), *.csv, Text Files (*.txt), *.txt"
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
If vrtSelectedItem <> "" Then
InputData (vrtSelectedItem)
End If
Exit For
Next vrtSelectedItem
Else
End If
End With
Set fd = Nothing
End Sub
二、参考源码
1.单个文件
代码如下(示例):
Sub SelectFile()
'选择单一文件
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False '单选择
.Filters.Clear '清除文件过滤器
.Filters.Add "Excel Files", "*.xls;*.xlw"
.Filters.Add "All Files", "*.*" '设置两个文件过滤器
If .Show = -1 Then 'FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)。
MsgBox "您选择的文件是:" & .SelectedItems(1), vbOKOnly + vbInformation, "智能Excel"
End If
End With
End Sub
2.多个文件
代码如下(示例):
Sub SelectFile()
'选择多个文件
Dim l As Long
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True '单选择
.Filters.Clear '清除文件过滤器
.Filters.Add "Excel Files", "*.xls;*.xlw"
.Filters.Add "All Files", "*.*" '设置两个文件过滤器
.Show
'FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)。
For l = 1 To .SelectedItems.Count
MsgBox "您选择的文件是:" & .SelectedItems(l), vbOKOnly + vbInformation, "智能Excel"
Next
End With
End Sub
3.其它
代码如下(示例):
Sub SelectFolder()
'选择单一文件
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
'FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)。
MsgBox "您选择的文件夹是:" & .SelectedItems(1), vbOKOnly + vbInformation, "智能Excel"
End If
End With
End Sub
代码如下(示例):
Sub SelectFile()
'选择单一文件
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False '单选择
.Filters.Clear '清除文件过滤器
.Filters.Add "Excel Files", "*.xls;*.xlw"
.Filters.Add "All Files", "*.*" '设置两个文件过滤器
.Execute
End With
End Sub
总结
分享:
乐曲本身单纯如镜,你之所以能够从白雪中听出那么深沉的悲伤,只有一个原因,那个一直没有从噩梦中醒来的人,恰恰就是你自己。