我正在使用 VBA 打开 excel 文件
Set Workbook = Application.Workbooks.Open(File)
在哪里File = "C:\GSTR Automation\GSTR2\February\1000\ReverseCharge\Outputs\ReverseChargeZonic_1000.xlsx"
我收到 vba 错误,它找不到文件。
最佳答案
But file is present there , I have verified the path manually – karan arora 33 mins ago
I have copied the file location and name from the file still getting the same error – karan arora 30 mins ago
这不是答案,但可以帮助您确定在这种情况下可能出现的问题。
逻辑:
此代码(未完全测试)将采用路径,并逐个文件夹检查它是否存在。我在我的
C:
中创建了相同的结构这样你就可以看到它是如何工作的代码:
Option Explicit
Sub Sample()
Dim sFile As String
Dim Ar As Variant
Dim i As Long
Dim DoesFileExist As Boolean
sFile = "C:\GSTR Automation\GSTR2\February\1000\ReverseCharge\Outputs\ReverseChargeZonic_1000.xlsx"
Ar = Split(sFile, "\")
If UBound(Ar) = 1 Then
MsgBox "File Exists: " & FileFolderExists(sFile)
Else
sFile = Ar(0)
For i = 1 To UBound(Ar)
sFile = sFile & "\" & Ar(i)
DoesFileExist = FileFolderExists(sFile)
If DoesFileExist = False Then
MsgBox sFile & " not found"
Exit Sub
Else
MsgBox sFile & " found"
End If
Next i
End If
End Sub
'~~> Function to check if file/folder exists
Private Function FileFolderExists(strFullPath As String) As Boolean
On Error GoTo Whoa
If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
Whoa:
On Error GoTo 0
End Function
行动中:现在我改了
February
至January
在上述路径中现在看看上面的代码是如何响应的
关于excel - 即使文件夹中存在文件,VBA也找不到excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66685982/