Home » excel » Excel VBA: Positioning and Alignment of Shapes

Excel VBA: Positioning and Alignment of Shapes

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to make a printable report from excel sheet with some shapes such as charts and pictures. I am able to put the charts on the sheet using the below code:

    oSheetReport.Range("A51").Select()

    Dim oChart1 As Excel.Shape
    oChart1 = oSheetReport.Shapes.AddChart()
    oChart1.Chart.ChartType = Excel.XlChartType.xlLine
    oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)

    oSheetReport.Range("A70").Select()

    oChart1 = oSheetReport.Shapes.AddChart()
    oChart1.Chart.ChartType = Excel.XlChartType.xlColumnStacked
    oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)

    oSheetReport.Range("A100").Select()

    oChart1 = oSheetReport.Shapes.AddChart()
    oChart1.Chart.ChartType = Excel.XlChartType.xlColumnStacked100
    oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)

But positioning and alignment is failing which is making my reports look ugly. Any better way of achieving this?

How to&Answers:

To add, position and align in one step you can use the following:

Set oChart1 = ActiveSheet.ChartObjects.Add _
        (Left:=250, Width:=375, Top:=75, Height:=225)

Left is the left alignment and Top is the top alignment. Width and Height – well, you can figure that out!

More info at http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html