Consulting

Results 1 to 4 of 4

Thread: Charting an array as a series

  1. #1
    VBAX Newbie
    Joined
    Jul 2008
    Posts
    3
    Location

    Charting an array as a series

    I have come across a problem in my code that I am having trouble identifying. I have created 4 arrays based on data in a spreadsheet that was given to me, and I am trying to chart them; one as the x-axis and the others as y-axis values. I have provided my code for the array creation loop as well as the chart with-blocks. I don't get any run-time errors, but when the data is graphed, my three y-axis arrays show up as {0,0,...,0}. The x values are fine, its just the y values that are wrong. Also, when stepping through the program and printing values from the arrays at various steps, the data is correct. I am very new to VBA (two days in) and don't understand what I am doing wrong.

    ReDim ClkDate(rowCount)
    ReDim LSeries(rowCount)
    ReDim MSeries(rowCount)
    ReDim NSeries(rowCount)
    For i = 1 To rowCount - 1
    ClkDate(i) = Cells(counter, "H")
    LSeries(i) = Cells(counter, "L")
    MSeries(i) = Cells(counter, "M")
    NSeries(i) = Cells(counter, "N")
    counter = counter + 1
    Next i

    Sheet1.Activate
    With Charts(tabTitle).SeriesCollection.NewSeries
    .Name = "AttainedStd"
    .Values = LSeries
    .XValues = ClkDate
    End With
    With Charts(tabTitle).SeriesCollection.NewSeries
    .Name = "Std Variance"
    .Values = MSeries
    .XValues = ClkDate
    End With
    With Charts(tabTitle).SeriesCollection.NewSeries
    .Name = "%Efficiency"
    .Values = NSeries
    .XValues = ClkDate
    End With

    Any advice you guys can provide would be greatly appreaciated.

    Edit: I found my solution. It was simply that I defined the arrays as strings, but to use them as a series they had to be integer values. It was a pretty stupid mistake and a simple fix, I just couldn't see it for the longest time.
    Last edited by smr122; 07-16-2008 at 06:03 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you post the complete workbook, it is a bit lacking in context.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Jul 2008
    Posts
    3
    Location
    Here is the spreadsheet. I don't have the code with me because I just got home from work. The basic idea that my boss wants is to be able to highlight a set of machines from column A and hit the macro so that it will graph the data from columns L, M, and N. Each of those will be separate series with the dates from column H as the x values. I'll post the full module first thing tomorrow morning.

    Thanks for the reply!

  4. #4
    VBAX Newbie
    Joined
    Jul 2008
    Posts
    3
    Location
    Here is my full code:

    Sub GraphCreation()

    ' Keyboard Shortcut: Ctrl+q

    Dim chtTitle As String
    Dim tabTitle As String
    Dim ClkDate() As String
    Dim LSeries() As String
    Dim MSeries() As String
    Dim NSeries() As String
    Dim counter As Integer
    Dim rowCount As Integer
    Dim i As Integer

    rowCount = Selection.Rows.Count
    chtTitle = Cells(ActiveCell.row, "B")
    tabTitle = Cells(ActiveCell.row, "A")
    counter = ActiveCell.row

    Charts.Add
    With ActiveChart
    .ChartType = xlXYScatterLines
    .HasTitle = True
    .ChartTitle.Text = chtTitle
    End With
    ActiveSheet.Name = tabTitle
    Charts(tabTitle).SeriesCollection(1).Delete

    Sheet1.Activate
    ReDim ClkDate(rowCount)
    ReDim LSeries(rowCount)
    ReDim MSeries(rowCount)
    ReDim NSeries(rowCount)
    For i = 1 To rowCount - 1
    ClkDate(i) = Cells(counter, "H")
    LSeries(i) = Cells(counter, "L")
    MSeries(i) = Cells(counter, "M")
    NSeries(i) = Cells(counter, "N")
    counter = counter + 1
    Next i

    Sheet1.Activate
    With Charts(tabTitle).SeriesCollection.NewSeries
    .Name = "AttainedStd"
    .Values = LSeries
    .XValues = ClkDate
    End With
    With Charts(tabTitle).SeriesCollection.NewSeries
    .Name = "Std Variance"
    .Values = MSeries
    .XValues = ClkDate
    End With
    With Charts(tabTitle).SeriesCollection.NewSeries
    .Name = "%Efficiency"
    .Values = NSeries
    .XValues = ClkDate
    End With

    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
  •