使用 VSTO 应用百分比格式 - 在幕后乘以 100
我有一段根据要求格式化单元格的代码:
Range.Font.Italic = Microsoft.Office.Core.MsoTriState.msoTrue;
Range.HorizontalAlignment = XlHAlign.xlHAlignGeneral;
Range.NumberFormat = "0.0_%_);(0.0)_%;-_%_)";
然后调用此代码,然后按下自定义功能区上的按钮。它类似于百分比单元格格式。我必须添加的另一件事是将单元格值乘以 100。 例如,
- 单元格值设置为 0.15,我单击按钮,值更改为 15%。
- 单元格值设置为 2.5,我单击按钮,值更改为 250% 等。(与默认的 Excel Precentage 单元格样式非常相似)
但是,如果我执行以下操作:
decimal result = Convert.ToDecimal(cell.Value);
cell.Value = result * 100;
并且用户多次点击按钮,则值每次都会乘以。有没有办法指定显示格式之类的内容,以便保留实际值并且仅将显示值乘以 100?或者有另一种方法可以防止价值倍增?
I have a piece of code that formats cell according to requirements:
Range.Font.Italic = Microsoft.Office.Core.MsoTriState.msoTrue;
Range.HorizontalAlignment = XlHAlign.xlHAlignGeneral;
Range.NumberFormat = "0.0_%_);(0.0)_%;-_%_)";
And this code is invoked then button on custom ribbon is pressed. It's similar to percentage cell format. One more thing I have to add is multiplying the cell value by 100.
For instance
- cell value is set to 0.15, I click button and value changes to 15%.
- cell value is set to 2.5, I click button and value changes to 250% etc. (very similar to default Excel Precentage cell style)
However, if I do something like this:
decimal result = Convert.ToDecimal(cell.Value);
cell.Value = result * 100;
and user hits button multiple times, value is multiplied every time. Is there a way to specify something like display format, so that actual value is preserved and only displayed value is multiplied by 100? Or another way to prevent value from being multiplied multiple times?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,您不需要将其乘以 100
如果单元格有 0.15,则应用格式
Range.NumberFormat = "0.00%"
它将自动更改为 15.00 %,您无需将其乘以 100。
跟进
开箱即用的想法...为什么不通过将 % 的颜色设置为白色来隐藏 % 符号?
VBA 代码
快照
Well you don't need to multiply it by 100
If the cell has .15 then apply the formatting
Range.NumberFormat = "0.00%"
It will automatically change to 15.00 % and you don't need to multiply it by 100.
FOLLOW UP
An out of the box thinking... Why not hide the % symbol by setting the color of the % to white?
VBA CODE
SNAPSHOT
一种肮脏的方法是维护隐藏工作表,移动原始值,或在单击按钮时在隐藏工作表中维护该单元格的标志
A dirty way would be maintain a hidden sheet, Move original value, or maintain flag for that cell in hidden sheet on button click