简体   繁体   English

Excel VBA 结束 Select 不带 Select

[英]Excel VBA End Select without Select Case

I am making a program to take data from a spreadsheet regarding products, gather their info, and calculate if more needs to be ordered.我正在制作一个程序来从电子表格中获取有关产品的数据,收集他们的信息,并计算是否需要订购更多产品。 I have been trying to get a Select Case statement working and no matter how I code it, I get the same error.我一直试图让 Select Case 语句正常工作,无论我如何编码,我都会遇到同样的错误。 i've done it in a while loop, in a for loop, and by itself.我已经在 while 循环、for 循环和自身中完成了它。 Error persists "End Select without Select Case" which as you can see isn't right.错误仍然存在“End Select without Select Case”如您所见,这是不正确的。

  Sub Ordering()
Dim VendorName, Product As String
Dim Counter, ProductOffset, OnHand, OnHandOffset, OnOrder, OnOrderOffset, Commited, CommitedOffset As Integer

ProductOffset = 14 'columns out from A
OnHandOffset = 19
OnOrderOffset = 20
CommitedOffset = 21
Counter = 0

'start active cell in top left
Range("A1").Select

    'grab product code
    Product = ActiveCell.Offset(0, ProductOffset).Value
    OnHand = ActiveCell.Offset(0, OnHandOffset).Value
    OnOrder = ActiveCell.Offset(0, OnOrderOffset).Value
    Commited = ActiveCell.Offset(0, CommitedOffset).Value

    Select Case Product
        Case "100HB"
            If (OnHand + OnOrder - Commited) <= 1000 Then
                MsgBox ("Found 100HB")

    End Select
End Sub

The compiler works its way down the procedure from the top.编译器从顶部向下运行。 When it encounters a "block start", it doesn't skip to the bottom to try and find the corresponding "block end".当它遇到“block start”时,它不会跳到底部去尝试找到对应的“block end”。 Instead it stacks up "block start" tokens and when it encounters a "block end" token, it pops that stack and compares to what it's expecting: if it's expecting End If but instead finds End Select , a compile (syntax) error is thrown:相反,它堆叠“块开始”标记,当它遇到“块结束”标记时,它会弹出该堆栈并与预期的内容进行比较:如果它期待End If但找到End Select ,则会引发编译(语法)错误:

  • Enter the Select Case block.输入Select Case块。 Expected end-block: End Select预期的结束块: End Select
  • Enter the If block in the first case.在第一种情况下输入If块。 Expected end-block: End If预期的结束块: End If
  • Encounter End Select , expected End If => throw "end select without select case" syntax error遇到End Select , 预期End If => throw "end select without select case" 语法错误

It's good to know that the If statement in VBA has two legal syntaxes:很高兴知道 VBA 中的If语句有两种合法语法:

  1. Block syntax块语法

    That's If {bool-expression} Then , immediately followed by a NEWLINE.这就是If {bool-expression} Then ,紧接着是一个 NEWLINE。 To be legal, there needs to be an End If token further down, to close that block.为了合法,需要有一个End If令牌进一步向下,以关闭该块。 If you always consistently hit ENTER after typing Then , and immediately type End If and insert & indent one line of code in-between, you'll never again hit this syntax/compile error in your lifetime.如果您在输入Then后始终按 ENTER 并立即输入End If并在其间插入和缩进一行代码,那么您将永远不会再遇到此语法/编译错误。

  2. Inline syntax内联语法

    That's If {bool-expression} Then {expression} , which is what this comment is suggesting to do... by introducing a line continuation token {SPACE}_{NEWLINE} to have the {expression} part on a separate line... which makes it read/look like the block syntax if you're not careful.那就是If {bool-expression} Then {expression} ,这就是这条评论建议做的事情......通过引入一个行继续标记{SPACE}_{NEWLINE}{expression}部分放在单独的行上...... . 如果您不小心,它会使其读取/看起来像块语法。 It also makes it harder than necessary to later add more conditional expressions, because then you must add the End If token.这也使得以后添加更多条件表达式变得比必要更加困难,因为您必须添加End If标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM