VBA 对于范围格式为百分比的每个单元格

VBA For Each cell In Range Format as Percentage

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

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

我认为这是因为 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%"