vba 选择带字符串的大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25141862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:05:51  来源:igfitidea点击:

Select Case with String

excelvbaexcel-vba

提问by Johaen

I am getting frustrated with using Select Case and a string to compare.

我对使用 Select Case 和一个字符串进行比较感到沮丧。

I just need to check if somebody makes a comment or just go on by scanning different objects.

我只需要检查是否有人发表评论或只是通过扫描不同的对象继续。

So I read in the statement and check if there is a part number coming in (always starting with a N) or if not so I know where to go next.

所以我阅读了声明并检查是否有零件编号(总是以 N 开头),如果没有,我知道下一步该去哪里。

Since there are three cases that could happen next I cannot take a if temp = Left(temp, 1) = "N".

由于接下来可能会发生三种情况,因此我无法将if temp = Left(temp, 1) = "N".

Where is my mistake?

我的错误在哪里?

            temp = Application.InputBox(Prompt:="Would you like to make a comment?" & vbCrLf & "If not please continue with next scan. ", Title:="Comment or go on", Type:=2)
            Select Case temp

                Case Is = Left(temp, 1) = "N"
                'New scan - next number got scanned
                    MsgBox ("Controlmessage - N Case")
                    i = i + 1
                    .Cells(i, "D").Select
                    temp = ""

            End Select

回答by

Try this

尝试这个

Sub Main()

    temp = Application.InputBox(Prompt:="Would you like to make a comment?" & vbCrLf & "If not please continue with next scan. ", Title:="Comment or go on", Type:=2)

    Select Case UCase(Left(temp, 1))
        Case "N"
            MsgBox "N"
        Case "C"
            MsgBox "C"
        Case Else
            MsgBox "something else"
    End Select

End Sub