Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting charts without referencing the chart number

Tags:

excel

vba

charts

I want to select a random chart on a sheet in excel using VBA without knowing the chart number because the chart generated always changes in number. Could anyone help please? Is it possible to select a chart without referencing the chart number? I want to change the chart name of the active chart.

1   ActiveSheet.ChartObjects("Chart 409").Activate
2   ActiveSheet.Shapes("Chart 409").Name = "Chart 1"
3   ActiveSheet.ChartObjects("Chart 1").Activate
like image 707
user2640906 Avatar asked Aug 12 '13 08:08

user2640906


1 Answers

To select all the charts or the Random one, You can use chart Index.

Sub getcharts()

Dim ws As Worksheet
Dim ch As ChartObject
Set ws = ActiveSheet

cnt = ws.ChartObjects.Count
random_num = Application.WorksheetFunction.RandBetween(1, cnt)

ws.ChartObjects(random_num).Name = "NAM"  'The Random chart
For Each ch In ws.ChartObjects
    ch.Name = "Put the name of Chart here "
    'Or Do anything with you all the charts here
Next


End Sub
like image 105
PankajKushwaha Avatar answered Oct 03 '22 03:10

PankajKushwaha