Results 1 to 5 of 5

Thread: [SOLVED] EXCEL how to change the NAME of a specific Chart

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [SOLVED] EXCEL how to change the NAME of a specific Chart

    How is it possible to change the name of a specific existing Chart in EXCEL using VBA?

    The code should be
    VB Code:
    1. Charts(x).Name="BlaBla"
    My problem is how can I determine the x (i.e. the Number of the Chart-Object)? Since the code could be used several times the number will change.
    To make it easy, the setting of the name could be done wwhen creating the chart (or just after the " Chart.add").

    Thanks
    Last edited by opus; Sep 25th, 2004 at 04:35 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Hi Opus, you could check this by looping through each chart in the collection & evaluating the name there ...
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Dim intLoopCounter As Integer
    3.    
    4.     For intLoopCounter = 1 To Workbooks(1).Charts.Count
    5.         If (Charts(intLoopCounter).Name = "Chart1") Then
    6.             Charts(intLoopCounter).Name = "Cartman"
    7.         End If
    8.     Next
    9. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Or you can use this method of doing the same thing - I've just checked this out & certainly in Excel 2000, the argument you pass to the charts collection can either be a string representing the name of a specific chart, or it can be a number index of the chart within the collection...
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Workbooks(1).Charts("Chart1").Name = "Cartman"
    3. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Finally, to set this property via code when you create the cart, you can use this one:
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.    Workbooks(1).Charts.Add
    3.     With ActiveChart
    4.         .ChartType = xlColumnClustered
    5.         .SetSourceData Source:=Sheets("Sheet1").Range("A1:A5"), _
    6.         PlotBy:=xlColumns
    7.         .HasLegend = True
    8.         .Legend.Select
    9.         .Legend.Position = xlRight
    10.         [b].Name = "Cartman"[/b]
    11.     End With
    12. End Sub
    Hope this helps!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Thanks Alex
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

Posting Permissions

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



Click Here to Expand Forum to Full Width