简体   繁体   English

运行时错误 1004 消息 - 我的代码有什么问题

[英]Run time error 1004 message-what is wrong with my code

Dim WB As Workbook
Dim wkbReport As Workbook
Dim Path As String
Dim Path1 As String
Dim file As String
Path = CreateObject("WScript.Shell").SpecialFolders("Desktop")
Path1 = "C:\Users\vipul\Desktop\photos to arrange"

Set wkbReport = ThisWorkbook

            If Not Dir$(Path & "\data sh*" & ".*", vbDirectory) = vbNullString Then
                    file = Dir$(Path & "\data sh*" & ".*")
            Else
                    file = Dir$(Path1 & "\Data sh*" & ".*")
            End If

Set WB = Workbooks.Open(file)

In my case, there is two possiblities of my file ("Data sheet for ABC.xlsx") will be either on desktop or in folder "C:\\Users\\vipul\\Desktop\\photos to arrange".就我而言,我的文件(“ABC.xlsx 的数据表”)有两种可能位于桌面上或文件夹“C:\\Users\\vipul\\Desktop\\photos 以进行排列”。 Here in this code when my file named as "Data sheet for ABC.xlsx" is on Desktop, the code is running fine.在此代码中,当我名为“ABC.xlsx 的数据表”的文件在桌面上时,代码运行良好。 But, when that file is in "C:\\Users\\vipul\\Desktop\\photos to arrange" folder, it gives me error at Set WB = workbooks.Open(file) Please guide me但是,当该文件位于“C:\\Users\\vipul\\Desktop\\photos to Arrange”文件夹中时,它会在 Set WB = workbooks.Open(file) 出现错误,请指导我

you have to include the path in the open file.您必须在打开的文件中包含路径。

try like this像这样尝试

Dim WB As Workbook
Dim wkbReport As Workbook
Dim Path As String
Dim Path1 As String
Dim file As String
Path = CreateObject("WScript.Shell").SpecialFolders("Desktop")
Path1 = "C:\Users\vipul\Desktop\photos to arrange"

Set wkbReport = ThisWorkbook

            If Not Dir$(Path & "\data sh*" & ".*", vbDirectory) = vbNullString Then
                    file = Dir$(Path & "\data sh*" & ".*")
                    Path = Path & "\"
            Else
                    file = Dir$(Path1 & "\Data sh*" & ".*")
                    Path = Path1 & "\"
            End If

Set WB = Workbooks.Open(Path & file)

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

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