I have a VBA macro that I've been working on for a while. I've almost got it to where I want it, but now I've got a for loop being skipped. I have it set up to check for a number then to go into the for loop and loop through a series of rows looking for certain info. using Breakpoints I can see that it checks the IF statement and then hits the For statement, but then it skips down to the End If and never hits what is inside the for loop. Here's the code in question, not the entire macro (if the whole thing is needed I can add it):
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
It is the first For loop that is hit the for f = fRow To 3 Step -1
then it skips to the last End If
and continues on without executing the code within the for loop. Any ideas what is going on?
Okay, I'm adding the rest of the macro to try and clarify some of the questions:
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
End Sub
I know it's rather long, but this is the whole thing. I've added several message boxes for trouble shooting, they would not normally be there.
What is happening is this: I prompt the user for a branch number, then to open the file from that branch. I have code that runs through the file and pulls info from it. (This code works fine using the for loops). The based on the info pulled and the branch number it chooses where to look for the tellers names and totals on the main workbook, then inserts all this info on a sheet determined by the branch number. Then starts over asking for the next branch number and file. It continues till all 16 branch files have been processed into the main file. I'm sure there are faster ways of doing some of this, but I've not gotten this working yet so I'm not worried about optimization right now. I just need it to work.
have you inspected the value of fRow. It could be that variable fRow is less than variable f when the loop starts, so the condition to start the loop is never met.
Note that the variable fRow is assuming the row value of the first not empty cell in your column fRow = Sheets(2).Range("A188").End(xlUp).Row
So, fRow has a value from 1 to 188, and your loop statement For f = fRow To 3 Step -1
will only work if fRow is greater than 3.
Please, check the value of fRow before it enters in your loop.
Also, BranchNumber can be only 1,2,3,4 or 5, you don't have to check if it's 2 when you already know it's 1....Use the Else condition: BranchNumber只能是1,2,3,4或5,则在您知道它为1时不必检查它是否为2。...使用Else条件:
If (BranchNumber) Like "1" Then
TellerTotal(count) = Range("C" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
ElseIf (BranchNumber) Like "2" Then
TellerTotal(count) = Range("E" & f).Value
MsgBox (TellerTotal(count))
count = count + 1
End If
End If
The error lies with this line:
fRow = Sheets(2).Range("A188").End(xlUp).Row
While this will get the last used row in many cases, if all the data from A1 to A188 is filled with data, the calculation will return 1, as that's the top cell in the range, as it hasn't found a blank cell to end it's search on.
there are 3 possible solutions to this, depending on how your data will appear:
fRow = 188
fRow = Sheets(2).Range("A1").End(xldown).Row
fRow = Sheets(2).cells(sheets(2).Usedrange.Rows.Count+1,1).End(xlUp).Row
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.