Vba 运行时错误';5';:运行second=Dir时参数无效
我有一个问题的代码,我知道它不是完美的,非常快,但我仍然在学习整个vba的事情。我遇到的问题是当我循环并到达第二个Vba 运行时错误';5';:运行second=Dir时参数无效,vba,Vba,我有一个问题的代码,我知道它不是完美的,非常快,但我仍然在学习整个vba的事情。我遇到的问题是当我循环并到达第二个=dir Sub LoopThroughFiles() Dim StrFolder As Variant Dim StrFile As String StrFolder = "C:\temp\output\" StrFile = Dir("C:\temp\output\") Do While Len(StrFile) > 0
=dir
Sub LoopThroughFiles()
Dim StrFolder As Variant
Dim StrFile As String
StrFolder = "C:\temp\output\"
StrFile = Dir("C:\temp\output\")
Do While Len(StrFile) > 0
Set wb = Workbooks.Open(Filename:=StrFolder & StrFile)
strSearch = "2675"
Set ws = Worksheets("TestCases")
With ws
Set aCell = .Columns(3).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bcell = aCell
Columns("E:E").Insert
aCell.Offset(0, 2) = "FD/WagesAmt"
strSearch = Dir
End If
End With
strSearch = "2017"
Set ws = Worksheets("TestCases")
With ws
Set aCell = .Columns(3).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bcell = aCell
aCell.Offset(0, 2) = "FD/Amt"
aCell.Offset(5, 2) = "FD/Amt"
strSearch = Dir
End If
End With
strSearch = "2078"
Set ws = Worksheets("TestCases")
With ws
Set aCell = .Columns(3).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bcell = aCell
aCell.Offset(0, 2) = "FD/mt"
aCell.Offset(5, 2) = "FD/mt"
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
strSearch = Dir
End If
StrFile = Dir
End With
StrFile = Dir
Loop
End Sub
当它完成第一个循环并转到下一个循环时,strearch=Dir
返回运行时错误,我不确定这是为什么。非常感谢您的帮助
为什么要将
strFile
和strearch
与Dir
一起使用?因为我只是在学习,不知道还能做什么:(去掉所有写着strearch=Dir
的行和重复的strFile=Dir
。如果我这样做,循环就永远不会结束,也不会继续使用同一个文件,它不会转到下一个文件。也许你的问题与你正在使用的更新代码有关。你为什么同时使用strFile
和strearch
Dir
?因为我只是在学习,不知道还能做什么:(去掉所有写着strearch=Dir
的行和重复的strFile=Dir
。如果我这样做,循环就永远不会结束,也会继续使用同一个文件,它不会转到下一个文件。也许你的问题与你正在使用的更新代码有关。