Consulting

Results 1 to 11 of 11

Thread: named range tracking via VB

  1. #1
    VBAX Regular
    Joined
    Dec 2014
    Posts
    69
    Location

    named range tracking via VB

    Hi all,

    Is there any way to track named range in chart. I mean is it possible to detect if a given named range is used within a chart with the help of VBA?

    Thanks!

    cross-posted here:
    http://www.excelforum.com/showthread.php?t=1079879&p=4057886#post4057886
    http://www.mrexcel.com/forum/excel-q...ml#post4143670

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    see attached.

    Sub vbax_52434_ChartSeriesSourceData()
    
        Dim wbNamedRanges()
        Dim cht As ChartObject
        Dim SeriesFormula As String, SeriesAddress As String, SeriesRange As String
        Dim i As Long
        
        With ThisWorkbook
            Select Case .Names.Count
                Case Is = 0
                    MsgBox "There no named ranges in this workbook!"
                    Exit Sub
                Case Else
                    ReDim wbNamedRanges(1 To .Names.Count)
                    For i = 1 To .Names.Count
                        wbNamedRanges(i) = .Names(i).Name
                    Next i
            End Select
        End With
        
        Set cht = Worksheets(1).ChartObjects(1) 'sample chart: named range
        'Set cht = Worksheets(1).ChartObjects(2) 'sample chart: not named range
        
        For i = 1 To cht.Chart.SeriesCollection.Count
            SeriesFormula = cht.Chart.SeriesCollection(i).Formula 
            SeriesAddress = Split(SeriesFormula, ",")(2) '3rd bit in series formula
            SeriesRange = Mid(SeriesAddress, InStr(SeriesAddress, "!") + 1) 'remove wb/ws referenece
            If UBound(Filter(wbNamedRanges, SeriesRange)) > -1 Then
                MsgBox "Chart: " & cht.Name & " / Series: " & cht.Chart.SeriesCollection(i).Name & " / Source Data: " & Filter(wbNamedRanges, SeriesRange)(0) & " / Named Range"
            Else
                MsgBox "Chart: " & cht.Name & " / Series: " & cht.Chart.SeriesCollection(i).Name & " / Source Data: " & SeriesRange & " / Not Named Range!"
            End If
        Next
    
    End Sub
    Attached Files Attached Files
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Dec 2014
    Posts
    69
    Location
    Hi mancubus,

    Thanks for the attached file. and this works really great. Just one tweak to this, what if i want to find if the given named range is used within a chart. I mean in multiple sheets there are named ranges, and say if i want to find one of the named ranges to check if its used in chart, how to go about this in VBA?

    Thanks!

  4. #4
    VBAX Regular
    Joined
    Dec 2014
    Posts
    69
    Location
    Or the given code does the same?

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    the code detects if a named range (either workbook or worksheet level) is used as source data and pops up a message box stating the finding.

    in the workbook i posted there are two sample charts with the same source data.

    in the first chart named ranges are used whereas in the second normal ranges are used.

    select chart 1. click Design in Chart Tools tab in ribbon. click Data Source in Data group. click FirstSeries in Legend entries. finally click Edit. Edit Series dialog pops up and you can see Series Values as =vbax_52434_ChartRange.xlsm!First (if you repeat these for SecondSeries the range formula is =vbax_52434_ChartRange.xlsm!Second)

    and if you select chart 2 and repeat these actions you will see the range formulas as =Sheet1!$B$2:$B$9 and =Sheet1!$C$2:$C$9 respectively.

    if you open name manager (Formulas tab, Defined Names group) you can see there ranges are named as First and Second respectively.

    to see the difference in the code, comment out (put a single quotation mark ' at the beginning) the line Set cht = Worksheets(1).ChartObjects(1) and uncomment (remove the single quotation mark ') the line Set cht = Worksheets(1).ChartObjects(2). then run the code again.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Regular
    Joined
    Dec 2014
    Posts
    69
    Location
    Okay, got it. Thanks a lot for your help!!

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    if you are sorted please mark the thread as solved from Thread Tools dropdown for future references.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Regular
    Joined
    Dec 2014
    Posts
    69
    Location
    Hi mancubus,

    You helped me out with a code for how to track named ranges in a chart. For your below code, can you help me modify the code so that it can find named ranges in a chart in any of sheets in a workbook where chart is available.

    Thanks!

    Here is your code:

    Sub vbax_52434_ChartSeriesSourceData() 
         
         
        Dim wbNamedRanges() 
        Dim cht As ChartObject 
        Dim SeriesFormula As String, SeriesAddress As String, SeriesRange As String 
        Dim i As Long 
         
        With ThisWorkbook 
            Select Case .Names.Count 
            Case Is = 0 
                MsgBox "There no named ranges in this workbook!" 
            Case Else 
                ReDim wbNamedRanges(1 To .Names.Count) 
                For i = 1 To .Names.Count 
                    wbNamedRanges(i) = .Names(i).Name 
                Next i 
            End Select 
        End With 
         
        Set cht = Worksheets(1).ChartObjects(1) 'named range
         'Set cht = Worksheets(1).ChartObjects(2) 'not named range
         
        For i = 1 To cht.Chart.SeriesCollection.Count 
            SeriesFormula = cht.Chart.SeriesCollection(i).Formula 
            SeriesAddress = Split(SeriesFormula, ",")(2) '3rd bit in series formula
            SeriesRange = Mid(SeriesAddress, InStr(SeriesAddress, "!") + 1) 'remove wb/ws referenece
            If UBound(Filter(wbNamedRanges, SeriesRange)) > -1 Then 
                MsgBox "Chart: " & cht.Name & " / Series: " & cht.Chart.SeriesCollection(i).Name & " / Source Data: " & Filter(wbNamedRanges, SeriesRange)(0) & " / Named Range" 
            Else 
                MsgBox "Chart: " & cht.Name & " / Series: " & cht.Chart.SeriesCollection(i).Name & " / Source Data: " & SeriesRange & " / Not Named Range!" 
            End If 
        Next 
         
         
    End Sub

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub vbax_52434_ChartSeriesSourceDataAllChartsWB()
    
        Dim wbNamedRanges()
        Dim cht As ChartObject, ws As Worksheet
        Dim SeriesFormula As String, SeriesAddress As String, SeriesRange As String
        Dim i As Long, j As Long
        
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
        End With
        
        With ThisWorkbook
            Select Case .Names.Count
                Case Is = 0
                    MsgBox "There no named ranges in this workbook!"
                Case Else
                    ReDim wbNamedRanges(1 To .Names.Count)
                    For i = 1 To .Names.Count
                        wbNamedRanges(i) = .Names(i).Name
                    Next i
            End Select
        End With
        
        On Error Resume Next
        Worksheets("Chart_Info").Delete
        On Error GoTo 0
        
        Worksheets.Add(Before:=Worksheets(1)).Name = "Chart_Info"
        Worksheets("Chart_Info").Range("A1:E1").Value = Array("Worksheet_Name", "Chart_Name", "Series_Name", "Source_Data", "Named_Range")
        
        j = 1
        For Each ws In Worksheets
            For Each cht In ws.ChartObjects
                For i = 1 To cht.Chart.SeriesCollection.Count
                    j = j + 1
                    SeriesFormula = cht.Chart.SeriesCollection(i).Formula
                    SeriesAddress = Split(SeriesFormula, ",")(2) '3rd bit in series formula
                    SeriesRange = Mid(SeriesAddress, InStr(SeriesAddress, "!") + 1) 'remove wb/ws referenece
                    With Worksheets("Chart_Info")
                        .Range("A" & j).Value = ws.Name
                        .Range("B" & j).Value = cht.Name
                        .Range("C" & j).Value = cht.Chart.SeriesCollection(i).Name
                        If UBound(Filter(wbNamedRanges, SeriesRange)) > -1 Then
                            .Range("D" & j).Value = Filter(wbNamedRanges, SeriesRange)(0)
                            .Range("E" & j).Value = "Yes"
                        Else
                            .Range("D" & j).Value = SeriesAddress
                            .Range("E" & j).Value = "No"
                        End If
                    End With
                Next i
            Next cht
        Next ws
    
        Worksheets("Chart_Info").Columns.AutoFit
    
    End Sub
    
    
    
    
    Sub vbax_52434_ChartSeriesSourceDataSingleChart()
    
        Dim wbNamedRanges()
        Dim cht As ChartObject
        Dim SeriesFormula As String, SeriesAddress As String, SeriesRange As String
        Dim i As Long
        
        With ThisWorkbook
            Select Case .Names.Count
                Case Is = 0
                    MsgBox "There no named ranges in this workbook!"
                Case Else
                    ReDim wbNamedRanges(1 To .Names.Count)
                    For i = 1 To .Names.Count
                        wbNamedRanges(i) = .Names(i).Name
                    Next i
            End Select
        End With
        
        Set cht = Worksheets("Sheet1").ChartObjects(1) 'named range
        'Set cht = Worksheets("Sheet1").ChartObjects(2) 'not named range
        
        For i = 1 To cht.Chart.SeriesCollection.Count
            SeriesFormula = cht.Chart.SeriesCollection(i).Formula
            SeriesAddress = Split(SeriesFormula, ",")(2) '3rd bit in series formula
            SeriesRange = Mid(SeriesAddress, InStr(SeriesAddress, "!") + 1) 'remove wb/ws referenece
            If UBound(Filter(wbNamedRanges, SeriesRange)) > -1 Then
                MsgBox "Chart: " & cht.Name & " / Series: " & cht.Chart.SeriesCollection(i).Name & " / Source Data: " & Filter(wbNamedRanges, SeriesRange)(0) & " / Named Range"
            Else
                MsgBox "Chart: " & cht.Name & " / Series: " & cht.Chart.SeriesCollection(i).Name & " / Source Data: " & SeriesAddress & " / Not Named Range!"
            End If
        Next
    
    End Sub
    Attached Files Attached Files
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  10. #10
    VBAX Regular
    Joined
    Dec 2014
    Posts
    69
    Location
    Wow!!!! That's great of you!! Thanks a lot mancubus!!

  11. #11
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome. i'm glad it helped.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •