Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 运行时错误1004消息我的代码有什么问题_Excel_Vba - Fatal编程技术网

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

Excel 运行时错误1004消息我的代码有什么问题,excel,vba,Excel,Vba,在我的情况下,我的文件(“ABC.xlsx数据表”)有两种可能位于桌面或文件夹“C:\Users\vipul\desktop\photos to Arranged”中。 在这段代码中,当我的名为“ABC.xlsx数据表”的文件在桌面上时,代码运行正常。 但是,当该文件位于“C:\Users\vipul\Desktop\photos to arrange”文件夹中时,在设置WB=workbooks.Open(文件)时会出现错误 请指导我您必须在打开的文件中包含路径 像这样试试 Dim WB As

在我的情况下,我的文件(“ABC.xlsx数据表”)有两种可能位于桌面或文件夹“C:\Users\vipul\desktop\photos to Arranged”中。 在这段代码中,当我的名为“ABC.xlsx数据表”的文件在桌面上时,代码运行正常。 但是,当该文件位于“C:\Users\vipul\Desktop\photos to arrange”文件夹中时,在设置WB=workbooks.Open(文件)时会出现错误
请指导我

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

像这样试试

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)

Dir$
不返回完整路径。。。除了使用
Dir$
的结果外,还可以使用
Path
Path1
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)