2017-03-16 225 views
1

我环顾四周,这似乎很简单,但我无法让它工作。VBA对于每个单元格范围格式为百分比

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

我认为这是因为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 
+2

更换.NumberFormat='Percent'尝试.NumberFormat更换.NumberFormat = '百分'= “0.00%” – RyanL

+0

@RyanL OMG我知道这很简单!我以为我曾试过,但我猜不是 – cheshire

回答

6

尝试用.NumberFormat="0.00%"

+1

我使用'.NumberFormat =“0%”'因为我不想额外的小数。仍然很好! – cheshire

+1

@cheshire刚刚尝试过Office Prof 2013,并没有问题,小数点,FWIW。也许版本依赖(?) – Levon

相关问题