【发布时间】:2018-11-28 11:27:27
【问题描述】:
请检查代码,我认为错误在于退出错误循环。
Option Explicit
Dim mm
Dim person As Range
Dim amount As Range
Dim ans
Sub Macro1()
again:
mm = InputBox("Enter the name of the sales person")
If mm = "Jack" Then
GoTo Start
ElseIf mm = "Heather" Then
GoTo Start
ElseIf mm = "Jeff" Then
GoTo Start
ElseIf mm = "Stephanie" Then
GoTo Start
ElseIf mm = "Mike" Then
GoTo Start
ElseIf mm = "Marty" Then
GoTo Start
ElseIf mm = "Peter" Then
GoTo Start
ElseIf mm = "Jack" Then
GoTo Start
ElseIf mm = "Lisa" Then
GoTo Start
Else
GoTo Error
Error:
MsgBox ("Error: invalid name,no match found")
ans = MsgBox("Do you want to try again?", vbYesNo)
If ans = 8 Then
GoTo again
ElseIf ans = 7 Then
GoTo Exit
Start:
Set person = Range(Cells(6, 2), Cells(148, 2))
Set amount = Range(Cells(6, 3), Cells(148, 3))
Cells(13, 8) = mm
Cells(14, 8) = WorksheetFunction.SumIfs(amount, person, mm)
Cells(15, 8) = WorksheetFunction.AverageIfs(amount, person, mm)
Cells(16, 8) = WorksheetFunction.MinIfs(amount, person, mm)
Cells(17, 8) = WorksheetFunction.MaxIfs(amount, person, mm)
ans = MsgBox("Do you want to try again?", vbYesNo)
If ans = 8 Then GoTo again
End If
Exit:
End Sub
由于某些奇怪的原因,excel不允许我输入退出:它显示编译错误,但我不知道为什么
非常感谢您的帮助
【问题讨论】:
-
Exit是保留字,不能作为标签使用。但正如已经提到的,这可能需要一些返工。您缺少几个End If实例。 -
@urdearboy 你能建议一种替代方法吗 - 如果我删除 goto start 我应该用什么交换它?我是 vba 和编程的新手,请多多包涵。谢谢!
-
@BigBen 非常感谢。一旦我更改了我的标签,这个问题似乎已经解决了 :) 祝你有美好的一天
-
现在您的代码可以正常工作,请随时将其发布到Code Review 以获取有关重组控制流和避免
GoTo跳转的提示。