我在编写代码时遇到问题,我知道它并不完美并且非常敏捷,但是我仍在学习整个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
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
当它完成第一个循环并转到下一个循环时,strSearch = Dir
会返回运行时错误,我不确定为什么会这样。任何帮助都非常感谢