Macro to create/name/position a chart.

duleysw

New Member
Joined
Jul 31, 2009
Messages
25
Hello,

I can't seem to find this anywhere for some reason but how do you name a chart. When I use the macro recorder I see they get named Chart 1, Chart 2 etc.
I have a macro to create a chart that works the first time around, but will create a new chart each time it is run. I want it to just update, but can't figure out how to get the name of the chart to reference it.

Code:
'Make Chart
Set d = .Find(Hours_Chart, [COLOR=red]LookIn:???)[/COLOR]
 
        [COLOR=seagreen]'If the chart hasn't been created, then create it.[/COLOR]
[COLOR=black]If d Is Nothing Then[/COLOR]
 
           ActiveSheet.Shapes.AddChart.Select
           ActiveChart.ChartType = xlColumnClustered
           ActiveChart.ApplyLayout (5)
           ActiveWindow.SmallScroll Down:=0
           ActiveChart.SetSourceData Source:=Range(("O3"), Range _
("P3").End(xlDown))
           [COLOR=red]Need to name chart as "Hours_Chart" so that below I can refer to it.[/COLOR]
 
                [COLOR=seagreen]'Otherwise if it is present, then update it.[/COLOR]
     Else:
 
           [COLOR=seagreen]'Chart you are updating[/COLOR]
                   ActiveSheet.ChartObjects("Hours_Chart").Activate
 
         [COLOR=seagreen]' Set range data[/COLOR]
               Set rngChartdata = Sheets("Hours Log").Range(("O3"), Range _
("P3").End(xlDown))
 
                   ActiveChart.SetSourceData Source:=rngChartdata, _
PlotBy:=xlColumns
        End If

1) How do I give the chart a specific name?
2) How do I use .find to see if it is already there.
3)What property defines the charts location on the screen.

Thanks as usual...

If I can't search for it, but can name it I can just select it, delete it and re-make it.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Thanks for the link. I actually ran across the same site, different page.

I may fiddle with the chart renaming later. I decided to create them in a template and just leave them to be updated each cycle.

I ended up going with the following.
Code:
[COLOR=seagreen]'Updates 100% Stacked Column Chart (Chart 1)[/COLOR]
        ActiveSheet.ChartObjects("Chart 1").Activate
        ActiveChart.SetSourceData Source:=Range(("M1"), Range("O1").End(xlDown))
 
    [COLOR=seagreen]'Place the Chart appropriately[/COLOR]
        Dim CvrRng As Range
 
        Set CvrRng = Sheets("Hours Log").Range("M" & (i + 3), "X" & (i + 23))
        Set mychart = ActiveChart.Parent
 
        With mychart
            .Height = CvrRng.Height
            .Width = CvrRng.Width
            .Left = CvrRng.Left
            .Top = CvrRng.Top
 
        End With
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,928
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top