Excel 使用其他工作簿中的范围填充SeriesCollection
我正在计划一个用户表单,它将使用来自其他文件的数据生成图形。我试图使用另一个工作簿中的数据从图表1填充Excel 使用其他工作簿中的范围填充SeriesCollection,excel,vba,excel-charts,Excel,Vba,Excel Charts,我正在计划一个用户表单,它将使用来自其他文件的数据生成图形。我试图使用另一个工作簿中的数据从图表1填充seriescollection。尽管如此,我的程序会导致空序列集合。下面是这个程序的代码(一些部分被切掉了,因为它们与问题无关) Dim ChtsBig(),openWb作为工作簿,genWb作为工作簿 设置genWb=ActiveWorkbook 如果ListBox1.ListCount=0,则MsgBox(“选择文件!”) ReDim保留ChtsBig(1到ListBox1.ListCou
seriescollection
。尽管如此,我的程序会导致空序列集合
。下面是这个程序的代码(一些部分被切掉了,因为它们与问题无关)
Dim ChtsBig(),openWb作为工作簿,genWb作为工作簿
设置genWb=ActiveWorkbook
如果ListBox1.ListCount=0,则MsgBox(“选择文件!”)
ReDim保留ChtsBig(1到ListBox1.ListCount)
对于i=1到ListBox1.ListCount
fileWb=ListBox1.List(i-1,1)
设置openWb=Application.Workbooks.Open(文件名:=fileWb,只读:=True)
Set sht1=openWb.工作表(1)
设置sht2=openWb.工作表(“周期”)
设置ChtsBig(i)=genWb.Charts.Add
与ChtsBig(一)
.Name=“Cell”&左侧(ListBox1.List(i-1,0),4)
.ChartType=xlxySpatterSmoothNoMarkers
j=1
k=1
对于curr_cyc=TextBox7.Value到TextBox8.Value步骤TextBox9.Value
做
如果sht2.Cells(1+j,1)=curr\u cyc,则
cyc_行=1+j
找到=真
如果结束
j=j+1
循环直到找到=True或sht2。单元格(2+j,1)=空
如果find=True,则
.seriecollection.NewSeries
.SeriesCollection(k).Name=“Cycle”和curr\u cyc&“Charge”
.SeriesCollection(k).XValues=sht1.Range(sht1.Cells(3+sht2.Cells(cyc_行,4),16),sht1.Cells(3+sht2.Cells(cyc_行,5,16))
.SeriesCollection(k).Values=sht1.Range(sht1.Cells(3+sht2.Cells(cyc_行,4),9),sht1.Cells(3+sht2.Cells(cyc_行,5,9))
Numpoint1=.SeriesCollection(k).Points.Count
.SeriesCollection(k).Points(Numpoint1).MarkerStyle=xlmarkerstyle三角形
.seriecollection.NewSeries
.SeriesCollection(k+1).Name=“Cycle”和curr\u cyc+1和“Discharge”
.SeriesCollection(k+1).XValues=sht1.Range(sht1.Cells(3+sht2.Cells(cyc_行,6),16),sht1.Cells(3+sht2.Cells(cyc_行,7,16))
.SeriesCollection(k+1).Values=sht1.Range(sht1.Cells(3+sht2.Cells(cyc_行,6),9),sht1.Cells(3+sht2.Cells(cyc_行,7,9))
.SeriesCollection(k+1).MarkerStyle=xlmarkerstylene
.SeriesCollection(k+1).Border.LineStyle=xlDash
Numpoint2=.SeriesCollection(k+1).Points.Count
.SeriesCollection(k+1).Points(Numpoint2).MarkerStyle=xlMarkerStyleDiamond
currentSeriesColorindex=(k+1)/2+40
如果(k+1)/2+40<57,则
currentSeriesColorindex=(k+1)/2+40
其他的
currentSeriesColorindex=(k+1)/2+32
如果结束
.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
如果结束
发现=错误
k=k+2
下一个
.hasttle=错
.轴(xlValue,xlPrimary)。最小刻度=2.5
.轴(xlValue,xlPrimary)。最大刻度=4.5
.轴(xlCategory,xlPrimary)。最小刻度=0.0001
.Axes(xlCategory).TickLabels.NumberFormat=“#0,0”
.Axes(xlCategory).hasttle=True
.Axes(xlCategory).axistTitle.Caption=“费用/Ah”
.Axes(xlValue).hasttle=True
.Axes(xlValue).AxisTitle.Caption=“电压/V”
.Axes(xlCategory).axistTitle.Font.Bold=False
.Axes(xlValue).axistTitle.Font.Bold=False
.Legend.IncludeInLayout=False
.Legend.Interior.Color=RGB(255、255、255)
.Axes(xlCategory).HasMajorGridlines=True
.Axes(xlCategory).MajorGridlines.Border.Color=RGB(160160160160)
.Axes(xlCategory).MajorGridlines.Border.LineStyle=xlDash
.Axes(xlValue).MajorGridlines.Border.Color=RGB(160160160)
.Axes(xlValue).MajorGridlines.Border.LineStyle=xlDash
以
openWb.Close SaveChanges:=False
下一个
调试此范围时:sht1.range(sht1.Cells(3+sht2.Cells(cyc_行,4),16),sht1.Cells(3+sht2.Cells(cyc_行,5),16))
在值2中有正确的值,尽管.SeriesCollection(k).XValues
为空
一旦澄清,sht1就有了图表的原始数据,但被分成了块(例如,一个数据集在第250行到第500行之间)。Sht2包含关于这些块位置的信息
如果有帮助的话,下面是图形的外观:此模式:
如果find=True,则
.seriecollection.NewSeries
.SeriesCollection(k).Name=“Cycle”和curr\u cyc&“Charge”
容易出现一些问题,因为这取决于是否存在预先存在的系列。由于NewSeries
返回添加的序列,因此这将更加健壮:
Dim s1 As系列
如果find=True,则
Set s1=.SeriesCollection.NewSeries()'在创建时获取引用
s1.Name=“周期”和当前周期和“费用”
无论何时添加新图表,都可以通过检查
SeriesCollection.Count
来确保excel没有为您“自动添加”任何系列。在开始添加系列之前,这也是一个好主意。您好,感谢您提出这个问题。我通常添加图表区。创建新图表后清除。你知道为什么我的公司
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, 4), 9), sht1.Cells(3 + sht2.Cells(cyc_row, 5), 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), 16), sht1.Cells(3 + sht2.Cells(cyc_row, 7), 16))
.SeriesCollection(k + 1).Values = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row, 6), 9), sht1.Cells(3 + sht2.Cells(cyc_row, 7), 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, 160)
.Axes(xlValue).MajorGridlines.Border.LineStyle = xlDash
End With
openWb.Close SaveChanges:=False
Next