+ Reply to Thread
Results 1 to 13 of 13

VBA Chart Data Labels msoNumberFormat for Percentage with decimals

  1. #1
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    How do you tell msoChartFieldPercentage to have the Number Format "0,00%"?

    Please Login or Register  to view this content.
    How do you integrate this line to the code:
    msoNumberFormat = "0,00%"

  2. #2
    Forum Contributor
    Join Date
    09-03-2016
    Location
    Catania, Italy
    MS-Off Ver
    Excel 2019
    Posts
    164

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    HI
    I think it's better to attach the file.
    But why not publish the request in the Charts section?
    HI,
    Mario

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Hi Mario, Hi Andy,

    Andy, thank you for your response! I tried the line, but it changes the format of the Category Value.
    As Mario suggested, I have attached the file – could you please have a look at the code?

    Thank you very much!
    Toni
    Attached Files Attached Files

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Please Login or Register  to view this content.
    You did not mention the data labels where compound labels.

  6. #6
    Forum Contributor
    Join Date
    09-03-2016
    Location
    Catania, Italy
    MS-Off Ver
    Excel 2019
    Posts
    164

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Thanks Andy
    I allowed myself to make only two changes (the ones colored in red below) but the percentages seem wrong to me. Only in the first label I get 100.00% while the others are all 0.00%

    Sub xx()
    Dim p As Point
    Dim CatValueLength As Variant
    Dim dls As DataLabels
    Dim length As Long
    Dim labelItems As Variant

    ActiveSheet.ChartObjects(1).Activate
    With ActiveChart.SeriesCollection(1)
    .HasDataLabels = True
    With .DataLabels
    .ShowValue = True
    .ShowCategoryName = True
    .ShowPercentage = True
    .Separator = vbLf
    .Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = vbBlack
    .Format.TextFrame2.TextRange.Font.Bold = False
    .NumberFormat = "0.0000"
    .Position = xlLabelPositionOutsideEnd
    End With
    For Each p In .Points
    labelItems = Split(p.DataLabel.Text, vbLf)
    With p.DataLabel.Format.TextFrame2.TextRange
    .Text = labelItems(0)
    .Text = .Text & vbLf & Format(labelItems(1), "0.00")
    .Text = .Text & vbLf & Format(labelItems(2), "0.00%")
    '~~> Color the category name
    length = Len(labelItems(0)) + Len(labelItems(1))
    .Characters(1, length).Font.Bold = True
    .Characters(1, length).Font.Fill.ForeColor.RGB = p.Format.Fill.ForeColor.RGB
    End With
    Next
    End With
    End Sub

    Did I do something wrong?

    HI,
    Mario

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    I get percentages as expected.

    Are you using regional settings? In which case perhaps the values have a , instead of period for decimal place. If so you will need to replace the comma with a period in labelItems(1) and lableItems(2) before formatting as number
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    09-03-2016
    Location
    Catania, Italy
    MS-Off Ver
    Excel 2019
    Posts
    164

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Hi

    No, I'm using system separators.
    But what is not clear to me is why it sets the first percentage equal to 100.00 and the others all to 0.00

    HI,
    Mario

  9. #9
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Hi Andy,

    stunning!

    So compound labels they are. So much to learn.

    Mario, as Andy says, it seems to be something regional.
    Try to replace the "." with a "," (.NumberFormat = "0,0000"), this worked for me.

    Andy, if I may, two questions:

    1.
    I added these lines to the end, as an option to have all three label lines in the same color:

    .Characters(8, length).Font.Bold = True ' by Toni
    .Characters(8, length).Font.Fill.ForeColor.RGB = p.Format.Fill.ForeColor.RGB ' by Toni
    .Characters(12, length).Font.Bold = False ' by Toni
    .Characters(12, length).Font.Fill.ForeColor.RGB = p.Format.Fill.ForeColor.RGB ' by Toni

    The first two lines work as expected, the last two don't. I can only go as far as "12". "13" is already an "invalid procedure".
    I also tried Mario's red "+Len(labelItems(1))". This does the job of formatting the value like the category.
    But unfortunately, it also formats the first two digits of the percentage?
    What am I missing?

    2.
    The "original" code had a line, pointing to the cells the formatting should follow.
    I could easily choose, whether to follow the category font colors or the value font colors:

    c = ActiveSheet.Cells(1, i + 1).Font.Color
    or
    c = ActiveSheet.Cells(2, i + 1).Font.Color

    But I can't work out, where to make that switch in your code.
    Would you please be so kind, to point at it?

    Thank you so much!
    Last edited by briskie; 03-19-2024 at 08:46 AM.

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Load the data label with text first, then format the bits of text

    Please Login or Register  to view this content.
    You need to include the length of vbLF, which is the new line.

  11. #11
    Forum Contributor
    Join Date
    09-03-2016
    Location
    Catania, Italy
    MS-Off Ver
    Excel 2019
    Posts
    164

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Hello everyone
    Never give up!
    The previous macro works if in the first formatting I put a comma and in the other two I put a point.
    I mistakenly always put either full points or commas.
    Thank you.

    HI, Mario

  12. #12
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Hello Andy, hello Mario.

    Andy, wow. This is so neat. Thank you so much!

    Mario, same case here with the dots and commas.

    What a life changer

    Thank you very, very much!

  13. #13
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Chart Data Labels msoNumberFormat for Percentage with decimals

    Hello Andy,

    might there also be a way to use cells in a column (instead of a row)?

    In your code, these lines point to the cell in a row to be used:

    categoryColorRow = 1
    valueColorRow = 2
    colIndex = 2

    Is there a way to choose a column instead?
    The code I was using up until now had a range setting, where you could either choose a horizontal or vertical row as the input for the labels:

    Please Login or Register  to view this content.
    Once again, I'm trying to combine with no success.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA Chart Data Labels - Different Formatting for Value and Percentage
    By briskie in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-17-2024, 04:54 PM
  2. Replies: 0
    Last Post: 05-13-2019, 08:08 PM
  3. [SOLVED] Assign a value to a percentage with decimals
    By rhybeka in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-18-2018, 03:07 PM
  4. Chart data Label decimals reflecting the data source
    By andgramar in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 07-22-2017, 05:49 AM
  5. Pivot chart stacked, axis in values labels in percentage
    By rpinxt in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 11-01-2016, 03:42 PM
  6. [SOLVED] Multiplying with percentage having decimals not working.
    By smartbuyer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-17-2013, 06:17 AM
  7. Convert numbers with decimals to percentage
    By Wskip49 in forum Excel General
    Replies: 2
    Last Post: 11-11-2008, 06:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1