我想要打开指定文件夹中的所有文件,并具有以下代码
Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
MyFolder = "\\ILAFILESERVER\Public\Documents\Renewable Energy\FiTs\1 Planning
Department\Marks Tracker\Quality Control Reports"
MyFile = Dir(MyFolder & "\*.xlsx")
Do While MyFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyFile
Loop
End Sub
我的问题是,它只是不断尝试打开文件夹中的第一个文件,并且不继续前进。有人能帮上忙吗?我是VBA的新手,真的需要一些帮助。我正在尝试打开大约30个报告,它们都是.xlsx格式的。在此之前,非常感谢您。
发布于 2012-06-22 08:58:35
您必须将此行添加到loop
之前
MyFile = Dir
Loop
发布于 2015-05-19 04:25:32
你可以在循环检查语句中使用Len(StrFile) > 0
!
Sub openMyfile()
Dim Source As String
Dim StrFile As String
'do not forget last backslash in source directory.
Source = "E:\Planning\03\"
StrFile = Dir(Source)
Do While Len(StrFile) > 0
Workbooks.Open Filename:=Source & StrFile
StrFile = Dir()
Loop
End Sub
发布于 2013-02-15 09:37:15
试试下面的代码:
Sub opendfiles()
Dim myfile As Variant
Dim counter As Integer
Dim path As String
myfolder = "D:\temp\"
ChDir myfolder
myfile = Application.GetOpenFilename(, , , , True)
counter = 1
If IsNumeric(myfile) = True Then
MsgBox "No files selected"
End If
While counter <= UBound(myfile)
path = myfile(counter)
Workbooks.Open path
counter = counter + 1
Wend
End Sub
https://stackoverflow.com/questions/11152870
复制相似问题