• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Resizing an Excel Chart from VB

I have found out how to add a chart to an excel file but what I want to do is move the chart  and resize it, can anybody point me in the right direction?

Here is how I am adding the chart

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim xlSheet2 As Excel.Worksheet
    Dim xlChart As Excel.Chart
    Dim i As Integer
    Dim NextVal As Integer
    ' Create Excel Application object
    Set xlApp = New Excel.Application
    ' Create a new workbook
    Set xlBook = xlApp.Workbooks.Add
    ' Get rid of all but one worksheet
    xlApp.DisplayAlerts = False
    For i = xlBook.Worksheets.Count To 2 Step -1
        xlBook.Worksheets(i).Delete
    Next i
    xlApp.DisplayAlerts = True
    Set xlSheet2 = xlBook.ActiveSheet
    xlSheet2.Name = "mysheet"
    Set xlChart = xlApp.Charts.Add
    With xlChart
        Dim xlsSerie As Excel.SeriesCollection
        Set xlsSerie = .SeriesCollection
        Dim xlsAxisCategory As Excel.Axes
        .ChartType = xlColumnStacked
        .SetSourceData xlSheet2.Range("B1:B" & j)
        xlsSerie.Item(1).HasDataLabels = True
        Set xlsAxisCategory = .Axes(, xlPrimary)
        xlsAxisCategory.Item(xlCategory).HasTitle = True
        xlsAxisCategory.Item(xlCategory).AxisTitle.Characters.Text = "Hours"
        .PlotBy = xlColumns
        .Location xlLocationAsObject, conSheetName2
    End With
    With xlBook.ActiveChart
        .HasTitle = True
        .HasLegend = True
        .SeriesCollection(1).XValues = xlSheet2.Range("A1:A" & j)
        With .ChartTitle
            .Characters.Text = conSheetName2 & " Chart"
            .Font.Size = 12
            .Shadow = True
            .Border.LineStyle = xlSolid
        End With
        With .ChartGroups(1)
            .GapWidth = 14
            .VaryByCategories = True
        End With
        .PlotArea.ClearFormats
    End With

Thanks in advance
0
cdthurman
Asked:
cdthurman
1 Solution
 
EDDYKTCommented:
From record the macro

' move
ActiveSheet.Shapes("Chart 1").IncrementLeft -95.4
ActiveSheet.Shapes("Chart 1").IncrementTop -94.2
'resize
ActiveSheet.Shapes("Chart 1").ScaleWidth 1.51, msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 1").ScaleHeight 1.39, msoFalse, msoScaleFromTopLeft
0
 
RogueSolutionsCommented:
I'm getting errors from your code - j is not set and neither is conSheetName2

Assuming you want the chart loose on mysheet then change your location line a little

        .Location Where:=xlLocationAsObject, Name:=xlSheet2.Name

The position of the chart is handled by the ChartObject (the container for the chart itself)

    With xlSheet2.ChartObjects(1)
        .Left = 40
        .Top = 40
    End With

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now