excel vba打开文件运行时错误424

Excel 2010 VBA:我想循环通过文件夹中的文件,只打开名称包含特定string的文件。 我之前已经完成了这个工作,而且我知道这个逻辑是可行的,但是当我打开目标文件的时候,我总是收到424错误。 我敢肯定,这与链接有关,并试图一切closures这些警报有问题,但我仍然得到错误

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 

看起来像你的function是试图打开一个非Excel文件。 将您的function更改为(未通过电话发送)

 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的值是myFile.Name