VBA案例选择多个条件 [英] VBA Case Select Multiple Conditions

查看:72
本文介绍了VBA案例选择多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBA的新功能.我正在尝试建立一个Dimensions值(从excel电子表格中的两个不同单元格中提取,其中一个可能比另一个大,并且我总是想先使用较低的数字)在其中输出(将被串联的字符串)以及来自其他函数的字符串)可能是以下之一:

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:

4868(无x分隔整数值)48x60.5(其中x分隔整数和实数)36.5x60(其中x分隔实数和整数)24.75x72.125(其中x分隔一个实数和一个整数)

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)

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

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

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?

推荐答案

您的变量"h"不是布尔值.但是,您要在选择的情况下调用它,以匹配条件为true或false.

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"更改为选择大小写为true".其他一切都会好的.

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

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

这篇关于VBA案例选择多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆