Excel 在下面的代码中,我需要知道如何启动B4列中的数据,以及如何使用某一行(比如c行)来处理x轴数据

Excel 在下面的代码中,我需要知道如何启动B4列中的数据,以及如何使用某一行(比如c行)来处理x轴数据,excel,vba,charts,Excel,Vba,Charts,我下面有一些代码是别人为我写的。我正在为工作表中的每一行制作一个折线图,并将这些图表放在工作表2中。我只需要修改这段代码,这样我就可以从单元格B4开始我的折线图数据,一直到数据可用的那一行的末尾。我还需要第2行以单元格C2开始,作为我的x轴数据,这是通过第2行列出的日期 Sub main() 'variable declaration Dim i As Long Dim LastRow As Long Dim LastColumn As Long Dim c

我下面有一些代码是别人为我写的。我正在为工作表中的每一行制作一个折线图,并将这些图表放在工作表2中。我只需要修改这段代码,这样我就可以从单元格B4开始我的折线图数据,一直到数据可用的那一行的末尾。我还需要第2行以单元格C2开始,作为我的x轴数据,这是通过第2行列出的日期

Sub main()
   'variable declaration
    Dim i As Long
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim chrt As Chart
    Dim Sht As Worksheet



    'Find the last used row
    LastRow = ActiveSheet.Range("A54").End(xlUp).Row

    'Find the last used column
    LastColumn = ActiveSheet.Range("A1").End(xlToRight).Column
        Set Sht = ActiveSheet
    'Looping from second row till last row which has the data
    For i = 2 To LastRow
        'Sheet 2 is selected bcoz 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 Sht
            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


我相信以下内容将满足您的要求

Sub main()
'variable declaration
Dim i As Long
Dim LastRow As Long
Dim LastColumn As Long
Dim chrt As Chart
Dim Sht As Worksheet



'Find the last used row
LastRow = ActiveSheet.Range("B54").End(xlUp).Row

'Find the last used column
LastColumn = ActiveSheet.Range("C2").End(xlToRight).Column
    Set Sht = ActiveSheet
'Looping from second row till last row which has the data
For i = 4 To LastRow
    'Sheet 2 is selected bcoz 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 Sht
        chrt.SetSourceData Source:=.Range(.Cells(i, 3), .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

当我尝试这样做时,它将我发送到第2页,但没有复制到第2页。谢谢你的回复,我真的很感激!你知道它为什么这么做吗?它对我有用。但我看到的是,在代码调整图表位置的最后一部分,它将图表移动到第29行(并在第二页的屏幕外)。尝试单步执行代码,您将看到它发生。啊,是的,您是对的,我确实有一个图表,但我需要为每行数据创建一个图表。在原始代码中,如果删除前两行和第一列,代码将为每行创建图表。我试图摆脱的是必须删除那些不需要的行和列。当我删除行和列时,我的第一个图表从第2页的左上角开始,其余的图表直接排列在那一行的下面。我提供的代码将为每一行生成。它基于与屏幕截图中相同的数据格式。我明白了将第一个图形放置在Sheet2(单元格A1)左上角的问题所在。您的代码将图表顶部设置为0。我建议将行
chrt.ChartArea.Top=(I-2)*chrt.ChartArea.Height
更改为
chrt.ChartArea.Top=(I-4)*chrt.ChartArea.Height
。我想那就行了。这不是定位图表的最干净的方法,但它已经足够了。我不知道为什么,但我不能让它在我的电子表格上工作。我搞不懂交易是什么