使用宏创建图形

时间:2012-12-19 10:16:22

标签: excel

1)问题

我想将宏用于我的原始数据安排, 我的原始数据存在于文件夹中的许多.xls文件中,所有这些文件的排列方式如下:

A1 ........空白 A(空白)---------空白

我需要在1个.xls文件中安排不同表格中的每一列,如下所示

数据(sheet1)数据(sheet2)直到空白... 列A列B直到空白...

2)问题是我需要循环这个宏以便于创建图形...真正适用于帮助

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+g

    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.ApplyChartTemplate ( _
        "C:\Users\bukharms\AppData\Roaming\Microsoft\Templates\Charts\analysis.crtx")

    ActiveSheet.ChartObjects("Ambient").Activate
    ActiveChart.SeriesCollection(1).Values = "='data'!$B$2:$B$298"
    ActiveChart.SeriesCollection(1).Values = "='data (2)'!$B$2:$B$298"
    ActiveChart.SeriesCollection(2).Values = "='data (3)'!$B$2:$B$261"
    ActiveChart.SeriesCollection(3).Values = "='data (4)'!$B$2:$B$243"
    ActiveChart.SeriesCollection(4).Values = "='data (5)'!$B$2:$B$265"
    ActiveChart.SeriesCollection(5).Values = "='data (6)'!$B$2:$B$307"
    ActiveChart.SeriesCollection(6).Values = "='data (7)'!$B$2:$B$728"

 ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.ApplyChartTemplate ( _
        "C:\Users\bukharms\AppData\Roaming\Microsoft\Templates\Charts\analysis.crtx")

    ActiveSheet.ChartObjects("Coolant Into Engine").Activate
    ActiveChart.SeriesCollection(1).Values = "='data'!$C$2:$C$298"
    ActiveChart.SeriesCollection(1).Values = "='data (2)'!$C$2:$C$298"
    ActiveChart.SeriesCollection(2).Values = "='data (3)'!$C$2:$C$261"
    ActiveChart.SeriesCollection(3).Values = "='data (4)'!$C$2:$C$243"
    ActiveChart.SeriesCollection(4).Values = "='data (5)'!$C$2:$C$265"
    ActiveChart.SeriesCollection(5).Values = "='data (6)'!$C$2:$C$307"
    ActiveChart.SeriesCollection(6).Values = "='data (7)'!$C$2:$C$728"

 ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.ApplyChartTemplate ( _
        "C:\Users\bukharms\AppData\Roaming\Microsoft\Templates\Charts\analysis.crtx")

    ActiveSheet.ChartObjects("Coolant Out from Engine").Activate
    ActiveChart.SeriesCollection(1).Values = "='data'!$D$2:$D$298"
    ActiveChart.SeriesCollection(1).Values = "='data (2)'!$D$2:$D$298"
    ActiveChart.SeriesCollection(2).Values = "='data (3)'!$D$2:$D$261"
    ActiveChart.SeriesCollection(3).Values = "='data (4)'!$D$2:$D$243"
    ActiveChart.SeriesCollection(4).Values = "='data (5)'!$D$2:$D$265"
    ActiveChart.SeriesCollection(5).Values = "='data (6)'!$D$2:$D$307"
    ActiveChart.SeriesCollection(6).Values = "='data (7)'!$D$2:$D$728"

1 个答案:

答案 0 :(得分:0)

您可以通过使用Chr()转换ascii代码来遍历字符。 Chr(64)='A',Chr(65)='B'等...

 For i = 1 To 4   
    ActiveSheet.ChartObjects(i).Activate
    ActiveChart.SeriesCollection(1).Values = "='data'!$" & Chr(i + 65) & "$2:$" & Chr(i + 65) & "$298"
    ActiveChart.SeriesCollection(1).Values = "='data (2)'!$" & Chr(i + 65) & "$2:$" & Chr(i + 65) & "$298"
    'etc...
  next i