Dim fd As FileDialog '定义对话框变量
Set fd = Application.FileDialog(msoFileDialogFolderPicker) '使用 FileDialog 对象显示“文件选取器”对话框
With fd
If .Show = -1 Then '如果选择了确定按钮,则(.show的值等于0,表示按取消按钮)
T = .SelectedItems(1) & "\" '用T记录下它的路径
Else
Set fd = Nothing: Exit Sub '按取消之后就退出程序
End If
End With
TextBox1 = fd.SelectedItems(1)
myPath = fd.SelectedItems(1) & "\" '保存文件路径 做为Dir()函数的参数
myName = Dir(myPath & "*.xlsx") '打开一个excel文件
Do While myName <> "" '循环,当文件名不为空时。
If myName <> "Book1.xlsm" Then
With GetObject(myPath & myName) '获得文件对象
‘操作文件内容的代码。在此省略
End With
End If
myName = Dir ' 遍历目录中的所有文件
Loop