VBA案例选择多个条件

时间:2014-01-23 21:33:20

标签: excel vba excel-vba

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

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

6 个答案:

答案 0 :(得分:3)

在选择案例中,您无法使用&#34;和&#34;运算符,而你必须使用逗号&#34;,&#34;

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/

答案 1 :(得分:1)

您的变量“ 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 :(得分:0)

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

你所有的案例陈述都评价为bool,true或fasle。无论什么等于,它不是那个!对于这段代码,你需要一个if if else else if。

答案 3 :(得分: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

答案 4 :(得分:0)

修正了我看到的错误,其中一些数字未正确处理。我错过了一个比较场景 - 应该进行四次比较而不是每次h&gt; = w或w&gt; = 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

答案 5 :(得分:0)

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

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