excel - 更改 VBA 中的单元格数据类型?

标签 excel vba math

我正在用 Excel 编写一个应用程序,帮助您学习完成平方过程。如果变量是整数,我希望将单元格格式设置为数字,如果不是整数,则将单元格格式设置为分数。

If h = (Int(h)) Then
Range("D1").Select
Selection.NumberFormat = "Number"
Else
Range("D1").Select
Selection.NumberFormat = "?/?"
End If

此代码由于某种原因不起作用,给出以下错误:

Unable to set the NumberFormat property of the Range class

还有其他方法可以做到这一点吗?

最佳答案

尝试:

If h = (Int(h)) Then
Range("D1").Select
Selection.NumberFormat = "0"
Else
Range("D1").Select
Selection.NumberFormat = "# ?/?"
End If

但是,如果将整数格式化为分数,它仍然会显示为整数(即​​“1”作为分数仍然是“1”)。那么为什么不直接将其格式化为分数呢?

关于excel - 更改 VBA 中的单元格数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037765/

相关文章:

java - 如何在 Android 的 XML 编辑器中将特殊字符作为文本插入?

Java 到 C# - BigInt.modPow

excel - 从工作表代码中引用添加全局变量

excel - VBA 是否可以在单元格的开头获取 ' 字符?

excel - 在excel中运行宏

excel - 比较多维数组中的元素

vba - 在同一 Outlook 对话下使用 VBA 发送电子邮件

algorithm - 我可以确定性地对任意排列的 float 的向量求和吗?

java - 如何使用java在Excel中将特定单元格设置为边框形式

vba - 为什么这条线没有抛出错误: str strVariable = "text"