4

I am trying to create charts in excel using VBA . The code is as follows :

Sub testmacro()    
    Dim i As Integer     
    i = Sheets("Data").Range("M2").Value
  Sheets("Email ID").Activate
    Range("A1").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered

    If i = 1 Then  
        ActiveChart.SetSourceData Source:=Range("Data!$E$1:$F$3")        
    ActiveChart.SeriesCollection(1).Select
    With Selection.Format.Fill
       .
       .
       .        
    End If     

    If i = 2 Then

   End if

   If i=3 Then  

    End If   

End Sub

Now, I want to be able to perform the actions when i=2 and i=3 on the same chart i created when i=1

However, I am not able to do so.

So can anyone help me how to give a name for the chart i created when i=1 and be able to refer to it whie i=2 and i=3??

2 Answers 2

6

Kira, you can change the chart name by code using:

yourchart.Name = "Name"

Or in the worksheet you can select the chart, go to the Layout Tab and in the right edge there will be a text box saying Chart Name

then you can refer to it in your vba code using

Worksheets("yoursheet").ChartObjects("Name").PropertyYouWant
Sign up to request clarification or add additional context in comments.

Comments

5

Here is a way to assign a name to a chart:

Sub dural()
    Dim ch As Shape
    Range("A1").Select
    ActiveSheet.Shapes.AddChart.Select
    Set ch = ActiveSheet.Shapes(1)
    ch.Name = "First Chart"
End Sub

1 Comment

and could you also include in your answer, how to refer to it in future cases in the vba code? I mean how to call it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.