VBA For Each cell In Range Format as Percentage

anonymous picture anonymous · Mar 16, 2017 · Viewed 61.3k times · Source

I've looked around for this and it seems so simple, yet I can't get it to work.

I have a table and one column needs to be formatted as a percentage. Below is my code but it is not formatting the cells, it just leaves them as the decimal.

I think this is because cell, even though declared as a range, is actually the value of the cell so I don't know how to refer to that range.

My returnRebate variable is declared as a range and the loop is looping through the correct range.

Code:

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

Answer

RyanL picture RyanL · Mar 16, 2017

Try replacing .NumberFormat='Percent' with .NumberFormat="0.00%"