Excel VBA:形状的定位和对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12816728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:03:59  来源:igfitidea点击:

Excel VBA: Positioning and Alignment of Shapes

vbaexcel-vbaexcel

提问by Srinivas

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:

我正在尝试使用某些形状(例如图表和图片)从 Excel 工作表制作可打印的报告。我可以使用以下代码将图表放在工作表上:

    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?

但是定位和对齐失败,这使我的报告看起来很难看。有没有更好的方法来实现这一目标?

回答by Jamie Bull

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)

Leftis the left alignment and Topis the top alignment. Widthand Height- well, you can figure that out!

Left是左对齐,Top是顶部对齐。Width而且Height- 好吧,你可以弄清楚!

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

更多信息请访问http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html