Consulting

Results 1 to 5 of 5

Thread: Solved: VBA Graph - Tweaks Needed to Amend Font and Placement?!! HELP!

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location

    Solved: VBA Graph - Tweaks Needed to Amend Font and Placement?!! HELP!

    Quick question - hopefully. I've created a VBA code which maps a graph based on a selected range - see below:

    [VBA]Sub BartonChart()
    Application.ScreenUpdating = False
    Charts.Add
    ActiveChart.ChartArea.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=Sheets("Summary").Range("D39:P40,D48:P49,D57:K58")
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Barton"
    ActiveChart.SeriesCollection(1).Name = "Barton Summary"
    ActiveChart.HasLegend = False
    With ActiveChart.Axes(xlValue)
    .HasMajorGridlines = False
    .HasMinorGridlines = False
    End With
    ActiveChart.PlotArea.Select
    With Selection.Border
    .ColorIndex = 15
    .Weight = xlThin
    .LineStyle = xlNone
    End With
    Selection.Interior.ColorIndex = xlNone
    Application.ScreenUpdating = True
    MsgBox ("Graphic Mapped")
    End Sub[/VBA]

    Is it at possible to specify the Font to be Arial, size 8 and place the graph over C9:N27?!

    Any help would be much appreciated.

    Thanks.

  2. #2
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi sDave,

    Not that quick...

    [VBA]
    Option Explicit
    Sub BartonChart()
    Dim sName
    Dim cName As String
    Application.ScreenUpdating = False

    Charts.Add

    With ActiveChart
    '.ChartArea.Select
    .ChartType = xlLine
    .SetSourceData Source:=Sheets("Summary").Range("D39:P40,D48:P49,D57:K58")
    .SeriesCollection(1).Name = "Barton Summary"
    .HasLegend = False
    .Location Where:=xlLocationAsObject, Name:="Barton"
    End With

    'Split name on space
    sName = Split(ActiveChart.Name, " ")
    'Drop sheet name
    cName = sName(1) & " " & sName(2)

    With ActiveSheet.Shapes(cName)
    .Left = Sheets("Summary").Range("C9").Left
    .Top = Sheets("Summary").Range("C9").Top
    .Height = Sheets("Summary").Range("C9:N27").Height
    .Width = Sheet1.Range("C9:N27").Width
    End With

    With ActiveChart.Axes(xlValue)
    .HasMajorGridlines = False
    .HasMinorGridlines = False
    .TickLabels.AutoScaleFont = False
    End With

    With ActiveChart.Axes(xlValue).TickLabels.Font
    .Name = "Arial"
    .FontStyle = "Regular"
    .Size = 8
    End With
    ActiveChart.Axes(xlCategory).TickLabels.AutoScaleFont = False
    With ActiveChart.Axes(xlCategory).TickLabels.Font
    .Name = "Arial"
    .FontStyle = "Regular"
    .Size = 8
    End With
    With ActiveChart.PlotArea.Border
    .ColorIndex = 15
    .Weight = xlThin
    .LineStyle = xlNone
    End With
    ActiveChart.PlotArea.Interior.ColorIndex = xlNone

    Application.ScreenUpdating = True

    MsgBox ("Graphic Mapped")
    End Sub

    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  3. #3
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    Thanks dr, you're a star - much appreciated.

    I've got one more question - sorry to be a pain.

    I'm trying to insert values for my X axis but I keep receiving a compile error.

    The script I'm trying to insert is as follows:

    .SeriesCollection(1).XValues = ("Summary").Range("D39:P39,D48:P48,D57:K57")

    Which I've inserted directly after (second line down after With ActiveChart) :

    .SetSourceData Source:=Sheets("Summary").Range("D40:P40,D49:P49,D58:K58")

    Again sorry to be a pain!

  4. #4
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi d

    Well I got it to run the code but...

    There seem to be two diffferent formats:

    .SeriesCollection(1).XValues = Sheets("Summary").Range("D39:P39")
    or
    .SeriesCollection(1).XValues = "=Summary!R39C4:R39C16"

    but that's not the problem. Doesn't seem to like non-contig ranges.

    I did notice that each of your ranges counts as a Seriescollection item.

    HTH


    [vba]

    With ActiveChart
    '.chartArea.Select
    .ChartType = xlLine
    .SetSourceData Source:=Sheets("Summary").Range("D39:P40,D48:P49,D57:K58")
    .SeriesCollection(1).Name = "Barton Summary"

    .SeriesCollection(1).XValues = Sheets("Summary").Range("D39:P39") 'rest of ranges left out...

    'or
    '.SeriesCollection(1).XValues = "=Summary!R39C4:R39C16" ',r48c4:r48c16,r57c4:r57c11"


    .HasLegend = False
    .Location Where:=xlLocationAsObject, Name:="Barton"
    End With
    [/vba]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  5. #5
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    Thanks dr - your a gem. Much appreciated!!!
    Last edited by SDave; 09-02-2009 at 01:49 AM.

Posting Permissions

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