如何解决从另一个工作簿中用范围填充SeriesCollection
我正在计划一个用户表单,该表单将使用其他文件中的数据生成图形。我试图使用另一个工作簿中的数据填充Chart1中的seriescollection
。虽然,我的程序导致空seriescollection
。下面是该程序的代码(某些部分已删除,因为它们与问题无关)。
Dim ChtsBig(),openWb As Workbook,genWb As Workbook
Set genWb = ActiveWorkbook
If ListBox1.ListCount = 0 Then MsgBox ("Select files!")
ReDim Preserve ChtsBig(1 To ListBox1.ListCount)
For i = 1 To ListBox1.ListCount
fileWb = ListBox1.List(i - 1,1)
Set openWb = Application.Workbooks.Open(Filename:=fileWb,ReadOnly:=True)
Set sht1 = openWb.Worksheets(1)
Set sht2 = openWb.Worksheets("Cycles")
Set ChtsBig(i) = genWb.Charts.Add
With ChtsBig(i)
.Name = "Cell " & Left(ListBox1.List(i - 1,0),4)
.ChartType = xlXYScatterSmoothNoMarkers
j = 1
k = 1
For curr_cyc = TextBox7.Value To TextBox8.Value Step TextBox9.Value
Do
If sht2.Cells(1 + j,1) = curr_cyc Then
cyc_row = 1 + j
found = True
End If
j = j + 1
Loop Until found = True Or sht2.Cells(2 + j,1) = Empty
If found = True Then
.SeriesCollection.NewSeries
.SeriesCollection(k).Name = "Cycle " & curr_cyc & " Charge"
.SeriesCollection(k).XValues = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,4),16),sht1.Cells(3 + sht2.Cells(cyc_row,5),16))
.SeriesCollection(k).Values = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,9),9))
Numpoint1 = .SeriesCollection(k).Points.Count
.SeriesCollection(k).Points(Numpoint1).MarkerStyle = xlMarkerStyleTriangle
.SeriesCollection.NewSeries
.SeriesCollection(k + 1).Name = "Cycle " & curr_cyc + 1 & " Discharge"
.SeriesCollection(k + 1).XValues = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,6),7),16))
.SeriesCollection(k + 1).Values = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,9))
.SeriesCollection(k + 1).MarkerStyle = xlMarkerStyleNone
.SeriesCollection(k + 1).Border.LineStyle = xlDash
Numpoint2 = .SeriesCollection(k + 1).Points.Count
.SeriesCollection(k + 1).Points(Numpoint2).MarkerStyle = xlMarkerStyleDiamond
currentSeriesColorindex = (k + 1) / 2 + 40
If (k + 1) / 2 + 40 < 57 Then
currentSeriesColorindex = (k + 1) / 2 + 40
Else
currentSeriesColorindex = (k + 1) / 2 + 32
End If
.SeriesCollection(k).Points(Numpoint1).MarkerForegroundColorIndex = currentSeriesColorindex
.SeriesCollection(k).Points(Numpoint1).MarkerBackgroundColorIndex = currentSeriesColorindex
.SeriesCollection(k + 1).Points(Numpoint2).MarkerForegroundColorIndex = currentSeriesColorindex
.SeriesCollection(k + 1).Points(Numpoint2).MarkerBackgroundColorIndex = currentSeriesColorindex
.SeriesCollection(k + 1).Border.ColorIndex = currentSeriesColorindex
.SeriesCollection(k).Border.ColorIndex = currentSeriesColorindex
End If
found = False
k = k + 2
Next
.HasTitle = False
.Axes(xlValue,xlPrimary).MinimumScale = 2.5
.Axes(xlValue,xlPrimary).MaximumScale = 4.5
.Axes(xlCategory,xlPrimary).MinimumScale = 0.0001
.Axes(xlCategory).TickLabels.NumberFormat = "#0,0"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Caption = "Charge / Ah"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Caption = "Voltage / V"
.Axes(xlCategory).AxisTitle.Font.Bold = False
.Axes(xlValue).AxisTitle.Font.Bold = False
.Legend.IncludeInLayout = False
.Legend.Interior.Color = RGB(255,255,255)
.Axes(xlCategory).HasMajorGridlines = True
.Axes(xlCategory).MajorGridlines.Border.Color = RGB(160,160,160)
.Axes(xlCategory).MajorGridlines.Border.LineStyle = xlDash
.Axes(xlValue).MajorGridlines.Border.Color = RGB(160,160)
.Axes(xlValue).MajorGridlines.Border.LineStyle = xlDash
End With
openWb.Close SaveChanges:=False
Next
在调试此范围时:sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,16))
为空,.SeriesCollection(k).XValues
在Value2中具有正确的值。
澄清一下,sht1拥有图表的原始数据,但已分为多个块(例如,一个数据集位于行250至500之间)。 Sht2包含有关这些块的位置的信息。
如果有帮助,这是图形的外观:Generated Graph
解决方法
此模式:
If found = True Then
.SeriesCollection.NewSeries
.SeriesCollection(k).Name = "Cycle " & curr_cyc & " Charge"
容易出现一些问题,因为它取决于是否存在预先存在的系列。由于NewSeries
返回添加的序列,因此会更可靠:
Dim s1 As Series
If found = True Then
Set s1 = .SeriesCollection.NewSeries() 'get a reference on creation
s1.Name = "Cycle " & curr_cyc & " Charge"
每当添加新图表以确保excel都没有为您“自动添加”任何系列时,这都是一个好主意,方法是在开始添加系列之前检查SeriesCollection.Count
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。