View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
INTP56 INTP56 is offline
external usenet poster
 
Posts: 66
Default Find source WS from Chart

Peter,

I'm not familiar with the split command, I'll have to look into it.

Bob

P.S. Here is what I did:

Public Sub ToolBarButtonCode()
'This code is being run because of an .OnAction property of a toolbar
button.
Dim wbActive As Workbook, wsTest As Worksheet, chtTest As Chart
Dim strChartFormula As String, lngStart As Long, lngLength As Long

On Error Resume Next
Set wbActive = ActiveWorkbook
Set wsTest = ActiveSheet
Set chtTest = ActiveSheet
On Error GoTo 0

If wbActive Is Nothing Or (wsTest Is Nothing And chtTest Is Nothing) Then
MsgBox "Action not available"
Exit Sub
End If

If Not chtTest Is Nothing Then
strChartFormula = chtTest.SeriesCollection(1).Formula
lngStart = 1 + InStr(1, strChartFormula, "'")
lngLength = InStr(1, strChartFormula, "!") - lngStart - 1

On Error Resume Next
Set wsTest = wbActive.Worksheets(Mid(strChartFormula, lngStart,
lngLength))
On Error GoTo 0

End If

If wsTest Is Nothing Then
MsgBox "Chart data not in active workbook"
Exit Sub
End If

'At this point, wsTest should hold the worksheet object of the source
data.
MsgBox wsTest.Name
End Sub


"Peter T" wrote:

Indeed only way is to parse the formula, eg

Sub test()
Dim s As String
s = ActiveSheet.ChartObjects(1).Chart.SeriesCollection (1).Formula
s = Split(s, ",")(2)
s = Left$(s, InStr(2, s, "!") - 1)
s = Replace(s, "'", "")
MsgBox s
'Debug.Print s
End Sub

Regards,
Peter T

"INTP56" wrote in message
...
Bill,

Thanks for the response. I know that in general the data sets can come

from
anywhere, but in this particular application I know all the source data is
coming from one worksheet. I was hoping something like
.Series(1).XValues.RefersToRange.Parent or something like that would just
give me the Worksheet object I was looking for.

Bob


"Bill Renaud" wrote:

<<Is there a better way of determining where the source data from a

chart
(or series really) is coming from?

In general, I don't think so. Remember that a chart can have data from
multiple worksheets, even if it is not a combination or mixed data chart
with a secondary axis.
--
Regards,
Bill Renaud