gpt4 book ai didi

VBA 案例选择多个条件

转载 作者:行者123 更新时间:2023-12-02 08:18:07 26 4
gpt4 key购买 nike

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

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

最佳答案

你的变量“h”不是 bool 值。但是,您在选择情况下调用它来匹配 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

关于VBA 案例选择多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21319788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com