vba 使用宏将excel中的单元格格式化为小数位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12962656/
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-08 14:13:47  来源:igfitidea点击:

Format cells in excel for decimal places using a macro

excelexcel-vbavba

提问by TankTank

How can I format Cell (D:) 2 decimals places Cell (E:) 1 decimal place and Cell (H:)3 decimals places in excel using a macro?

如何使用宏在excel中格式化单元格(D:)2个小数位单元格(E:)1个小数位和单元格(H:)3个小数位?

回答by scott

the numberformat property of a range will format it to any custom format

范围的 numberformat 属性将其格式化为任何自定义格式

  sheets("sheet1").range("D:D").numberformat = "0.00"
  sheets("sheet1").range("E:E").numberformat = "0.0"
  sheets("sheet1").range("H:H").numberformat = "0.000"

for numbers with a comma or currency displayed this would work:

对于显示逗号或货币的数字,这将起作用:

   sheets("sheet1").range("D:D").numberformat = "&#,##0"