Re: Need help with SeriesCollection Object Please !
- From: "Peter T" <peter_t@discussions>
- Date: Fri, 17 Apr 2009 19:53:31 +0100
I have a few questions regarding your code.
1) Why did you have your for next loop in the GetSource*** 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 start with the first part of your Q2,
=SERIES(Series-name,Catagory or X-Values,Y-Values, Order)
after doing the Split the 4 element array will hold the following *
Series-name
Catagory or X-Values
Y-Values
Index
Re Q1
Actually I started from the last but one element
For i = UBound(arr) - 1 To 0 Step -1
Although the Series-name can be linked to a cell, often it's not, ditto the
category values. The Y-Values is by far the most likely to be linked to
cells, so might as well start with that, then the next most likely the
X-Values.
Typically, 99+%, will get the source from the 3rd element, then bail out
with Exit For having got the range (don't continue the loop as you appear to
be doing). But if not, try a long shot and loop backwards just in case a
range can be made from either of the earlier elements.
Note, the source in the formula might not be a cell-ref, eg could be in
named range and that'll also get picked up this way. However data can be
data in the formula, eg {1,2,3}, or a named array. These of course are not
linked to cells at all.
You'd probably want to include some more checks to ensure you've got a valid
series formula beforehand.
* a bubble chart has an additional 5th section for the bubble sizes.
Regards,
Peter T
"Dan Thompson" <DanThompson@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5674BFCA-0F50-4D03-A8C6-BCD43697FF3D@xxxxxxxxxxxxxxxx
Well Peter you are right I tried my code with a work*** name that has a
space ie.. "*** 1" and it fails because of the preceding and ending
apostrophes added by Excel when a work*** 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 GetSource*** 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 GetSource***(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
GetSource*** = 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 '*** 1'. Spaces and some other
characters
in the *** 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" <DanThompson@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:3B31E068-09D6-4817-BCD2-9C614CB21048@xxxxxxxxxxxxxxxx
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$5852,1)
Sheet1
FormulaLocal =SERIES("20d MA Spot
Price",Sheet1!$A$2312:$A$5852,Sheet1!$E$2312:$E$5852,1)
Sheet1
FormulaR1C1Local =SERIES("20d MA Spot
Price",Sheet1!R2312C1:R5852C1,Sheet1!R2312C5:R5852C5,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 *** 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" <DanThompson@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:C64AB27B-8DDE-4882-B134-928B99FC6359@xxxxxxxxxxxxxxxx
Thanks Peter for your repsonse
That is strange when I use my code it returns the *** 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 GetSource***(s, sWSname, sFile) Then
MsgBox sWSname & vbCr & sFile
Else
MsgBox "source not determined" '(eg array or named
formula)
End If
End Sub
Function GetSource***(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
GetSource*** = 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" <DanThompson@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:ABBCC779-63EE-4599-B59C-DE04CF952AF2@xxxxxxxxxxxxxxxx
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 GetSource***()
SheetName = ActiveSheet.Name
With ActiveChart
On Error Resume Next
ChartIs*** = False
ChartIndex = .Parent.Index 'For chart objects within a
spread***
If Err.Number = 438 Then 'Error Ocures if the chart selected
is
not
a
chart object with in a spread ***
ChartIndex = .Index 'For a chart that is it's own
***(chart
***)
ChartIs*** = 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
.
- Follow-Ups:
- Re: Need help with SeriesCollection Object Please !
- From: Dan Thompson
- Re: Need help with SeriesCollection Object Please !
- References:
- Need help with SeriesCollection Object Please !
- From: Dan Thompson
- Re: Need help with SeriesCollection Object Please !
- From: Peter T
- Re: Need help with SeriesCollection Object Please !
- From: Dan Thompson
- Re: Need help with SeriesCollection Object Please !
- From: Peter T
- Re: Need help with SeriesCollection Object Please !
- From: Dan Thompson
- Re: Need help with SeriesCollection Object Please !
- From: Peter T
- Re: Need help with SeriesCollection Object Please !
- From: Dan Thompson
- Re: Need help with SeriesCollection Object Please !
- From: Peter T
- Re: Need help with SeriesCollection Object Please !
- From: Dan Thompson
- Need help with SeriesCollection Object Please !
- Prev by Date: Re: IsBlank or 6 digits required
- Next by Date: Need help to modify RDB code
- Previous by thread: Re: Need help with SeriesCollection Object Please !
- Next by thread: Re: Need help with SeriesCollection Object Please !
- Index(es):