从某个单元格开始使用VBA excel为每一行创建折线图(动态行,列)

时间:2014-04-10 18:53:05

标签: excel vba

我对此网站上发布的问题提出了类似的问题,我只需要修改一下它的完成情况。

Creating Line Chart for each Row using VBA excel (Dynamic Row,Column)

我需要做与上面完全相同的事情,除了我需要我的选择从单元格B4开始。从B4开始,它需要转到最后一行和最后一列,就像上面一样。只需要在B4而不是A1开始。我试图从上面的帖子中提出这个问题,但有人因某种原因将其删除了。

1 个答案:

答案 0 :(得分:2)

为什么不尝试以下操作(在最后一列条目中换掉A1代替B4)。取自@Santosh提供的优秀答案here

Sub main()

   'variable declaration
    Dim i As Long
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim chrt As Chart

    'Find the last used row
    LastRow = Sheets("Sheet1").Range("A65536").End(xlUp).Row

    'Find the last used column - this will be the place that you start, which is in cell B4
    LastColumn = Sheets("Sheet1").Range("B4").End(xlToRight).Column

    'Looping from second row till last row which has the data
    For i = 2 To LastRow
        'Sheet 2 is selected because the charts will be inserted here
        Sheets("Sheet2").Select

        'Adds chart to the sheet
        Set chrt = Sheets("Sheet2").Shapes.AddChart.Chart
        'sets the chart type
        chrt.ChartType = xlLine

        'now the line chart is added...setting its data source here
        With Sheets("Sheet1")
            chrt.SetSourceData Source:=.Range(.Cells(i, 1), .Cells(i, LastColumn))
        End With

        'Left & top are used to adjust the position of chart on sheet
        chrt.ChartArea.Left = 1
        chrt.ChartArea.Top = (i - 2) * chrt.ChartArea.Height

        Next

End Sub