查找最多3个输入VBA [英] Finding Max of 3 Inputs VBA

查看:157
本文介绍了查找最多3个输入VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到最多3个输入。问题不在算法中,因为当我在python中编写相同的脚本时,它的工作正常。问题是它不按预期工作。我会写一些情景,结果如何:



8 5 12 - 最大:12

5 8 12 - 最大:12

12 5 8 - 最高:8

12 8 5 - 最大:8

5 12 8 - 最大:8

8 12 5 - 最大值:8

100 22 33 - 最大:33

22 3 100 - 最大:100

100 22 3 - 最大:22

似乎它适用于相当的组合,但不适用于每一个。我还没有找到一个模式,我无法确定出了什么问题。



我附上了代码:

  Sub Maxthree()
'计算三个数字的最大值
Dim x,y,z As Single
x = InputBox(输入第一个数字!)
y = InputBox(输入第二个数字!)
z = InputBox(输入第三个数字!)
MsgBox(X: & x&Y:& y&Z:& z)
如果x> y然后
如果x> z然后
MsgBox(最大值为& x)
Else
MsgBox(最大值为& z)
End If
Else
如果y> z然后
MsgBox(最大值为& y)
Else
MsgBox(最大值为& z)
End If
End如果
End Sub


解决方案

因为它们是输入使用InputBox,它是比较文本值。因此,例如8大于12。相反,请尝试转换为 Longs ,如:

  x = CLng(InputBox 输入第一个数字!))

您还可以将代码简化为:

  MsgBox WorksheetFunction.Max(x,y,z)


I am trying to find the maximum of 3 inputs. The problem is not in the algorithm, as when I made the same script in python it worked just fine. The problem is that it does not work as expected. I will write some scenarios and what the outcome was:

8 5 12 - Max: 12
5 8 12 - Max: 12
12 5 8 - Max: 8
12 8 5 - Max: 8
5 12 8 - Max: 8
8 12 5 - Max: 8
100 22 33 - Max: 33
22 3 100 - Max: 100
100 22 3 - Max: 22

It seems that it works for quite some combination, but not for each and every one. I haven't managed to find a pattern yet, and I can't figure out what is going wrong.

I am attaching the code:

Sub Maxthree()
'Calculates the maximum of three numbers'
Dim x, y, z As Single
x = InputBox("Enter the first number!")
y = InputBox("Enter the second number!")
z = InputBox("Enter the third number!")
MsgBox ("X: " & x & " Y: " & y & " Z: " & z)
If x > y Then
    If x > z Then
        MsgBox ("the maximum is : " & x)
    Else
        MsgBox ("the maximum is : " & z)
    End If
Else
    If y > z Then
        MsgBox ("the maximum is : " & y)
    Else
        MsgBox ("the maximum is : " & z)
    End If
End If
End Sub

解决方案

Because they are input using an InputBox, it's comparing text values. So, for example "8" is greater than "12". Instead try converting to Longs like:

x = CLng(InputBox("Enter the first number!"))

You can also simplify your code to:

MsgBox WorksheetFunction.Max(x, y, z)

这篇关于查找最多3个输入VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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