简体   繁体   English

Excel VBA打开工作簿

[英]excel vba open workbook

My code is: 我的代码是:

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

The error which I am receiving is Application or object defined runtime error . 我收到的错误是应用程序或对象定义的运行时错误

How to solve this? 如何解决呢?

Dir() only returns the filename, but Workbooks.Open() requires the full path. Dir()仅返回文件名,但Workbooks.Open()需要完整路径。 Try something like this: 尝试这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM