我有一个宏将选择数据从一个工作表复制到另一个工作表。从该工作表中获取数据并将其复制到图表中。尽管我的宏指定了进入图表的源数据,但它并没有将其全部用完。
我正在运行Windows 7.我有两个运行Windows 10但没有此问题的同事。完全相同的代码。创建完全相同的源数据。但是我的版本没有提取所有源数据。他们的确如此。我不明白为什么。
以下是代码:
Sub graph_creator()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.Calculation = xlManual
Dim cf As Worksheet
Dim CF_lrow As Long
Dim graphdata As Worksheet
Dim ptr As Long
Dim i As Long
Dim graph_ws As Worksheet
Dim graph_lrow As Long
Dim graph As Chart
Set cf = ThisWorkbook.Worksheets("Cashflow with Payment Schedule")
Set graphdata = ThisWorkbook.Worksheets("Graph Data")
CF_lrow = cf.Cells(Rows.Count, 6).End(xlUp).Row
graphdata.Rows(2 & ":" & Rows.Count).EntireRow.Delete
For ptr = 2 To CF_lrow - 28
graphdata.Cells(ptr, 1).Formula = "='Cashflow with Payment Schedule'!F" & (ptr + 28)
graphdata.Cells(ptr, 2).Formula = "='Cashflow with Payment Schedule'!J" & (ptr + 28)
graphdata.Cells(ptr, 3).Formula = "='Cashflow with Payment Schedule'!H" & ptr + 27
Next
i = 29
For ptr = CF_lrow - 27 To (CF_lrow - 27) * 2 - 2
graphdata.Cells(ptr, 1).Formula = "='Cashflow with Payment Schedule'!F" & i
graphdata.Cells(ptr, 3).Formula = "='Cashflow with Payment Schedule'!H" & i
graphdata.Cells(ptr, 2).Formula = "='Cashflow with Payment Schedule'!J" & i
i = i + 1
Next
Set graph = cf.ChartObjects("Chart 1").Chart
graph_lrow = graphdata.Cells(Rows.Count, 1).End(xlUp).Row
graph.SetSourceData Source:=graphdata.Range("A1:C" & graph_lrow)
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.Calculation = xlAutomatic
End Sub
同样,我们都使用相同的确切代码。为什么我的代码不像他们的那样提取所有源数据。另请注意图表上的日期。
答案 0 :(得分:1)
您的图表是正确的,而您的同事图表则没有。原因如下:
您的数据集有两个重叠的序列,在同一年覆盖大致相同的曲线。
您的同事图表按顺序显示所有Y值,而不考虑X值。这是不正确的。
同事机器上的默认图表类型与您的不同,因此当您将通用图表传递给Excel上的三列而不告诉Excel如何处理这三列时,程序的默认值定义了曲线图。
如图ChartType property所示设置图表的here。那应该清除它!