Results 1 to 2 of 2

Thread: Finding/Changing Range from existing Excel Chart

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Finding/Changing Range from existing Excel Chart

    I have an Excel template that contains numerous pre-defined charts and a program that opens this template and collects data used to populate the charts. This works great but I now want to adjust the series length so that the charts only used the data collected (not a range of 32000). I'm trying to iterate through the charts and determine the range for each series of each chart but not having much luck. The snippet below allows me to see the xvalues but does not return any information as to where this data is located.

    Does anyone know how to extract the range for each series in a chart?

    vb Code:
    1. For Each oCht As Excel.Chart In oExcel.Workbooks(1).Charts
    2.  
    3.   Dim cnt As Integer = CType(oCht.SeriesCollection, Excel.SeriesCollection).Count
    4.   For i As Integer = 1 To cnt
    5.     oSeries = oCht.SeriesCollection(i)
    6.     x = oSeries.XValues
    7.  
    8.     oRng = CType(CType(oCht.SeriesCollection, Excel.SeriesCollection).Item(i), Excel.Range)
    9.   Next
    10. Next

    BTW, I know how to set the range using

    vb Code:
    1. oCht.SetSourceData(oShtData.Range(strRng), Excel.XlRowCol.xlColumns)

    The problem is that I need to know the current range so that I can simply modify the max length of the series.
    Last edited by campster; Aug 2nd, 2011 at 09:16 AM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Finding/Changing Range from existing Excel Chart

    Here's the solution that I came up with. Is there a better way?

    vb Code:
    1. Public Sub setSeriesLength()
    2.  
    3.         Dim oShtData As Excel.Worksheet
    4.         Dim oSeries As Excel.Series
    5.         Dim rng As String
    6.         Dim tRng As String
    7.         Dim tempRng As String
    8.         Dim strAlpha() As String
    9.  
    10.         oShtData = CType(oExcel.Workbooks(1).Worksheets("data"), Excel.Worksheet)
    11.         Dim rows As Integer = oShtData.UsedRange.Rows.Count
    12.         tRng = "A2:A" & rows
    13.         rng = ""
    14.  
    15.         For Each oCht As Excel.Chart In oExcel.Workbooks(1).Charts
    16.             Dim cnt As Integer = CType(oCht.SeriesCollection, Excel.SeriesCollection).Count
    17.             rng = ""
    18.             For i As Integer = 1 To cnt
    19.                 oSeries = oCht.SeriesCollection(i)
    20.                 tempRng = CType(oCht.SeriesCollection, Excel.SeriesCollection).Item(i).Formula
    21.                 strAlpha = tempRng.Trim.Split("$"c)
    22.                 If strAlpha(0).ToLower.Contains("data") Then
    23.                     tempRng = strAlpha(UBound(strAlpha) - 1) & "2:" & strAlpha(UBound(strAlpha) - 1) & rows
    24.                     If (Len(rng) > 0) Then
    25.                         rng = rng & "," & tempRng
    26.                     Else
    27.                         rng = tempRng
    28.                     End If
    29.  
    30.                     oSeries.Values = oShtData.Range(rng)
    31.                     oSeries.XValues = oShtData.Range(tRng)
    32.                 End If
    33.                
    34.             Next
    35.             oCht.SetSourceData(oShtData.Range(tRng & "," & rng), Excel.XlRowCol.xlColumns)
    36.  
    37.         Next
    38.     End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width