+ Reply to Thread
Results 1 to 1 of 1

Adding horizontal line to VBA column chart

Hybrid View

  1. #1
    Registered User
    Join Date
    04-08-2013
    Location
    Wakefield, England
    MS-Off Ver
    Excel 2010
    Posts
    8

    Adding horizontal line to VBA column chart

    Hi all,

    I've put together some code linked to a button in my data sheet ("Scores"). When pressed, this creates a bar chart in separate worksheets for each row of data. The code probably doesn't look too tidy, but it works fine The data for the chart is in columns H to BZ, of these H is used for the graph title/worksheet name and all other columns are numbers 0-100

    Now, I'd also like to add a target line onto these charts - this would be horizontal but at a different level for each graph so I can't just draw it in, the data for this is stored in column E. I wondered if anyone might be able to give any ideas on whether this is possible, or would it be a better move to create a separate target line graph over the top with a transparent background?

    Sub Charts()
        Dim Ws As Worksheet
        Dim NewWs As Worksheet
        Dim cht As Chart
        Dim LastRow As Long
        Dim CurrRow As Long
         
        Set Ws = ThisWorkbook.Worksheets("Scores")
         
        LastRow = Ws.Range("A65536").End(xlUp).Row
        For CurrRow = 3 To LastRow
            Set NewWs = ThisWorkbook.Worksheets.Add
            NewWs.Name = Ws.Range("H" & CurrRow).Value
            Set cht = ThisWorkbook.Charts.Add
            With cht
                .ChartType = xlColumnClustered
                .SeriesCollection.NewSeries
                .SeriesCollection(1).Values = "=" & Ws.Name & "!R" & CurrRow & "C78:R" & CurrRow & "C9"
                .SeriesCollection(1).Name = "=" & Ws.Name & "!R" & CurrRow & "C8"
                .Location Where:=xlLocationAsObject, Name:=NewWs.Name
                ActiveChart.SeriesCollection(1).XValues = "='Scores'!$I$2:$BZ$2"
                ActiveChart.Axes(xlValue).Select
                ActiveChart.Axes(xlValue).MaximumScale = 100
                ActiveChart.Axes(xlValue).MajorUnit = 10
            End With
                Dim RngToCover As Range
                Dim ChtOb As ChartObject
                Set RngToCover = ActiveSheet.Range("A1:AC46")
                Set ChtOb = ActiveChart.Parent
                ChtOb.Height = RngToCover.Height
                ChtOb.Width = RngToCover.Width
                ChtOb.Top = RngToCover.Top
                ChtOb.Left = RngToCover.Left
        Next CurrRow
    End Sub
    Thanks, Adam
    Last edited by the_adam; 07-04-2013 at 10:00 AM. Reason: edit code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1