Consulting

Results 1 to 6 of 6

Thread: Display percent value

  1. #1

    Display percent value

    The code below displays values in decimal format. (Ex: 0.25) How do I get it to display as a percent value. (Ex: 25%)

    Select Case Len(Me.MPct)
        Case 1
            Me.PPct = Format(Me.PPct, "0\.00")
        Case Is > 1
            v = Replace(Me.PPct, ".", "")
            Me.PPct = Format(Val(v), "0\.00")
    End Select

  2. #2
    Hi ,

    Can't test the following without having the form it relates to but see how this goes:

    Select Case Len(Me.MPct)
            Case 1
                Me.PPct = Format(Me.PPct, "0\.00%")
            Case Is > 1
                v = Replace(Me.PPct, ".", "")
                Me.PPct = Format(Val(v), "0\.00%")
        End Select
    HTH

    Robert

  3. #3
    didn't work. I attached an example. whenever I type a number in the textbox, it errors: Overflow

  4. #4
    It's because it's getting stuck in what seems to be endless loop i.e. each time the format of the field is changed the code fires off again.
    See if the attached suffices.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    More simply
    [VBA]
    Private Sub PPct_AfterUpdate()
    Me.PPct = Format(Me.PPct, "0.00%")
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    I wanted to provide a solution to my original request for future references in case any one else needed a similar solution. This code was provided by my good friend Ger Plante.

    Private Sub PPct_Change()
    Dim v As String
     
    v = Replace(Me.PPct, "%", "")
    Me.PPct = Val(v) & "%"
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •