我正在用 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/