VBA to list location of all charts on 2 worksheets

Access Beginner

Active Member
Joined
Nov 8, 2010
Messages
311
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a workbook that has a lot of charts (over 300 on 2 sheets).
What I am after if possible, is to list each chart on each sheet and it's location (cell reference).

Sheet one is called: Chart_Tables

Sheet two is called: Remote_Chart_Tables

Worksheet name where they are to be listed: ChartLocations
Chart_Tables to be listed from B2 in ChartLocations
Remote_Chart_Tables to be listed from D2 in ChartLocations
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
See if this is the sort of thing you are after.
Code:
Sub Chart_Locations()
  Dim ch As ChartObject
  Dim itm As Variant
  Dim ws As Worksheet
  Dim rFirst As Range
  Dim oSet As Long
  
  Const sData As String = "Chart_Tables#B2|Remote_Chart_Tables#D2"
  
  For Each itm In Split(sData, "|")
    oSet = 0
    Set ws = Sheets(Split(itm, "#")(0))
    Set rFirst = Sheets("ChartLocations").Range(Split(itm, "#")(1))
    
    For Each ch In ws.ChartObjects
      rFirst.Offset(oSet).Value = ch.TopLeftCell.Address(0, 0)
      oSet = oSet + 1
    Next ch
  Next itm
End Sub
 
Upvote 0
Or this
(sheet name "ChartLocations" does not include an underscore as per your notes)

Code:
Sub ListCharts()
    Dim RCT As Worksheet, CT As Worksheet, CL As Worksheet, chrt As ChartObject, cel As Range
    Set RCT = Sheets("Remote_Chart_Tables")
    Set CT = Sheets("Chart_Tables")
    Set CL = Sheets("ChartLocations")

'clear old values (allows repeated runs)
    CL.Range("B1:F" & Rows.Count).ClearContents
    Range("B1").Resize(, 4).Value = Split("Chart,Addr,Remote,Addr", ",")
'create list of chart locations
    For Each chrt In CT.ChartObjects
        Set cel = CL.Cells(Rows.Count, "B").End(xlUp).Offset(1)
        cel = chrt.Name
        cel.Offset(, 1) = chrt.TopLeftCell.Address(0, 0)
    Next
    For Each chrt In RCT.ChartObjects
        Set cel = CL.Cells(Rows.Count, "D").End(xlUp).Offset(1)
        cel = chrt.Name
        cel.Offset(, 1) = chrt.TopLeftCell.Address(0, 0)
    Next
End Sub
 
Upvote 0
Thanks Yongle, worked perfectly and even though I didn't stipulate for the chart name to be included ( and I did want that) your code did the trick.
 
Upvote 0
Thanks Peter, worked like a charm
You're welcome, but yes, I missed the part about the chart names. :oops:
However, my overall method would be useful if there might later be other worksheets with charts to also process as all you would need to do is to add to the 'Const' line to list the sheet names and result starting cells. :)
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

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