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