VBA 生成数据的年度折线图
VBA generate yearly line graph for data
我的函数需要帮助,我想从两列数据生成图表。第一列包含日期,大约每个月一个,第二列是这些日期的数据(天然气使用量)。我希望图表是一个折线图,每年都有单独的线系列(我没问题)。我希望这些点是每月的,因为数据是每月的,并且 运行 从 1 月到 12 月。
我现在遇到的问题是底部轴从列出的第一个月开始运行(示例图片中的 11 月)并且包含超过 12 个月,因为有些月份不止一次列出,因为读数是那个月服用了不止一次。最终我希望在一个月内将这些汇总为一个值,但稍后我会计算出来。此外,这些值似乎与它们旁边的列中的月份不对应,而是按列出的顺序排列。
Here is a link to a sample picture of the sheet and the current chart
这是我当前的代码:
'These dates will be compared to see if the year has changed
NextDate = ActiveCell.Value
LastDate = ActiveCell.Value
j = 5
K = 1
'Makes the linechart
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
'For some reason a bunch of series are made automatically
'Delete all those garbage series...
For Each s In ActiveChart.SeriesCollection
s.Delete
Next s
'I have a function that already found the last row and assigned it to LastRow... So this runs through all of column A, checks if the years are different and makes a series when the years are different
For i = 5 To (LastRow)
NextDate = ActiveCell.Offset(i - 5, 0).Value
If (Year(LastDate) <> Year(NextDate)) Or (i = LastRow) Then
'The series starts at value after the end of the last
'series, or at row 5
Set rngRange = ActiveSheet.Range(Cells(j, 1), Cells(i, 1))
'K counts up for every series made, starting at 1
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(K).Name = "=""" & Year(LastDate) & """"
ActiveChart.SeriesCollection(K).Values = rngRange.Offset(0, 4)
'Right now I have a function which copies the months into column H.
'Later I'll get it to just use the dates and change the axis to months I think
ActiveChart.SeriesCollection(K).XValues = rngRange.Offset(0, 7)
'Stop
j = i
K = K + 1
LastDate = NextDate
End If
Next i
ActiveChart.ChartStyle = 34
ActiveChart.ApplyLayout (1)
ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
ActiveChart.ClearToMatchStyle
感谢您的帮助,希望我的代码有意义。我边走边学。
为您的日期和使用价值创建一个支点table。将日期放在行区域,将使用总和放在数据区域。然后按年和月对日期进行分组,并将年移动到列区域。制作支点图表 table.
我的函数需要帮助,我想从两列数据生成图表。第一列包含日期,大约每个月一个,第二列是这些日期的数据(天然气使用量)。我希望图表是一个折线图,每年都有单独的线系列(我没问题)。我希望这些点是每月的,因为数据是每月的,并且 运行 从 1 月到 12 月。
我现在遇到的问题是底部轴从列出的第一个月开始运行(示例图片中的 11 月)并且包含超过 12 个月,因为有些月份不止一次列出,因为读数是那个月服用了不止一次。最终我希望在一个月内将这些汇总为一个值,但稍后我会计算出来。此外,这些值似乎与它们旁边的列中的月份不对应,而是按列出的顺序排列。
Here is a link to a sample picture of the sheet and the current chart
这是我当前的代码:
'These dates will be compared to see if the year has changed
NextDate = ActiveCell.Value
LastDate = ActiveCell.Value
j = 5
K = 1
'Makes the linechart
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
'For some reason a bunch of series are made automatically
'Delete all those garbage series...
For Each s In ActiveChart.SeriesCollection
s.Delete
Next s
'I have a function that already found the last row and assigned it to LastRow... So this runs through all of column A, checks if the years are different and makes a series when the years are different
For i = 5 To (LastRow)
NextDate = ActiveCell.Offset(i - 5, 0).Value
If (Year(LastDate) <> Year(NextDate)) Or (i = LastRow) Then
'The series starts at value after the end of the last
'series, or at row 5
Set rngRange = ActiveSheet.Range(Cells(j, 1), Cells(i, 1))
'K counts up for every series made, starting at 1
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(K).Name = "=""" & Year(LastDate) & """"
ActiveChart.SeriesCollection(K).Values = rngRange.Offset(0, 4)
'Right now I have a function which copies the months into column H.
'Later I'll get it to just use the dates and change the axis to months I think
ActiveChart.SeriesCollection(K).XValues = rngRange.Offset(0, 7)
'Stop
j = i
K = K + 1
LastDate = NextDate
End If
Next i
ActiveChart.ChartStyle = 34
ActiveChart.ApplyLayout (1)
ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
ActiveChart.ClearToMatchStyle
感谢您的帮助,希望我的代码有意义。我边走边学。
为您的日期和使用价值创建一个支点table。将日期放在行区域,将使用总和放在数据区域。然后按年和月对日期进行分组,并将年移动到列区域。制作支点图表 table.