【问题标题】:excel vba open file runtime error 424excel vba打开文件运行时错误424
【发布时间】:2015-07-28 22:00:40
【问题描述】:

Excel 2010 VBA:我正在尝试遍历文件夹中的文件,并且只打开名称包含特定字符串的文件。我以前做过这个,我知道逻辑有效,但是当我打开目标文件时,我不断收到 424 错误。我很确定它与链接有关,并且已经尝试了一切以关闭这些警报,但我仍然收到错误

Private Sub CommandButton1_Click()
    Dim lSecurity As Long
    Dim myPath As Variant

    lSecurity = Application.AutomationSecurity
    Application.AutomationSecurity = msoAutomationSecurityLow
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False

    myPath = "F:\Pathname"
    Call Recurse(myPath)

    Application.AutomationSecurity = lSecurity
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
End Sub


Function Recurse(sPath As Variant) As String
    Dim FSO As New FileSystemObject
    Dim myFolder As Folder
    Dim myFile As Variant
    Dim file As String
    Dim A As Workbook
    Dim B As Workbook
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim count As Integer

    Set myFolder = FSO.GetFolder(sPath)
    Set A = ThisWorkbook
    i = 2

    For Each myFile In myFolder.Files
        If InStr(myFile.Name, "_2015_DOMESTIC_TB") <> 0 Then
            Set B = Workbooks.Open(Filename:=myFile)
            Call Datadump
            B.Close SaveChanges:=False
        End If
        i = i + 1
    Next

End Function

Function Datadump()

    A.Cells(i, 1).Value = B.Cells(1, 4).Value

    For count = 1 To 59
        k = 2
        A.Cells(i, k).Value = B.Cells(11 + count, 4).Value
        count = count + 1
        k = k + 1
    Next count

End Function

【问题讨论】:

  • 哪一行产生了错误?
  • 错误发生在哪一行?可能在If InStr(myFile.Name, "_2015_DOMESTIC_TB") &lt;&gt; 0 Then。尝试使用Dim myFile As Object 而不是Dim myFile As Variant。然后,Set B = Workbooks.Open(Filename:=myFile.Name) 而不是 Set B = Workbooks.Open(Filename:=myFile)
  • @simple man:根据您的上述评论(信用到期!),我修正了我帖子中的错字:)
  • 我尝试将 myFile 更改为变体,但这会产生错误 1004,并且目标文件永远不会打开。使用原始代码,文件将打开,然后我会在 InStr 代码行中收到 424 错误。我认为错误源于目标文件起源于 Web 文档,但我无法以编程方式越过这些对话框

标签: vba excel


【解决方案1】:

您的函数似乎正在尝试打开非 Excel 文件。将您的功能更改为(未经测试从手机发帖)

Function Recurse(sPath As Variant) As String
    Dim FSO As New FileSystemObject
    Dim myFolder As Folder
    Dim myFile As Variant
    Dim file As String
    Dim A As Workbook, B As Workbook
    Dim i As Integer, j As Integer, k As Integer, count As Integer
    Dim MyAr As Variant

    Set myFolder = FSO.GetFolder(sPath)
    Set A = ThisWorkbook
    i = 2

    For Each myFile In myFolder.Files
        If InStr(myFile.Name, "_2015_DOMESTIC_TB") <> 0 Then
            MyAr = Split(myFile.Name, ".")
            If MyAr(UBound(MyAr)) Like "xls*" Then '<~~ Check if it is an Excel file
                Set B = Workbooks.Open(Filename:=myFile.Name)
                Call Datadump
                B.Close SaveChanges:=False
            End If
        End If
        i = i + 1
    Next
End Function

此功能将检查您是否正在尝试打开有效的 excel 文件。

如果您仍然收到错误,请告诉我们是哪一行出现了错误以及出现错误时myFile.Name 的值是多少。

【讨论】:

  • 代码实际上是打开正确的文件和类型,424错误发生在目标文件打开后(你是对的,在 InStr 行)。我认为文件打开后“updatelinks”会出现问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多