无法从文件夹VBA获取xls文件

时间:2014-03-17 01:12:42

标签: excel vba excel-vba

我正在尝试从文件夹中打开xls格式文件,并使用宏工作簿从中获取数据 但不能这样做。我把它变成空的

Application.ScreenUpdating = False

sPath = "C:\Documents and Settings\Administrator\My Documents\New Folder"
qwe = Dir(sPath & "*.xls")

即使文件夹不为空,我的qwe也是空的,它有3个格式为xls的excel文件。

2 个答案:

答案 0 :(得分:1)

我认为这可能有效,只需在新文件夹目录中添加斜杠,而不是所有以'新文件夹'开头的xls。在我的文件目录中:

sPath = "C:\Documents and Settings\Administrator\My Documents\New Folder"
qwe = Dir(sPath & "\*.xls")

答案 1 :(得分:0)

我刚刚在Excel 2010中编写并测试了它。这是非常简单的代码,应该可以在Excel 2007中使用。

这将枚举所有匹配* .xls

的文件
Public Sub GetFiles()

Dim spath As String
Dim file As String

spath = "<enter your path here with a trailing backslash>" 
'eg "C:\Documents and Settings\Administrator\My Documents\New Folder\"

file = Dir(spath & "*.xls")

Do While file <> ""
    MsgBox (file)
    file = Dir
Loop


End Sub
相关问题