excel中批量生成图表的脚本

经常要根据一堆数据生成各种bug曲线的报表,所以就抽时间完成了这个脚本,有需求的可以拿去自行修改
Function getcolname(ByVal intcol As Long)
    intcol = intcol - 1
    getcolname = IIf(intcol \ 26, Chr(64 + intcol \ 26), "") & Chr(65 + intcol Mod 26)
End Function

Sub 批量生成图表()
'
' 批量生成图表
' by marty
' 原始数据需要保存在sheet1中

    x = 0
    y = 0
    Sheets.Add After:=Sheets(Sheets.Count)
    'ActiveSheet.Name = "Bug图表"
    '获取sheet1中总的列数
    intcol = Sheets("Sheet1").Range("IV1").End(xlToLeft).Column
    strCol = getcolname(intcol)
    '生成图表
    For i = 2 To 6
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlLineMarkers
        ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("C" & i & ":" & strCol & i)
        ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$C$1:$" & strCol & "$1"
        ActiveChart.SeriesCollection(1).Name = "=Sheet1!$B$" & i
        '设置x轴的类型为文本坐标轴,而不是时间坐标轴
        ActiveChart.Axes(xlCategory).Select
        ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
    Next i
    '调整每个图表的位置
    For Each Chart In ActiveSheet.ChartObjects
        Chart.Activate
        ActiveChart.ChartArea.Select
            Chart.Top = y * 206
            Chart.Left = x * 606
            Chart.Height = 200
            Chart.Width = 600
        x = x + 1
        If x >= 1 Then
            x = 0
            y = y + 1
        End If
    Next Chart
    
End Sub


你可能感兴趣的:(脚本,Excel,报表)