VBA最小和最大功能总是返回0
您好,我试图从数组中获取最小值和最大值,尽管如此,它总是返回“ 0”。我的代码:
Dim MachineCapacitySmallestArray() As Variant
MachineCapacitySmallestArray = thisworkbook.worksheets(1).range("C25:D25")
SmallestCapacity = Application.Min(MachineCapacitySmallestArray)
在范围内,我有自然数量
,我尝试将这些单元格格式化为数字等。但是没有任何作用。我犯了什么错误以及如何解决?
Hello I am trying to get the MIN and MAX values from the array and it always returns "0" despite anything. My code:
Dim MachineCapacitySmallestArray() As Variant
MachineCapacitySmallestArray = thisworkbook.worksheets(1).range("C25:D25")
SmallestCapacity = Application.Min(MachineCapacitySmallestArray)
in range I have natural numbers
I tried formatting those cells to numbers etc. but nothing works. What is the mistake I'm making and how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论,看来您的问题是您的数据,您的单元格中可能有字符串,而不是
已经提到的数字(也许是某种程度上导入?),更改单元格格式不会更改单元格的内容,它只是定义如何显示数据。数字
3.14
可以显示为3,3.140000,00003.14或3.14e+00,没有任何变化的值。但是,字符串'3.14
是字符的组合3
,。,1
和4
,与数字无关。在之后设置单元格式该值在单元格中不会将其转换为数字。如果您将数据读取到VBA中,VBA将获得Excel的确切值,在您的情况下,您必须手动将其转换为数字,例如使用以下例程。如果单元格不包含可以转换为数字的东西,则
on错误简历下一个
将防止类型不匹配。现在,只需将此例程添加到您的代码中即可。如果您也想在Excel中使用数字,请从上一个语句中删除注释符号。
According to the comments, it seems that your problem is your data, you have likely strings in your cell, not numbers (maybe somehow imported?)
As already mentioned, changing the cell format doesn't change the content of a cell, it just defines how to display data. The number
3.14
can be displayed as 3, as 3.140000, as 00003.14 or as 3.14E+00, nothing changes it's value. However, a String'3.14
is a combination of the characters3
,.
,1
and4
and has nothing to do with a number. Setting a cell format after the value is in the cell will not convert it to a number.If you read your data into VBA, VBA will get the exact values from Excel and in your case, you will have to convert it into numbers manually, for example with the following routine. The
On Error Resume Next
will prevent a type mismatch if a cell doesn't contain something that can be converted into a number.Now just add a call to this routine to your code. If you want to have the numbers also in Excel, remove the comment sign from the last statement.