我有一些代码是由别人为我写的。我正在为工作表中的每一行制作折线图,并将这些图表放在工作表2上。我只需要修改此代码,以便我可以从单元格B4开始我的折线图数据,一直到该行的结尾数据可用的地方。我还需要从单元格C2开始的第2行,是我的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
答案 0 :(得分:0)
我相信以下内容适用于您所要求的内容。
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