Last time this question was asked it was wrong! Vba code as bugs in it. Can you
ID: 3851778 • Letter: L
Question
Last time this question was asked it was wrong!
Vba code as bugs in it. Can you please fix the bugs and leave comments to explain the bugs and what was done to fix the bugs with copy code.
The tabs at the bottom are Indiana, Ohio, Illinois, Wisconsin, Michigan.
This is the code I need to use, I need to fix the bugs and comments on how they were fixed. please leave a copy code
Sub ListStates()
Dim ws As String, message As String
message = "Here is a list of states:"
For Each ws In ActiveWorkbook
message = message & vbCrLf & "ws.Name"
Next
MsgBox message, vbInformation, "State list"
End Sub
Sub StateSearch()
InputBox "Enter a state you'd like to search for.", "State search"
FindState state, isFound
MsgBox "State was " & isFound, vbInformation
End Sub
Sub FindState(state As String)
Dim isFound As Boolean
For Each ws In ActiveWorkbook
If ws.Name = state Then
isFound = True
Exit For
Else
isFound = False
End If
Next
End Sub
Sub CountSales()
Dim state As String, salesRep As String
state = InputBox "Enter a state"
salesRep = InputBox "Enter a SalesRep"
On Error GoTo NoSuchState
With Worksheets(state).Range("B3")
Count = 0
For Each cell In Range(.Offset(1, 0), .End)
If cell = StateRep Then Count = Count + 1
Next
End With
MsgBox "SalesRep made " Count " sales in " state, vbInformation
NoSuchState:
MsgBox "The state " state " is not one of the worksheets.", vbInformation
End Sub
Sub TotalSales1()
Dim lastDate As Date, total As Currency
MsgBox "You'll now be asked for a date. Then the total of all " _
" sales up to that date will be totaled.", vbInformation , "Purpose"
lastDate = InputBox("Enter the last date for the total."),"Last date"
total = 0
For Each ws In ActiveWorkbook
With Worksheets("ws").Range("A3")
Do Until .Offset(i, 0) = ""
If .Offset(i, 0) <= lastDate Then total = total + .Offset(i, 2)
Loop
End With
Next
MsgBox "The total of all sales through " & Format(lastDate, "1/1/99") is " _
& Format(Total,$#,##0), vbinformation, "Sales Total"
End Sub
Sub TotalSales2()
Dim states() As String, reps() As String, repName As String, _
nStates As Integer, nReps As Integer, newName As Boolean, _
ws As Worksheet, i As Integer, j As Integer
nStates = 0
nReps = 0
For Each ws In ActiveWorkbook
' Add this sheet's state to the States array.
ReDim states(nStates)
states(nStates) = ws.Name
nStates = nStates + 1
' Go through all of the names in this state's worksheet. For each name, check
' if this is a new rep not already in the reps array. If it is, add it to
' the array.
With Worksheets(ws).Range("B3")
Do Until .Offset(i, 0) = ""
repName = .Offset(i, 0)
newName = True
If nReps > 0 Then
For j = 1 To nReps
If repName = reps(j) Then
newName = False
Exit For
End If
Next
If newName = True Then
ReDim reps(nReps)
reps(nReps) = repName
nReps = nReps + 1
End If
End If
Loop
End With
Next
' Show the user form and capture the user's selections in the variables
' selectedState and selectedRep.
frmInputs.Show
' Find the total sales for the selected rep in the selected state.
total = 0
With Worksheets("SelectedState").Range("B3")
Do Until .Offset(i, 0) <> ""
For j = 1 To nReps
repName = .Offset(i, 0)
If selectedRep = repName Then
total = total + .Offset(i, 1)
Exit For
End If
Next
Loop
End With
MsgBox "The total sales for " & selectedRep & " in " & state & was " _
Format(Total,$#,##0), vbInformation
End Sub
Explanation / Answer
Sub ListStates()
Dim ws As String, message As String
message = "Here is a list of states:"
For Each ws In ActiveWorkbook
message = message & vbCrLf & "ws.Name"
Next
MsgBox message, vbInformation, "State list"
End Sub
Sub StateSearch()
InputBox "Enter a state you'd like to search for.", "State search"
FindState state, isFound
MsgBox "State was " & isFound, vbInformation
End Sub
Sub FindState(state As String)
Dim isFound As Boolean
For Each ws In ActiveWorkbook
If ws.Name = state Then
isFound = True
Exit For
Else
isFound = False
End If
Next
End Sub
Sub CountSales()
Dim state As String, salesRep As String
state = InputBox "Enter a state"
salesRep = InputBox "Enter a SalesRep"
On Error GoTo NoSuchState
With Worksheets(state).Range("B3")
Count = 0
For Each cell In Range(.Offset(1, 0), .End)
If cell = StateRep Then Count = Count + 1
Next
End With
MsgBox "SalesRep made " Count " sales in " state, vbInformation
NoSuchState:
MsgBox "The state " state " is not one of the worksheets.", vbInformation
End Sub
Sub TotalSales1()
Dim lastDate As Date, total As Currency
MsgBox "You'll now be asked for a date. Then the total of all " _
" sales up to that date will be totaled.", vbInformation , "Purpose"
lastDate = InputBox("Enter the last date for the total."),"Last date"
total = 0
For Each ws In ActiveWorkbook
With Worksheets("ws").Range("A3")
Do Until .Offset(i, 0) = ""
If .Offset(i, 0) <= lastDate Then total = total + .Offset(i, 2)
Loop
End With
Next
MsgBox "The total of all sales through " & Format(lastDate, "1/1/99") is " _
& Format(Total,$#,##0), vbinformation, "Sales Total"
End Sub
Sub TotalSales2()
Dim states() As String, reps() As String, repName As String, _
nStates As Integer, nReps As Integer, newName As Boolean, _
ws As Worksheet, i As Integer, j As Integer
nStates = 0
nReps = 0
For Each ws In ActiveWorkbook
' Add this sheet's state to the States array.
ReDim states(nStates)
states(nStates) = ws.Name
nStates = nStates + 1
' Go through all of the names in this state's worksheet. For each name, check
' if this is a new rep not already in the reps array. If it is, add it to
' the array.
With Worksheets(ws).Range("B3")
Do Until .Offset(i, 0) = ""
repName = .Offset(i, 0)
newName = True
If nReps > 0 Then
For j = 1 To nReps
If repName = reps(j) Then
newName = False
Exit For
End If
Next
If newName = True Then
ReDim reps(nReps)
reps(nReps) = repName
nReps = nReps + 1
End If
End If
Loop
End With
Next
' Show the user form and capture the user's selections in the variables
' selectedState and selectedRep.
frmInputs.Show
' Find the total sales for the selected rep in the selected state.
total = 0
With Worksheets("SelectedState").Range("B3")
Do Until .Offset(i, 0) <> ""
For j = 1 To nReps
repName = .Offset(i, 0)
If selectedRep = repName Then
total = total + .Offset(i, 1)
Exit For
End If
Next
Loop
End With
MsgBox "The total sales for " & selectedRep & " in " & state & was " _
Format(Total,$#,##0), vbInformation
End Sub
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.