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

ano*_*ous 5 vba loops excel-vba percentage number-formatting

我环顾四周看似这么简单,但我无法让它发挥作用.

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

我认为这是因为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
Run Code Online (Sandbox Code Playgroud)

小智 12

尝试更换.NumberFormat='Percent'.NumberFormat="0.00%"

  • 我使用了`.NumberFormat =“ 0%”`,因为我不想使用其他小数。仍然很棒! (2认同)