Excel VBA - 舍入函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9698743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:30:39  来源:igfitidea点击:

Excel VBA - Round function

vbaexcel-vbaexcel

提问by CustomX

I want to create a macro to edit the price list my faulty ERP packet generates. I currently get 175,42$, etc and I would like to round them to 175,4$ (depending on the second decimal).

我想创建一个宏来编辑我的错误 ERP 数据包生成的价目表。我目前得到 175,42$ 等,我想将它们四舍五入为 175,4$(取决于第二位小数)。

  J           K
175,42      175,4 
193,76      193,8

dim i as integer
For i = 16 to 25
   Range("K" & i).Select = "=ROUND(J & i;1)"
Next i

So I presume this should run through K16:K25 and use the round function, but it seems to give an error and I'm guessing it's the round function.

所以我认为这应该通过 K16:K25 并使用 round 函数,但它似乎给出了一个错误,我猜它是 round 函数。

回答by chris neilsen

Change your code to

将您的代码更改为

Range("K" & i).Formula = "=ROUND(J" & i & ";1)"

回答by mattboy

Try this instead. VBA has its own function for Round.

试试这个。VBA 有自己的 Round 函数。

Now tested and working for me!

现在经过测试并对我来说有效!

dim i as integer
For i = 16 to 25
   Range("K" & i) = Round(Range("J" & i), 1) 
Next i

回答by Siddharth Rout

Try this (UNTESTED)

试试这个(未经测试

Sub Sample()
    Dim i As Integer
    For i = 16 To 25
       Sheets("Sheet1").Range("K" & i).Formula = "=ROUND(J" & i & ",1)"
    Next i
End Sub