Pages

Saturday, November 22, 2008

Convert Decimal to Percentage using VBA Format Function

Convert Decimal to Percentage using VBA FormatPercent Function

Here is a simple example to convert a decimal to percentage using VBA function

Sub Convert_Decimal2Percentage()


Dim dblSuccess As Double
Dim sSuccess As String

dblSuccess = 0.456345

sSuccess = FormatPercent(dblSuccess, 2)

sSuccess = Format(Expression:=dblSuccess, Format:="Percent")


End Sub

6 comments:

  1. Anonymous9:13 AM

    Thanks, this is quite useful.

    ReplyDelete
  2. Anonymous8:34 AM

    So easy, when you find the correct instruction :)

    ReplyDelete
  3. Anonymous6:36 AM

    thanks... that was very helpful... but how if you want the reverse of it? I mean, to convert percentage into decimal?

    ReplyDelete
  4. Anonymous10:29 AM

    divide by 100...

    ReplyDelete
  5. Ummmm I'm lost, can you give an example, will this code go into the TextBox that you want to change the decimals on? For example I have an amount in a sheet that I view on a UserForm via a TextBox, do I just add this code to it? Shouldn't it be like a AfterUpdate() or a Change() Sub? All I want is for the TextBox to show the percentage as 16% but no matter how I Format it it always displays as 0.16000%. I have tried TextBox1= Format(TextBox1, "Percent") also TextBox1= Format(TextBox1, "##.#0%") and nothing.

    ReplyDelete
    Replies
    1. Anonymous4:55 PM

      sSuccess= FormatPercent(dblSuccess, -1)
      sSuccess = Format(Expression:=dblSucess, Format:="#0%; (#0%)")

      Delete