brianhfield
New Member
- Joined
- Mar 28, 2024
- Messages
- 5
- Office Version
- 365
- Platform
- Windows
I have a sheet (call it sheet1) with 235 charts. The first chart is in cells D5 through L17 and is named Chart1 in the circled area. This is actually what I want this chart to be named/called.
The second chart starts in cell D19 and spans cells D19 through L31. This chart is named Chart233 (see second image). I am trying to write a code that will rename all the carts on the page from Chart 1 to Chart235 from the top of the sheet to the bottom of the sheet. So, the code needs to examine the upper left cells in column D only as all charts on the sheet start in column D and they just continue below the previous chart.
The below code is what I tried but still leaves the second chart named Chart233. Any thoughts?
The second chart starts in cell D19 and spans cells D19 through L31. This chart is named Chart233 (see second image). I am trying to write a code that will rename all the carts on the page from Chart 1 to Chart235 from the top of the sheet to the bottom of the sheet. So, the code needs to examine the upper left cells in column D only as all charts on the sheet start in column D and they just continue below the previous chart.
The below code is what I tried but still leaves the second chart named Chart233. Any thoughts?
VBA Code:
Sub ResetChartObjectNames()
Dim cht As ChartObject
Dim chartName As String
Dim counter As Integer
' Set starting counter and chart name
counter = 1
chartName = "Chart" & counter
' Loop through all chart objects on the worksheet
For Each cht In ActiveSheet.ChartObjects
' Update chart object name based on counter
cht.Name = chartName
' Increase counter for next chart
counter = counter + 1
chartName = "Chart" & counter
Next cht
End Sub