VBA案例select多个条件

新的VBA。 我试图build立一个Dimensions的值(从一个Excel电子表格中的两个不同的单元格中拉出来,其中一个可能比另一个大,我总是希望先输出较低的数字),其中输出(一个string将被连接来自其他函数的string)可能是以下之一:

4868(不分隔整数值的x)48×60.5(x分隔整数和实数)36.5×60(x分隔实数和整数)24.75×72.125(x分隔实数和整数)

在VBA中将variablestypes定义为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 

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

只是为了完整性,你能find的结构最接近这种types的东西:

 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 

select案例不会像这样工作。 它将呈现的项目(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(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 

在select的情况下,你不能使用“和”运算符,而不得不使用逗号“,”

 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/

修正了我看到一些数字没有被正确处理的错误。 我错过了一个比较情景 – 应该是四个比较,而不是每个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