VBA案例选择多个条件

问题描述 投票:0回答:7

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

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

变量类型在 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

它将编译,但不会返回任何输出。 给什么?

vba excel
7个回答
8
投票

你的变量“h”不是布尔值。 但是,您在选择情况下调用它来匹配 true 或 false 的条件。

将“选择案例 h”更改为“选择案例 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

2
投票

选择大小写不是这样工作的。它将呈现的项目 (h) 与针对个别案例陈述计算的值进行比较。

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


1
投票

为了完整起见,您可以找到的最接近您正在寻找的结构的是这种类型的东西:

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

0
投票

试试这个:

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

0
投票

修复了我看到的一些数字未正确处理的错误。 我错过了一个比较场景 - 应该对每个 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

0
投票

在 Select 情况下,不能使用“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

请参阅下面的示例以更清楚地了解

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


0
投票

我想将 switch 语句与多个 or 条件一起使用。在这种情况下使用

,
即可。请参阅以下有效代码。

Dim value as String

'Get a value to use in switch case
expression = getValues(variable)

Select Case expression
'if the expression has value1 or value2
'Execute the below case statement

    Case "value1", "value2"
         Call firstSub(expression)

    Case "value3"
         Call secondSub()
          
End Select
© www.soinside.com 2019 - 2024. All rights reserved.