Results 1 to 2 of 2

Thread: Excel Select Chart Index

  1. #1
    New Member
    Join Date
    Jun 12
    Posts
    8

    Excel Select Chart Index

    I have the following code:

    Code:
    Sub GetTLineEq()
      Dim chrtobj As ChartObject
      Dim chrt As Chart
      Dim srs As Series
      Dim tline As trendline
      Dim dlbl As DataLabel
      Dim str As String
    
      With ActiveSheet
        Set chrtobj = ActiveSheet.ChartObjects(1)
        Set chrt = chrtobj.Chart
        Set srs = chrt.SeriesCollection(1)
        Set tline = srs.Trendlines(1)
        Set dlbl = tline.DataLabel
        str = dlbl.Text
        .Range(Selection.Address).Value = str
      End With
    End Sub
    I would like the Macro to work with selected chart instead of ChartObjects(1). I have many charts in the spreadsheet. Many thanks.

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,522

    Re: Excel Select Chart Index

    it is easy enough to do, but have to make sure the chart is selected properly
    Code:
    if the typename(selection)= "ChartArea" then 
      set chrtobj = selection
      else 
      msgbox "chart not selected correctly"
    end if
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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