Need help with SeriesCollection Object Please !
Well Peter you are right I tried my code with a worksheet name that has a
space ie.. "Sheet 1" and it fails because of the preceding and ending
apostrophes added by Excel when a worksheet has a space in the name
I have tried your code and indeed it does work more consistently than mine.
by the way I was unaware of the "Split" Function very nice I can think of
several other macros I have which could benefit from that one. And the use of
turning the split string into a range and using the "Parent " property to
extract the table name seems to be a much more reliable of way doing it.
I have a few questions regarding your code.
1) Why did you have your for next loop in the GetSourceSheet function
looping backwards threw your array and not forwards
2) Why did you use 4 Elements in your Array and then the use of the Exit for
which seems to be unnessicary
To Illistrate my questions below is my edited version of your function. If
there is some reason that you did your function the way you did which makes
it more reliable could you please explain it to me ?
Function GetSourceSheet(sFmla As String, sWSname, sFile As String) As Boolean
Dim i As Long
Dim arr
Dim rng As Range
On Error Resume Next
arr = Split(Mid$(sFmla, 9, Len(sFmla) - 9), ",")
For i = 0 To 1
Set rng = Range(arr(i))
If Not rng Is Nothing Then
sWSname = rng.Parent.Name
sFile = rng.Parent.Parent.Name
GetSourceSheet = True
End If
Next
End Function
Dan Thompson
"Peter T" wrote:
Well Peter you have got me scratching my head on this one I noticed when I
ran my code on certain other charts sometimes it would return ('Sheet1')
and
other charts it would return as it should (Sheet1)
That's a different issue. I'll bet you never returned precisely 'Sheet1' but
you may well have returned say 'Sheet 1'. Spaces and some other characters
in the sheet name introduce the embracing apostrophes. Easily correctable,
replace the apostrophes with "", but that aside I still don't think your
routine is right.
Only thing I can figure is maybe difference in Excel versions being used
like I said I am using Excel 2000 perhaps newer versions handle things
differently ??
No, Excel 97 to Excel 2007 will work the same in this respect
If you use the routine I suggested you may find your head suffers less from
over scratching! Also it might cater for more scenarios than you may have
yet considered.
Regards,
Peter T
"Dan Thompson" wrote in message
...
Well Peter you have got me scratching my head on this one I noticed when I
ran my code on certain other charts sometimes it would return ('Sheet1')
and
other charts it would return as it should (Sheet1) so I changed my code to
use .FormulaR1C1Local and that seems to work consistantly for both the
charts
that were returning normal (Sheet1) and the ones that were returning
('Sheet1') I don't know why you are getting the comma though I have run
several tests and I am not getting the preceding comma, like I said I did
have some issues with some charts returning with preceding and post single
quotes ('Sheet1') however the .FormulaR1C1local seems to do the trick for
eliminating that problem
I ran your test code though and here are the results for all the .formula
methods used plus I added the R1C1 too.
Sub test3()
Dim s As String
s = ActiveChart.SeriesCollection(1).Formula
Debug.Print "Formula", s
Debug.Print GetSheetName(s)
s = ActiveChart.SeriesCollection(1).FormulaLocal
Debug.Print "FormulaLocal", s
Debug.Print GetSheetName(s)
s = ActiveChart.SeriesCollection(1).FormulaR1C1Local
Debug.Print "FormulaR1C1Local", s
Debug.Print GetSheetName(s)
End Sub
Function GetSheetName(ByVal ChartSeriesString As String) As String
Dim FChar As Integer, LChar As Integer
FChar = InStr(1, ChartSeriesString, ",") + 1
LChar = InStr(1, ChartSeriesString, "!") - 1
GetSheetName = Mid(ChartSeriesString, FChar, LChar - FChar + 1)
End Function
debug results
Formula =SERIES("20d MA Spot
Price",Sheet1!$A$2312:$A$5852,Sheet1!$E$2312:$E$58 52,1)
Sheet1
FormulaLocal =SERIES("20d MA Spot
Price",Sheet1!$A$2312:$A$5852,Sheet1!$E$2312:$E$58 52,1)
Sheet1
FormulaR1C1Local =SERIES("20d MA Spot
Price",Sheet1!R2312C1:R5852C1,Sheet1!R2312C5:R5852 C5,1)
Sheet1
Only thing I can figure is maybe difference in Excel versions being used
like I said I am using Excel 2000 perhaps newer versions handle things
differently ??
Dan Thompson
"Peter T" wrote:
That is strange when I use my code it returns the Sheet Name (String)
Fine
without the preceding comma I did notice that you are using
Activechart.Seriescollection(1).formula I am using .formulalocal
Sub test3()
Dim s As String
s = ActiveChart.SeriesCollection(1).Formula
Debug.Print "Formula", s
Debug.Print GetSheetName(s)
s = ActiveChart.SeriesCollection(1).FormulaLocal
Debug.Print "FormulaLocal", s
Debug.Print GetSheetName(s)
End Sub
Function GetSheetName(ByVal ChartSeriesString As String) As String
Dim FChar As Integer, LChar As Integer
FChar = InStr(1, ChartSeriesString, ",") + 1
LChar = InStr(1, ChartSeriesString, "!") - 1
GetSheetName = Mid(ChartSeriesString, FChar, LChar - FChar + 1)
End Function
debug results
Formula =SERIES(,,Sheet1!$A$1:$A$3,1)
,Sheet1
FormulaLocal =SERIES(,,Sheet1!$A$1:$A$3,1)
,Sheet1
As you can see, a comma both ways
I am using Excel 2000 so I don't know if that has
somthing to do with it.
No, that wouldn't make a difference, but if the series name is linked to
a
cell it would (but can't be sure it always is).
I am not conserned at this time with
charts that are linked to data in different workbooks
The addin I mentioned also has a function to "resource" data from one
location to another, eg from an external wb to the chart wb. To cater for
most scenarios was amount of work.
Regards,
Peter T
"Dan Thompson" wrote in message
...
Thanks Peter for your repsonse
That is strange when I use my code it returns the Sheet Name (String)
Fine
without the preceding comma I did notice that you are using
Activechart.Seriescollection(1).formula I am using .formulalocal I
don't
know
if that makes a differnce in the string returned having a preceding
comma
perhaps my math is off by 1 placement in the string but strangely
enough
my
code works on my system ? I am using Excel 2000 so I don't know if
that
has
somthing to do with it.
But yes you are right the objective of the macro is to remove all data
that
is not relevent to the active chart. I am not conserned at this time
with
charts that are linked to data in different workbooks however you bring
up
a
good point that I may need to incorporate into this code in the future.
I will try your code out and compare thanks for your input on this
today.
Dan Thompson
"Peter T" wrote:
Your GetSheetName routine doesn't seem right, I get ",Sheet1" with the
preceding comma
The following if anything is slightly more complicated, but more
reliable.
It will also return the workbook name which might be relevant if the
data
and chart are not in same file.
Sub test()
Dim s As String
Dim sWSname As String, sFile As String
s = ActiveChart.SeriesCollection(1).Formula
If GetSourceSheet(s, sWSname, sFile) Then
MsgBox sWSname & vbCr & sFile
Else
MsgBox "source not determined" '(eg array or named formula)
End If
End Sub
Function GetSourceSheet(sFmla As String, sWSname, sFile As String) As
Boolean
Dim i As Long
Dim arr
Dim rng As Range
On Error Resume Next
arr = Split(Mid$(sFmla, 9, Len(sFmla) - 9), ",")
For i = UBound(arr) - 1 To 0 Step -1
Set rng = Range(arr(i))
If Not rng Is Nothing Then
sWSname = rng.Parent.Name
sFile = rng.Parent.Parent.Name
GetSourceSheet = True
Exit For
End If
Next
End Function
If I follow you are trying to remove all but essential chart data. I
have
an
addin that replaces source data in cells with source data in named
arrays
or
arrays in the series formula (latter subject relatively small data
limits
per series). IOW, can end up with a workbook with zero data in cells,
or
if
charts are chart-sheets zero worksheets. Contact me if interested (my
address is in the reply-to field).
Regards,
Peter T
"Dan Thompson" wrote in
message
...
Darn I thought that in this day and age SeriesCollection Object
would
alread
have a built in property or method for returning the Worksheetname
Guess I
will have to use my original method which does work just was hoping
for
a
shortcut :)
Here is the code I am using :
Sub GetSourceSheet()
SheetName = ActiveSheet.Name
With ActiveChart
On Error Resume Next
ChartIsSheet = False
ChartIndex = .Parent.Index 'For chart objects within a
spreadsheet
If Err.Number = 438 Then 'Error Ocures if the chart selected is
not
a
chart object with in a spread sheet
ChartIndex = .Index 'For a chart that is it's own sheet(chart
sheet)
ChartIsSheet = True
End If
Err.Clear
NumOfSeries = .SeriesCollection.Count
End With
ReDim SeriesArray(NumOfSeries)
ReDim ColArray(NumOfSeries)
For X = 1 To NumOfSeries
SeriesArray(X) = ActiveChart.SeriesCollection(X).FormulaLocal
ColArray(X) = DataCol(SeriesArray(X))
Next X
SourceWrksheet = GetSheetName(SeriesArray(1))
Worksheets(SourceWrksheet).Activate
End Sub
Function GetSheetName(ByVal ChartSeriesString As String) As String
Dim FChar As Integer, LChar As Integer
FChar = InStr(1, ChartSeriesString, ",") + 1
LChar = InStr(1, ChartSeriesString, "!") - 1
GetSheetName = Mid(ChartSeriesString, FChar, LChar - FChar + 1)
End Function
Keep in mind this code is only part of my entire program / macro
What the Entire Code does is remove all worksheets and data from a
workbook
that does not directly or indirectly belong to the Active Chart
and than alows the user to save the end result as a new workbook.
Thus
not
overiting the original workbook.
If you are interested here is the Entire Working Code for this Macro
Option Base 1
Sub AddChartSeriesDataFilterMenuButton()
'*******Add's A Menu Button to Excel to run Procedure "CSDF"
**********
Dim xlBar As CommandBar
|