【问题标题】:VBA For Each cell In Range Format as Percentage范围格式中每个单元格的 VBA 百分比
【发布时间】:2017-08-08 06:12:56
【问题描述】:

我四处寻找这个,它看起来很简单,但我无法让它工作。

我有一个表格,其中一列需要格式化为百分比。下面是我的代码,但它没有格式化单元格,它只是将它们保留为小数。

我认为这是因为cell,即使声明为范围,实际上是单元格的值,所以我不知道如何引用该范围。

我的returnRebate 变量被声明为一个范围,并且循环正在循环正确的范围。

代码:

Dim cell As Range, p As Double
For Each cell In returnRebate
    p = cell.Value

    If p <> 0 And p > 1 Then
        p = p * 0.01
        cell.Value = p
        cell.NumberFormat = "Percent"    'Not formatting here
     ElseIf p < 1 And p > 0 Then
        'do nothing to it
    Else
        cell.Value = vbNullString
    End If
Next

【问题讨论】:

  • 尝试将 .NumberFormat='Percent' 替换为 .NumberFormat="0.00%"
  • @RyanL 天哪,我知道这很简单!我以为我已经尝试过了,但我想没有

标签: vba excel loops number-formatting percentage


【解决方案1】:

尝试将.NumberFormat='Percent' 替换为.NumberFormat="0.00%"

【讨论】:

  • 我使用了.NumberFormat="0%",因为我不想要额外的小数。仍然很好用!
  • @cheshire 刚用 Office Prof 2013 试过,小数点没问题,FWIW。也许版本相关(?)
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多