如何应用自定义格式,例如“文本:” 0.0%在Excel VBA [英] How do I apply a custom format such as "Text:" 0.0% in Excel VBA

查看:135
本文介绍了如何应用自定义格式,例如“文本:” 0.0%在Excel VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Excel VBA根据用户控制编写一个或多个百分比。

I would like Excel VBA to write either a number or a percentage, depending upon a user control.

在Excel中,我可以按Ctrl 1应用自定义格式,例如文本:0.0%或文本:0.0。

In Excel, I can hit Ctrl 1 to apply a custom format such as "Text:" 0.0% or "Text:" 0.0.

我已经设法写入文本,然后使用Format()或Round(),具体取决于控件的值。但是,格式有两个小数位,我想要一个小数点。

I have managed to write the text and then use either Format() or Round(), depending upon the control value. However, Format has two decimal places, and I would like a single decimal place.

这是一些说明性的代码。如上所述,我想控制百分比的小数位数(例如0,1,2)。

Here is some illustrative code. As stated above, I would like to have control over the number of decimal places for the percentages (e.g. 0, 1, 2).

Select Case sControl
Case "Percentage"
  oRange = "Text: " & Format(dvalue + 0.000001, "Percent")
Case "Numeric"
  oRange = "Text: " & Round(dvalue + 0.000001, 1)
End Select

有关如何做的任何指针会很好。我在这两种情况下都添加了0.000001的dvalue,因为我明白这有助于保持传统的舍入精度。

Any pointers on how to do this would be great. I am adding 0.000001 to dvalue in both cases, as I understand that this helps to maintain traditional rounding accuracy.

Cheers
Tim

Cheers Tim

推荐答案

我认为这是你正在寻找的:

I think this is what you are looking for:

FormatPercent(1 + 0.000001, 1)

它以百分比格式化,第二个参数在这种情况下为1)是您想要的小数点数。

It formats as a percentage and the second argument (the 1 in this case) is the number of decimal points you want.

这是我发现的地方

这是格式化函数的一个非常好的资源。

这篇关于如何应用自定义格式,例如“文本:” 0.0%在Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆