我有一个我已经研究了一段时间的 VBA 宏。我几乎把它放到了我想要的地方,但现在我跳过了一个 for 循环。我将其设置为检查一个数字,然后进入 for 循环并遍历一系列行以查找某些信息。使用断点我可以看到它检查了 IF 语句,然后点击了 For 语句,但随后它跳到了 End If 并且从不点击 for 循环内的内容。这是有问题的代码,而不是整个宏(如果需要整个东西,我可以添加它):
If (BranchNumber) Like "0" Or (BranchNumber) Like "1" Or (BranchNumber) Like "2" Or (BranchNumber) Like "3" Or (BranchNumber) Like "4" Or (BranchNumber) Like "5" Then
fRow = Sheets(2).Range("A188").End(xlUp).Row
For f = fRow To 3 Step -1
Range("A" & f).Select
For CountDown = 0 To 30 Step 1
If (Range("A" & f).Value) Like Tellers(CountDown) Then
TellerName(count) = Range("B" & f).Value
MsgBox (CountDown)
MsgBox (Tellers(CountDown))
MsgBox (count)
MsgBox (TellerName(count))
MsgBox (TellerTotal(count))
If (BranchNumber) Like "0" Then
TellerTotal(count) = Range("C" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "1" Then
TellerTotal(count) = Range("D" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "2" Then
TellerTotal(count) = Range("E" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "3" Then
TellerTotal(count) = Range("F" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "4" Then
TellerTotal(count) = Range("G" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "5" Then
TellerTotal(count) = Range("H" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
End If
Next
Next f
End If
它是第一个被命中的 For 循环,for f = fRow To 3 Step -1
然后它跳到最后一个End If
并继续,而不执行 for 循环中的代码。有什么想法吗?
好的,我正在添加宏的其余部分以尝试澄清一些问题:
Sub GetInfo()
Dim NumberBranches
Dim Branch
Dim Tellers(30), TellerTotal(30), TellerName(30)
Dim count As Long
Dim CountDown
Dim l, f As Long
Dim lRow, fRow As Long
Dim BranchNumber As Long
Dim Branches As Long
Dim lngIndex As Long
Dim strPath() As String
NumberBranches = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12", "13", "16", "18", "19", "20")
For Branches = 0 To 15 Step 1
BranchNumber = InputBox("What is the branch number?", "Branch Number", NumberBranches(Branches))
Branch = Application.GetOpenFilename("Excel files (*.xls), *.xls")
'Application.ScreenUpdating = False
Workbooks.Open FileName:=Branch
strPath() = Split(path, "\")
lngIndex = UBound(strPath)
Branch = Mid(Mid(Branch, InStrRev(Branch, "/") + 1), InStrRev(Branch, "\") + 1)
Windows(Branch).Activate
lRow = Sheets(1).Range("A200").End(xlUp).Row
MsgBox (lRow)
count = 0
For l = lRow To 1 Step -1
MsgBox (l)
MsgBox (lRow)
Sheets(1).Activate
Range("A" & l).Select
If (BranchNumber) Like "19" Then
If (Range("B" & l).Value) Like "TC:*" Then
Tellers(count) = Replace(Range("B" & l).Value, "TC: ", "")
count = count + 1
End If
If (Range("H" & l).Value) Like "TC:*" Then
Tellers(count) = Replace(Range("H" & l).Value, "TC: ", "")
count = count + 1
End If
End If
If BranchNumber <> "19" Then
If (Range("A" & l).Value) Like "TC:" Then
Tellers(count) = Range("B" & l).Value
count = count + 1
End If
If (Range("H" & l).Value) Like "TC:" Then
Tellers(count) = Range("I" & l).Value
count = count + 1
End If
End If
Next l
Windows("TellerTranCounts.xlsm").Activate
Sheets(2).Activate
If (BranchNumber) = "0" Or (BranchNumber) = "1" Or (BranchNumber) = "2" Or (BranchNumber) = "3" Or (BranchNumber) = "4" Or (BranchNumber) = "5" Then
fRow = Sheets(2).Range("A188").End(xlUp).Row
MsgBox (fRow)
MsgBox (f)
For f = fRow To 3 Step -1
MsgBox (f)
Range("A" & f).Select
For CountDown = 0 To 30 Step 1
If (Range("A" & f).Value) Like Tellers(CountDown) Then
TellerName(count) = Range("B" & f).Value
MsgBox (CountDown)
MsgBox (Tellers(CountDown))
MsgBox (count)
MsgBox (TellerName(count))
MsgBox (TellerTotal(count))
If (BranchNumber) Like "0" Then
TellerTotal(count) = Range("C" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "1" Then
TellerTotal(count) = Range("D" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "2" Then
TellerTotal(count) = Range("E" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "3" Then
TellerTotal(count) = Range("F" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "4" Then
TellerTotal(count) = Range("G" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
If (BranchNumber) Like "5" Then
TellerTotal(count) = Range("H" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
End If
Next
Next f
End If
If (BranchNumber) Like "6" Or (BranchNumber) Like "7" Or (BranchNumber) Like "8" Or (BranchNumber) Like "9" Or (BranchNumber) Like "11" Or (BranchNumber) Like "12" Then
fRow = Sheets(2).Range("A375").End(xlUp).Row
For f = fRow To 189 Step -1
Range("A" & f).Select
For CountDown = 0 To 30 Step 1
If (Range("A" & f).Value) Like Tellers(CountDown) Then
TellerName(count) = Range("B" & f).Value
If (BranchNumber) Like "6" Then
TellerTotal(count) = Range("C" & f).Value
count = count + 1
End If
If (BranchNumber) Like "7" Then
TellerTotal(count) = Range("D" & f).Value
count = count + 1
End If
If (BranchNumber) Like "8" Then
TellerTotal(count) = Range("E" & f).Value
count = count + 1
End If
If (BranchNumber) Like "9" Then
TellerTotal(count) = Range("F" & f).Value
count = count + 1
End If
If (BranchNumber) Like "11" Then
TellerTotal(count) = Range("G" & f).Value
count = count + 1
End If
If (BranchNumber) Like "12" Then
TellerTotal(count) = Range("H" & f).Value
count = count + 1
End If
End If
Next
Next f
End If
If (BranchNumber) Like "13" Or (BranchNumber) Like "16" Or (BranchNumber) Like "18" Or (BranchNumber) Like "19" Or (BranchNumber) Like "20" Or (BranchNumber) Like "51" Then
fRow = Sheets(2).Range("A562").End(xlUp).Row
For f = fRow To 377 Step -1
Range("A" & f).Select
For CountDown = 0 To 30 Step 1
If (Range("A" & f).Value) Like Tellers(CountDown) Then
TellerName(count) = Range("B" & f).Value
If (BranchNumber) Like "13" Then
TellerTotal(count) = Range("C" & f).Value
count = count + 1
End If
If (BranchNumber) Like "16" Then
TellerTotal(count) = Range("D" & f).Value
count = count + 1
End If
If (BranchNumber) Like "18" Then
TellerTotal(count) = Range("E" & f).Value
count = count + 1
End If
If (BranchNumber) Like "19" Then
TellerTotal(count) = Range("F" & f).Value
count = count + 1
End If
If (BranchNumber) Like "20" Then
TellerTotal(count) = Range("G" & f).Value
count = count + 1
End If
If (BranchNumber) Like "51" Then
TellerTotal(count) = Range("H" & f).Value
count = count + 1
End If
End If
Next
Next f
End If
Dim Counted
Dim InputCount
Dim BranchName As String
If BranchNumber Like "1" Then
BranchName = "Swinney Teller Report"
Sheets(3).Activate
End If
If BranchNumber Like "2" Then
BranchName = "Decatur Teller Report"
Sheets(4).Activate
End If
If BranchNumber Like "3" Then
BranchName = "Tillman Teller Report"
Sheets(5).Activate
End If
If BranchNumber Like "4" Then
BranchName = "Huntington Teller Report"
Sheets(6).Activate
End If
If BranchNumber Like "5" Then
BranchName = "Medical Park Teller Report"
Sheets(7).Activate
End If
If BranchNumber Like "6" Then
BranchName = "West Jefferson Teller Report"
Sheets(8).Activate
End If
If BranchNumber Like "7" Then
BranchName = "New Haven Teller Report"
Sheets(9).Activate
End If
If BranchNumber Like "8" Then
BranchName = "Waynedale Teller Report"
Sheets(10).Activate
End If
If BranchNumber Like "9" Then
BranchName = "Scottsville Teller Report"
Sheets(11).Activate
End If
If BranchNumber Like "11" Then
BranchName = "Columbia City Teller Report"
Sheets(12).Activate
End If
If BranchNumber Like "12" Then
BranchName = "Danville Teller Report"
Sheets(13).Activate
End If
If BranchNumber Like "13" Then
BranchName = "Mattoon Teller Report"
Sheets(14).Activate
End If
If BranchNumber Like "16" Then
BranchName = "Lima Teller Report"
Sheets(15).Activate
End If
If BranchNumber Like "18" Then
BranchName = "Stellhorn Crossing Teller Report"
Sheets(16).Activate
End If
If BranchNumber Like "19" Then
BranchName = "Wayne Haven Teller Report"
Sheets(17).Activate
End If
If BranchNumber Like "20" Then
BranchName = "Hopkinsville Teller Report"
Sheets(18).Activate
End If
Range("B1").Value = BranchName
Range("B2").Formula = "=Today()"
ActiveCell.NumberFormat = "[$-409]mmmm-yy;@"
Range("A5").Value = "Totals"
Range("B5").Value = "Name of Employee"
Range("C5").Value = "Error"
Range("D5").Value = "Percent"
Counted = 6
For InputCount = 0 To 30 Step 1
If TellerName(InputCount) <> "" Then
Range("A" & InputCount + 6).Value = TellerTotal(InputCount)
Range("A" & InputCount + 6).NumberFormat = "#,##0"
Range("B" & InputCount + 6).Value = TellerName(InputCount)
Range("D" & InputCount + 6).Formula = "=if(A" & InputCount & "<>0,(A" & InputCount & "-C" & InputCount & ")/A" & InputCount & ",""N/A"")"
Range("D" & InputCount + 6).NumberFormat = "0.000%"
Counted = Counted + 1
End If
Next
Range("A" & Counted).Formula = "=sum(A6:A" & Counted & ")"
Range("B" & Counted).Value = "Total Tellers"
Range("C" & Counted).Formula = "=sum(C6:C" & Counted & ")"
Range("D" & Counted).Formula = "=if(A" & Counted & "<>0,(A" & Counted & "-C" & Counted & ")/A" & Counted & ",""N/A"")"
Range("D" & Counted).NumberFormat = "0.000%"
Columns("A:D").Select
Selection.Columns.AutoFit
Call Format(Counted)
Call FormatBorder(Counted)
Range("A1").Select
Windows(Branch).Activate
Windows(Branch).Close (False)
Next
结束子
我知道它很长,但这就是全部。我添加了几个用于故障排除的消息框,它们通常不会出现。
发生的事情是这样的:我提示用户输入分支号,然后从该分支打开文件。我有贯穿文件并从中提取信息的代码。(此代码使用 for 循环可以正常工作)。根据提取的信息和分行编号,它选择在主工作簿上查找出纳员姓名和总数的位置,然后将所有这些信息插入到由分行编号确定的工作表上。然后重新开始询问下一个分支编号和文件。它一直持续到所有 16 个分支文件都被处理到主文件中。我确信有更快的方法来做这件事,但我还没有得到这个工作,所以我现在不担心优化。我只需要它工作。