【发布时间】:2019-10-18 15:32:31
【问题描述】:
Sub FormatThreeDecimals()
Dim ws As Worksheet
Dim rng As Range
Dim lastCell
Set ws = Worksheets("Summary")
lastCell = Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Select
Set rng = [B4:LastCell]
Range("rng").NumberFormat = "0.000"
End Sub
我想用变量 rng 定义范围 B4:lastCell,然后对其应用“0.000”数字格式。我在正确设置范围时遇到问题。
编辑:我收到运行时错误 '424': Oject required when my code hit the line Set rng = [B4:LastCell]。
【问题讨论】:
-
Range(rng)不是"rng"因为它是你的变量 -
我已经切换了那部分,谢谢。我仍然对设置 rng = [B4:LastCell] 的部分有疑问,我认为这是不正确的方法或语法。
-
使用
[]意味着评估。你需要写Range("B4:B" & cells(4,2).end(xldown).row)或类似的东西 -
所以这种方法会截断数字,我该如何调整它以四舍五入到小数点后两位?
-
我添加了 ActiveWorkbook.PrecisionAsDisplayed = True ,现在存储的值与显示的值匹配,但我怀疑这是四舍五入还是进一步截断?