【发布时间】: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") <> 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 文档,但我无法以编程方式越过这些对话框