VBA多条件选择:使用Case语句

6

刚学习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

它可以编译,但不会返回任何输出。出了什么问题?

糟糕,刚意识到最后一个 case 语句是实数和实数。脑子抽了一下... - user3229658
那么...你的问题解决了吗?如果是的话,请删除帖子或自己回答(请参见屏幕底部的按钮)。 - Max Leske
还在调试中......它适用于大多数情况,但不是所有情况。 - user3229658
6个回答

7

您的变量'h'不是布尔类型。然而,您正在调用它来匹配条件,这些条件要么为true,要么为false。

将您的“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

2

Select Case不能像这样使用。它将呈现的项目(h)与各个case语句计算出的值进行比较。

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


if/then 语法使我离目标更进一步了。请参阅上面有关小数点后数字(特别是零)处理的注释。 - user3229658

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

3
如上所述,在VBA中不能使用"And"与Case语句。 - variant
@variant 这并不是完全准确的,经过一些测试,我发现“and”可以始终使用,除非其右侧是一个“is”语句。 - 6diegodiego9

0
在选择语句中,您不能使用 "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/


4
这是错误的:在select语句中,逗号表示“或”而不是“且”。 - 6diegodiego9
1
这个答案非常错误。在Select Case中,您可以使用“And”,逗号不等同于“And”。它的作用就像您在之后有一个相同代码的额外Case一样(它有点像“Or”)。请查看https://excelmacromastery.com/vba-select-case获取更多详细信息。 - Wildhorn

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

很抱歉这么晚才回复你关于这个方案是否可行的问题。我手头有六件事情要处理,而这个(虽然对我很重要)并不是老板所关注的,所以它被放在了优先级列表的最底部。但是,咚咚咚,它适用于大多数情况。不幸的是,它并不适用于其他情况。具有讽刺意味的是,似乎没有任何规律。请帮我解释一下带有%的变量的定义? - user3229658
它似乎在从我的电子表格定义变量时出现了问题,其中所有小数点后面都是零。换句话说,48.50000被准确地截断为48.5;然而,48.00000根本没有产生任何结果。 - user3229658
使用round函数来去除零,例如getDimDisplay = CStr(round(h,10)) & "x" & CStr(round(w,10))dim iH%dim iH as Integer完全相同,只是缩写版本。在此处阅读更多信息:http://support.microsoft.com/kb/191713。 - PPh
我在其他情况下确实使用了round函数,但由于这里没有容错的余地,所以不适合使用。我的问题与具有正确数量的比较选项有关。在我的原始示例中,我有四种不同的场景,但应该有八种。一旦我解决了这个问题,世界就恢复了平静。 :) - user3229658

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接