Consulting

Results 1 to 9 of 9

Thread: Solved: Chart Creation

  1. #1
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location

    Cool Solved: Chart Creation

    getting out of range error on red line...seems like this should work to me

    [vba]Sub STEP5_Trending()
    Dim LCol As Integer
    Dim i As Integer
    Dim dataRng(1 To 5) As Range
    Dim title(1 To 5) As String
    ActiveWorkbook.Sheets("Daily Totals").Activate
    Range("A1").Select
    For i = 2 To 40
    If Cells(2, i).Value = "" Then
    LCol = i
    Exit For
    End If
    Next i
    With ActiveSheet
    Set dataRng(1) = Range(.Cells(1, 1), .Cells(13, LCol))
    Set dataRng(2) = Range(.Cells(16, 1), .Cells(28, LCol))
    Set dataRng(3) = Range(.Cells(31, 1), .Cells(43, LCol))
    Set dataRng(4) = Range(.Cells(46, 1), .Cells(58, LCol))
    Set dataRng(5) = Range(.Cells(61, 1), .Cells(73, LCol))
    title(1) = Range("A1").Text
    title(2) = Range("A16").Text
    title(3) = Range("A31").Text
    title(4) = Range("A46").Text
    title(5) = Range("A61").Text
    End With
    For i = 1 To 5
    ChartMaking dataRng(i), title(i)
    Next i
    End Sub

    Private Sub ChartMaking(dataRng As Range, title As String)
    ActiveWorkbook.Sheets("Charts").Activate
    Charts.Add
    With ActiveChart
    .ChartType = xlXYScatterSmoothNoMarkers
    .HasTitle = True
    .ChartTitle.Text = title
    .SetSourceData _
    Source:=Sheets("Daily Total").dataRng, _
    PlotBy:=xlColumns
    .Location Where:=xlLocationAsObject, Name:="Charts"

    .ChartWizard _
    HasLegend = True, _
    CategoryTitle:="Dates", _
    ValueTitle:="Tickets"

    End With

    End Sub[/vba]

    something wrong with my range declaration?

  2. #2
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    is this more what i should have? i imagine its a problem with the range declaration, however i debugged and the range is selecting properly?
    [VBA]

    Set dataRng(1) = Range(""" & .Cells(1, 1).Address & ":" & .Cells(13, LCol).Address & """)
    [/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    DataRng is a vector, so you have to reference it by index, its occurrence in the variant.
    ____________________________________________
    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

  4. #4
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    Quote Originally Posted by xld
    DataRng is a vector, so you have to reference it by index, its occurrence in the variant.
    that wasnt my problem i dont think, its working now but im not really sure why!!!!!

    [vba]Sub STEP5_Trending()
    Dim LCol As Integer
    Dim i As Integer
    Dim dataRng(1 To 5) As Range
    Dim title(1 To 5) As String
    ActiveWorkbook.Sheets("Daily Totals").Activate
    Range("A1").Select
    For i = 2 To 40
    If Cells(2, i).Value = "" Then
    LCol = i - 1
    Exit For
    End If
    Next i
    With ActiveSheet
    Set dataRng(1) = Range(.Cells(1, 1), .Cells(13, LCol))
    Set dataRng(2) = Range(.Cells(16, 1), .Cells(28, LCol))
    Set dataRng(3) = Range(.Cells(31, 1), .Cells(43, LCol))
    Set dataRng(4) = Range(.Cells(46, 1), .Cells(58, LCol))
    Set dataRng(5) = Range(.Cells(61, 1), .Cells(73, LCol))

    title(1) = Range("A1").Text
    title(2) = Range("A16").Text
    title(3) = Range("A31").Text
    title(4) = Range("A46").Text
    title(5) = Range("A61").Text
    End With
    For i = 1 To 5
    ChartMaking dataRng(i), title(i)
    Next i
    End Sub

    Private Sub ChartMaking(dataRng As Range, title As String)
    Charts.Add 'Create a chart sheet
    With ActiveChart 'Set chart properties
    .ChartType = xlXYScatterLines
    .HasTitle = True
    .ChartTitle.Text = title
    .HasLegend = True
    .Legend.Position = xlRight
    .Axes(xlCategory).MinorTickMark = xlOutside
    .Axes(xlValue).MinorTickMark = xlOutside
    .Axes(xlCategory).HasTitle = True
    .Axes(xlCategory).AxisTitle.Characters.Text = "Date"
    .Axes(xlValue).HasTitle = True
    .Axes(xlValue).AxisTitle.Characters.Text = "Tickets"

    .Location Where:=xlLocationAsObject, Name:="Charts"
    End With

    End Sub
    [/vba]

    it seems like im not even using the data range but its working correctly....

    EDIT: its not really working correctly. its churning out 5 of the same chart, but it is using hte correct first data range for all 5
    Last edited by CatDaddy; 06-23-2011 at 04:22 PM.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It seems it was to me, you are now referencing the vector by index as I said.
    ____________________________________________
    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

  6. #6
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    Quote Originally Posted by xld
    It seems it was to me, you are now referencing the vector by index as I said.
    i'm sorry i don't follow, what changed in the way i was referencing the array? and it doesnt actually work now it just uses dataRng(1) to populate 5 appropriatly named charts...

  7. #7
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    ok so this makes the first chart correctly and then says there is an out of range error on the .SetSourceData

    application or user defined error:

    [vba]Sub STEP5_Trending()
    Dim SrcSheet As Worksheet
    Dim LCol As Integer
    Dim i As Integer
    Dim CurTitle As String
    Dim CurRng As Range
    Dim dataRng(1 To 5) As Range
    Dim title(1 To 5) As String
    Set SrcSheet = ActiveWorkbook.Sheets("DailyTotals")
    SrcSheet.Activate
    Range("A1").Select
    For i = 2 To 40
    If Cells(2, i).Value = "" Then
    LCol = i - 1
    Exit For
    End If
    Next i
    With ActiveSheet
    Set dataRng(1) = Range(.Cells(1, 1), .Cells(13, LCol))
    Set dataRng(2) = Range(.Cells(16, 1), .Cells(28, LCol))
    Set dataRng(3) = Range(.Cells(31, 1), .Cells(43, LCol))
    Set dataRng(4) = Range(.Cells(46, 1), .Cells(58, LCol))
    Set dataRng(5) = Range(.Cells(61, 1), .Cells(73, LCol))
    title(1) = Range("A1").Text
    title(2) = Range("A16").Text
    title(3) = Range("A31").Text
    title(4) = Range("A46").Text
    title(5) = Range("A61").Text
    End With
    For i = 1 To 5
    CurTitle = title(i)
    Set CurRng = dataRng(i)
    ChartMaking CurRng, CurTitle
    Next i
    End Sub
    Private Sub ChartMaking(CurRng As Range, CurTitle As String)
    Dim NewChart As Chart
    Set NewChart = ThisWorkbook.Charts.Add
    With NewChart 'Set chart properties

    .SetSourceData _
    Source:=Worksheets("DailyTotals").Range("CurRng"), _
    PlotBy:=xlRows

    .ChartType = xlXYScatterLines
    .HasTitle = True
    .ChartTitle.Text = CurTitle
    .HasLegend = True
    .Legend.Position = xlRight
    .Axes(xlCategory).MinorTickMark = xlOutside
    .Axes(xlValue).MinorTickMark = xlOutside
    .Axes(xlCategory).HasTitle = True
    .Axes(xlCategory).AxisTitle.Characters.Text = "Date"
    .Axes(xlValue).HasTitle = True
    .Axes(xlValue).AxisTitle.Characters.Text = "Tickets"

    End With
    End Sub
    [/vba]
    Last edited by CatDaddy; 06-24-2011 at 04:29 PM.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try

    [vba]
    .SetSourceData Source:=CurRng, PlotBy:=xlRows [/vba]
    ____________________________________________
    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

  9. #9
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    Quote Originally Posted by xld
    Try

    [vba]
    .SetSourceData Source:=CurRng, PlotBy:=xlRows [/vba]
    once again danke schoen

Posting Permissions

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