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
Dim CustMnuBar As CommandBarButton
Set xlBar = Application.CommandBars("Chart Menu Bar")
Set CustMnuBar = xlBar.Controls.Add(Type:=msoControlButton,
Temporary:=False)
CustMnuBar.Caption = "ChartSeriesDataFilter"
CustMnuBar.Style = msoButtonCaption
CustMnuBar.Visible = True
With CustMnuBar
.OnAction = "CSDF"
End With
End Sub
'#############################################################################
' Chart Series Data Filter [MACRO]
'
'Import this module into a workbook with charts you wish to run it on.
'Import this module into "Personal Workbook" to have access to it from
any
Workbook.
'
'To run simply select a chart object, or chart Sheet and run the macro.
'The macro when run, will delete all charts and data out of the current
'workbook which are not relevent to the chart you selected to isolate.
'#############################################################################
Sub CSDF()
Dim ChartIndex As Integer, NumOfSeries As Integer, X As Integer,
SheetCount
As Integer, y As Integer
Dim SeriesArray() As Variant, ColArray() As Variant
Dim ChartIsSheet As Boolean
Dim SheetName As String, SheetsToDel() As String, fileSaveName As
String
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
For X = 1 To NumOfSeries
Range(ColArray(X) & ":" & ColArray(X)).Select 'Selects source data
column of a chart series
Selection.Interior.ColorIndex = 4 'Colors chart series data column
bright green
Range(ColArray(X) & ":" &
ColArray(X)).Precedents.Columns.EntireColumn.Select 'The Precedents
command
Returns a Range object that represents all the precedents(links) of a
cell
If Not Err.Number = 1004 Then 'Error 1004 is "No Cells Were Found"
Meaning there are no Precedents(Links) for the cells.
Selection.Interior.ColorIndex = 35 'colors all
precendent(Linked)
cells light pastell green
End If
Next X
Err.Clear
Resume
GetNonGreenColPos
If ChartIsSheet = False Then
Sheets(SheetName).Activate
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="NewChart"
ElseIf ChartIsSheet = True Then
Sheets(SheetName).Activate
Sheets(SheetName).Name = "NewChart"
End If
SheetCount = ActiveWorkbook.Sheets.Count
For y = 1 To SheetCount
If Not Sheets(y).Name = SourceWrksheet Then
If Not Sheets(y).Name = "NewChart" Then
t = t + 1
ReDim Preserve SheetsToDel(t)
SheetsToDel(t) = Sheets(y).Name
End If
End If
Next y
Application.DisplayAlerts = False ' prevent nags
For y = LBound(SheetsToDel) To UBound(SheetsToDel)
Sheets(SheetsToDel(y)).Delete
Next y
Application.DisplayAlerts = True
With Application
fileSaveName =
Application.GetSaveAsFilename(InitialFilename:="NewChart.xls",
fileFilter:="Excel File (*.xls), *.xls")
End With
ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlNormal
ActiveWorkbook.Close (True)
End Sub
Sub GetNonGreenColPos()
Dim N As Integer, a As Integer, z As Integer, FirstOcurance As Integer,
SecondOcurance As Integer
Dim CurCelAddress As String, NonGreenColArray() As String,
MyRangeString
As
String
Dim rngToDelete As Range
With ActiveSheet
a = 0
For N = 1 To 256
If Not Cells(1, N).Interior.ColorIndex = 4 Then
If Not Cells(1, N).Interior.ColorIndex = 35 Then
CurCelAddress = Cells(1, N).Address
FirstOcurance = InStr(1, CurCelAddress, "$")
SecondOcurance = InStr(FirstOcurance + 1, CurCelAddress,
"$")
NonGreenCol = Mid(CurCelAddress, FirstOcurance + 1,
SecondOcurance - (FirstOcurance + 1))
a = a + 1
ReDim Preserve NonGreenColArray(a)
NonGreenColArray(a) = NonGreenCol
End If
End If
Next N
Set rngToDelete = Columns(NonGreenColArray(2))
For z = 3 To UBound(NonGreenColArray())
Set rngToDelete = Union(rngToDelete, Columns(NonGreenColArray(z)))
Next z
'rngToDelete.Select 'For Just Selection of Data Columns linked to Chart
Series.
rngToDelete.Delete 'To delete all data columns not relevent to selected
chart.
End With
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
Function DataCol(ByVal DataRange As String) As String
'*****************************************************
'Returns single Column letter from a "A1" Style Range
'
'Example:
' MyRangeString = "'Data Calc'!$A$4:$A$20"
' MyColLetter = DataCol(MyRangeString)
'
' Returns String: A
'*****************************************************
Dim t As Integer, X As Integer
X = Len(DataRange)
For t = 1 To X
If Left(Right(DataRange, t), 1) = "!" Then
If Left(Right(DataRange, t - 3), 1) = "$" Then
DataCol = Left(Right(DataRange, t - 2), 1)
Exit For
Else
DataCol = Left(Right(DataRange, t - 2), 2)
Exit For
End If
End If
Next t
End Function
:
You're going about it the right way, extract the sheet name from the
series
formula. You could show us your parsing routine in case it might be
simplified.
Regards,
Peter T