VBA 案例选择多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21319788/
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-08 17:37:04  来源:igfitidea点击:

VBA Case Select Multiple Conditions

excelvbaexcel-vba

提问by user3229658

New to VBA. I'm attempting to build a value of Dimensions (pulling from two different cells in an excel spreadsheet in which one might be larger than the other, and I always want the lower number first) in which the output (a string which will be concatenated with strings from other functions) might be one of the following:

VBA 新手。我正在尝试构建 Dimensions 的值(从 Excel 电子表格中的两个不同单元格中提取,其中一个可能大于另一个,并且我总是希望先使用较低的数字),其中输出(将被连接的字符串)来自其他函数的字符串)可能是以下之一:

4868 (no x separating the integer values) 48x60.5 (with x separating an integer and real number) 36.5x60 (with x separating a real number and an integer) 24.75x72.125 (with x separating a real number and an integer)

4868(没有 x 分隔整数值) 48x60.5(x 分隔整数和实数) 36.5x60(x 分隔实数和整数) 24.75x72.125(x 分隔实数和整数)

Variable types are defined in VBA as Single (not Double). Here's my code:

变量类型在 VBA 中定义为 Single(而不是 Double)。这是我的代码:

Function getDimDisplay(h As Single, w As Single) As String

Dim strResult As String
Dim iH As Integer
Dim iW As Integer
Dim strH As Variant
Dim strW As Variant

iH = CInt(h)
iW = CInt(w)

Select Case h
    Case (h >= w And iH = h And iW = w)
        strH = CStr(iH)
        strW = CStr(iW)
        strResult = strW & strH
    Case (h >= w And iH <> h And iW = w)
        strH = CStr(h)
        strW = CStr(iW)
        strResult = strW & "x" & strH
    Case (w >= h And iH = h And iW <> w)
        strH = CStr(iH)
        strW = CStr(w)
        strResult = strH & "x" & strW
    Case (w >= h And iH <> h And iW <> w)
        strH = CStr(h)
        strW = CStr(w)
        strResult = strH & "x" & strW
End Select

getDimDisplay = strResult

End Function

It will compile, but it won't return any output. What gives?

它会编译,但不会返回任何输出。是什么赋予了?

回答by Rajathithan Rajasekar

In Select case, you can't use "and" operator, instead you have to use a comma ","

在 Select case 中,您不能使用“and”运算符,而必须使用逗号“,”

Select Case h
Case Is >= w , Is = iH
    If w = iW Then
    '   do stuff
    Else
    '   do other stuff
    End If
Case Is <= w , Is = iH
    If w <> iW Then
    '   do stuff
    End If
Case Is > -w , Is <> iH
    If w <> iW Then
    '   do stuff
    End If
End Select

Please see the below example for more clarity

请参阅以下示例以获得更清晰的信息

http://gadoth.com/excel-vba-series-post-9-select-case/

http://gadoth.com/excel-vba-series-post-9-select-case/

回答by Michael James

your variable 'h' is not a boolean. However, you're calling it in select case to match conditions which are either true or false.

您的变量 'h' 不是布尔值。但是,您在 select case 中调用它以匹配真或假的条件。

Change your "select case h" to "select case true". all else will work ok.

将“select case h”更改为“select case true”。一切正常。

Select Case True

Case (h >= w And iH = h And iW = w)
    strH = CStr(iH)
    strW = CStr(iW)
    strResult = strW & strH
Case (h >= w And iH <> h And iW = w)
    strH = CStr(h)
    strW = CStr(iW)
    strResult = strW & "x" & strH
Case (w >= h And iH = h And iW <> w)
    strH = CStr(iH)
    strW = CStr(w)
    strResult = strH & "x" & strW
Case (w >= h And iH <> h And iW <> w)
    strH = CStr(h)
    strW = CStr(w)
    strResult = strH & "x" & strW

End Select

回答by simon at rcl

Select Case doesn't work like this. It compares the item presented (h) to the values calculated for the individual case statements.

Select Case 不能像这样工作。它将呈现的项目 (h) 与为单个案例陈述计算的值进行比较。

The case statements you have all evaluate to a bool, true or fasle. Whatever h equals, it's not that! For this bit of code, you nedd an if then else if structure.

您所有的 case 语句都评估为 bool、true 或 fasle。无论 h 等于什么,都不是这样!对于这段代码,您需要一个 if then else if 结构。

回答by PPh

try this:

尝试这个:

Function getDimDisplay(h As Single, w As Single) As String
Dim iH%:    iH = CInt(h)
Dim iW%:    iW = CInt(w)

If h >= w And iH = h And iW = w Then
    getDimDisplay = CStr(iW) & CStr(iH)
Else
    If h >= w And iH <> h And iW = w Then
        getDimDisplay = CStr(iW) & "x" & CStr(h)
    Else
        If w >= h And iH = h And iW <> w Then
            getDimDisplay = CStr(iH) & "x" & CStr(w)
        Else
            If w >= h And iH <> h And iW <> w Then
                getDimDisplay = CStr(h) & "x" & CStr(w)
            End If
        End If
    End If
End If
End Function

回答by user3229658

Fixed the error I was seeing with some numbers not being handled correctly. I was missing a comparison scenario - should have been four comparisons to make instead of three for each h>=w or w>=h situation. Yay! Thanks folks! Here's the working code:

修复了我看到的一些数字未正确处理的错误。我错过了一个比较场景 - 应该对每个 h>=w 或 w>=h 情况进行四次比较而不是三个比较。好极了!谢谢各位!这是工作代码:

Function getDimDisplay(h As Single, w As Single) As String

Dim iH%:    iH = CInt(h)
Dim iW%:    iW = CInt(w)

If h >= w And iH = h And iW = w Then
    getDimDisplay = CStr(w) & CStr(h)
Else
    If h >= w And iH <> h And iW = w Then
        getDimDisplay = CStr(w) & "x" & CStr(iH)
    Else
        If h >= w And iH = h And iW <> w Then
            getDimDisplay = CStr(w) & "x" & CStr(iH)
        Else
            If h >= w And iH <> h And iW <> w Then
                getDimDisplay = CStr(w) & "x" & CStr(h)
            Else
                If w >= h And iH = h And iW = w Then
                    getDimDisplay = CStr(iH) & CStr(iW)
                Else
                    If w >= h And iH <> h And iW = w Then
                        getDimDisplay = CStr(h) & "x" & CStr(iW)
                    Else
                        If w >= h And iH = h And iW <> w Then
                            getDimDisplay = CStr(iH) & "x" & CStr(w)
                        Else
                            If w >= h And iH <> h And iW <> w Then
                                getDimDisplay = CStr(h) & "x" & CStr(w)
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If
End If    
End Function

回答by Cool Blue

Just for completeness, the closest you can get to the structure youre looking for is this type of thing:

为了完整起见,最接近您要查找的结构的是这种类型的东西:

Select Case h
Case Is >= w And Is = iH
    If w = iW Then
    '   do stuff
    Else
    '   do other stuff
    End If
Case Is <= w And Is = iH
    If w <> iW Then
    '   do stuff
    End If
Case Is > -w And Is <> iH
    If w <> iW Then
    '   do stuff
    End If
End Select