【问题标题】:excel vba open workbookexcel vba 打开工作簿
【发布时间】:2015-06-15 03:03:44
【问题描述】:

我的代码是:

Dim wb As Workbook
Dim MyObj As Object, MySource As Object, file As Variant
file = Dir("C:\Users\dlf164\Desktop\NE\")
While (file <> "")
    If InStr(file, a) > 0 Then
        Set wb = Workbooks.Open(file)

    End If
file = Dir
Wend

我收到的错误是应用程序或对象定义的运行时错误

如何解决?

【问题讨论】:

标签: vba excel


【解决方案1】:

Dir() 只返回文件名,但Workbooks.Open() 需要完整路径。试试这样的:

Dim wb As Workbook
Dim MyObj As Object, MySource As Object, file As Variant

Dim path As String
path = "C:\Users\dlf164\Desktop\NE\"
file = Dir(path)
While (file <> "")
    If InStr(file, a) > 0 Then
        Set wb = Workbooks.Open(path & file)
    End If
file = Dir
Wend

【讨论】:

  • @user3764484 请accept 回答(对所有旧问题都这样做)。在左侧的上/下箭头下,有一个复选标记,点击接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多