如何用VBA打开选择文件和选择路径的对话框

如何用VBA打开选择文件和选择路径的对话框,第1张

'注意引用 microsoft office 10.0 (或以上) object library'在文件对话框对中返回选择一个文件夹的路径.Public Function ChooseFolder() As StringDim dlgOpen As FileDialogSet dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)With dlgOpenIf .Show = -1 ThenChooseFolder = .SelectedItems(1)End IfEnd WithSet dlgOpen = NothingEnd Function'--------------------------------------------------------'在文件对话框对中,选择一个文件。Public Function ChooseOneFile(Optional TitleStr As String= 选择你要的文件, Optional TypesDec As String = 所有文件,Optional Exten As String = *.*) As StringDim dlgOpen As FileDialogSet dlgOpen = Application.FileDialog(msoFileDialogFilePicker)With dlgOpen.Title = TitleStr.Filters.Clear'清除所有的文件类型..Filters.Add TypesDec, Exten.AllowMultiSelect = False '不能多选.If .Show = -1 Then'.AllowMultiSelect = True '多个文件'For Each vrtSelectedItem In .SelectedItems'MsgBox Path name:& vrtSelectedItem'Next vrtSelectedItemChooseOneFile = .SelectedItems(1)'第一个文件End IfEnd With

Sub SelectFile()

Application.DisplayAlerts = False

fil = ThisWorkbook.Name

Filename = Application.GetOpenFilename("Excel 文件 ,*.xls*.xlsx")

If Filename <>False Then

aFile = Split(Filename, "\")

sfilename = aFile(UBound(aFile))

End If

Workbooks.Open (Filename)

Workbooks(sfilename).Sheets(1).Cells.Copy Workbooks(fil).Sheets(1).Cells

Workbooks(sfilename).Close

Application.DisplayAlerts = True

End Sub

Excel

开发人员参考

Application.Dialogs

属性

返回一个

Dialogs

集合,该集合表示所有内置对话框的。只读。

语法

表达式.Dialogs

表达式

一个代表

Application

对象的变量。

示例

本示例显示“文件”菜单的“打开”对话框。

Visual

Basic

for

Applications

Application.Dialogs(xlDialogOpen).Show

搞一个事件引发Application.Dialogs(xlDialogOpen).Show

就会

d出文件对话框

至于打开文件,要看你是这么打开的


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7949935.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存