excel - 即使文件夹中存在文件,VBA也找不到excel文件

标签 excel vba

我正在使用 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: 中创建了相同的结构这样你就可以看到它是如何工作的
enter image description here
代码:
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
行动中:
enter image description here
现在我改了FebruaryJanuary在上述路径中
enter image description here
现在看看上面的代码是如何响应的
enter image description here

关于excel - 即使文件夹中存在文件,VBA也找不到excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66685982/

相关文章:

vba - 遍历表格中的每一行

excel - 使用值复制和粘贴

excel - SharePoint 2010 : working with Excel files

c# - 使用 NPOI 将超链接从一个单元格复制到另一个单元格

php - Excel VBA : Dynamic Variable Name

vba - 如何在 SUMIF 中使用不连续范围

excel - 子间误差线

vba - 使用 VBA 在 Excel 单元格中编写公式

excel - 同时将 2 个选定的相邻单元格的内容复制到表格的最后一行

vba - CheckSpelling 函数中的类型不匹配