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

时间:2019-11-25 17:44:09

标签: excel vba

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)

就我而言,我的文件有两个可能性(“ ABC.xlsx数据表”)将位于桌面上或位于“ C:\ Users \ vipul \ Desktop \ photos进行排列”文件夹中。 在此代码中,当我的名为“ ABC.xlsx数据表”的文件在桌面上时,该代码运行良好。 但是,当该文件位于“ C:\ Users \ vipul \ Desktop \ photos进行排列”文件夹中时,它在设置WB = workbooks.Open(file)时给我错误 请指导我

1 个答案:

答案 0 :(得分:1)

您必须在打开的文件中包含路径。

尝试这样

<img id="Main_Url" class="rounded-circle  height-150" alt="Card image"src="../pics/**%20**shlomo.jpeg">