将录制的宏设置为动态代码

时间:2012-05-17 09:03:24

标签: vba excel-vba excel

我录制了这个宏。这是用于插入图表。如何创建图表形状并将其与变量签名以供日后参考?我想将变量传递给范围。我只是想改变(52)数字。因此,如果变量是row,我如何在以下代码中使用它。我试过这个

("'Simpson'!$K$3:$L$row") 

但它不起作用

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Simpson'!$K$3:$L$52")
ActiveChart.ChartType = xlXYScatterSmooth
ActiveChart.ChartTitle.Select
ActiveSheet.ChartObjects("Chart 5").Activate
ActiveChart.ChartTitle.Text = "integrated function chart"
ActiveSheet.ChartObjects("Chart 5").Activate
ActiveChart.Axes(xlValue).Select
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleHorizontal)
ActiveSheet.ChartObjects("Chart 5").Activate
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
ActiveSheet.ChartObjects("Chart 5").Activate
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "x values"
ActiveSheet.ChartObjects("Chart 5""""").Activate
'How can I delete the legend in one. combining the next two
ActiveChart.Legend.Select
Selection.Delete

1 个答案:

答案 0 :(得分:0)

  

我尝试了这个(“'Simpson'!$ K $ 3:$ L $ row”)无法正常工作

试试这个

Dim LRow as Long

LRow = 52 '<~~ Change this as required.

ActiveChart.SetSourceData Source:=Range("'Simpson'!$K$3:$L$" & LRow)