我想在for循环中使用单个范围调用来将数据系列添加到图表(XY行)。
在我的示例中,基于用户输入他们想要在绘图(1,2或3)上绘制多少数据系列,ChartData(1),ChartData(2)和ChartData(3)。对应于SeriesCollection(1),SeriesCollection(2),SeriesCollection(3)。
无法确定范围内的用途。
Dim cb As ComboBox
Dim rge As range
Dim MyChart As Chart
Dim ChartData As range
Dim chartIndex As Integer
Dim ChartName(3) As String
Dim n As Long
Dim i As Long
Dim valid As Boolean: valid = True
m = Application.InputBox("Please enter the number of TML's to graph (1,2, or 3): ", "Select # of TML's", Type:=1)
For i = 1 To m
Set cb = TargetSheet.Shapes("ComboBox" & i).OLEFormat.Object.Object
Set rge = TargetSheet
If IsNumeric(m) And m <= 3 And m > 0 Then _
For a = 1 To m
chartIndex(a) = cb(a).ListIndex
For n = 3 To lastRowTarget2
Select Case chartIndex
Case n - 3
Set ChartData(a) = TargetSheet.range(TargetSheet.Cells(n, 5), TargetSheet.Cells(n, MyRange.Columns.Count - 2))
ChartName = TargetSheet.range("C" & n).Text
UserForm1.TextBox1.Value = TargetSheet.Cells(n, MyRange.Columns.Count).Value
UserForm1.TextBox2.Value = TargetSheet.Cells(n, MyRange.Columns.Count - 1).Value
UserForm1.TextBox3.Value = TargetSheet.Cells(n, 4)
End Select
Next n
Application.ScreenUpdating = False
Set MyChart = TargetSheet.Shapes.AddChart.Chart
With MyChart
.ChartType = xlLineMarkers
.HasTitle = True
.ChartTitle.Text = "Wall Thickness Trend based on Data Points"
.SeriesCollection.NewSeries
.SeriesCollection(a).Name = ChartName
.SeriesCollection(a).Values = ChartData
.SeriesCollection(a).XValues = TargetSheet.range(TargetSheet.Cells(1, 5), TargetSheet.Cells(1, MyRange.Columns.Count))
.SeriesCollection(a).Trendlines.Add Type:=xlLinear
.DisplayBlanksAs = xlInterpolated
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (dd-mm-yyyy)"
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Measured Wall Thickness (mm)"
.Legend.Delete
.Parent.Height = 350
.Parent.Width = 550
.Parent.Top = 100
.Parent.Left = 100
End With
Dim ser As Series
Set ser = MyChart.SeriesCollection(a)
ser.ErrorBar Direction:=xlY, Include:=xlErrorBarIncludeBoth, Type:=xlErrorBarTypeFixedValue, Amount:=1
Next a