- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 12288 个
- 通用积分
- 370.7282
- 学术水平
- 23 点
- 热心指数
- 36 点
- 信用等级
- 14 点
- 经验
- 3019 点
- 帖子
- 970
- 精华
- 0
- 在线时间
- 3246 小时
- 注册时间
- 2014-4-18
- 最后登录
- 2024-5-11
学科带头人
还不是VIP/贵宾
TA的文库 其他... 投资人 骑牛者
- 威望
- 0 级
- 论坛币
- 12288 个
- 通用积分
- 370.7282
- 学术水平
- 23 点
- 热心指数
- 36 点
- 信用等级
- 14 点
- 经验
- 3019 点
- 帖子
- 970
- 精华
- 0
- 在线时间
- 3246 小时
- 注册时间
- 2014-4-18
- 最后登录
- 2024-5-11
| 难过 2020-8-3 16:39:57 |
---|
签到天数: 62 天 连续签到: 1 天 [LV.6]常住居民II
|
10论坛币
求教大神,本人初学VBA,书上的这段代码运行不出来,请教下面红色那里是怎么错的
Sub CreatedStackedChart()
Dim Cht As Chart
Dim Ser As Series
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
finalcol = Cells(1, Columns.Count).End(xlToLeft).Column
OrigSeriesCount = finalcol - 1
finalseriescount = OrigSeriesCount * 2
ChtHeight = 1000
LabSize = 200
nextcol = finalcol + 2
Cells(1, 1).Resize(finalrow, finalcol).Copy Destination:=Cells(1, nextcol)
finalcol = Cells(1, Columns.Count).End(xlToLeft).Column
MyFormula = "= & ChtHeight & RC[-1]"
For i = finalcol + 1 To nextcol + 2 Step -1
Cells(1, i).EntireColumn.Insert
Cells(1, i).Value = "dummy"
Cells(2, i).Resize(finalrow - 1, l).FormulaR1C1 = MyFormula
Next i
finalcol = Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.Shapes.AddChart(xlAreaStacked).Select
Set Cht = ActiveChart
Cht.SetSourceData Source:=Range(Cells(1, nextcol), Cells(finalrow, finalcol))
Cht.PlotBy = xlColumns
For i = finalseriescount - 1 To 1 Step -2
Cht.Legend.LegendEntries(i).Delete
Next i
TopScale = OrigSeriesCount * ChtHeight
With Cht.Axes(xlValue)
.MaximumScale = TopScale
.MinorUnit = LabSize
.MajorUnit = ChtHeight
End With
Cht.SetElement (msoElementPrimaryValueGridLinesMinorMajor)
For i = finalseries To 2 Step -2
Cht.SeriesCollection(i).Interior.ColorIndex = xlNone
Next i
Cht.Axes(xlValue).TickLabelPositon = xlNone
AxisRow = finalrow + 2
Cells(AxisRow, 1).Resize(1, 3).Value = Array("Label", "X", "Y")
TickMarkCount = OrigSeriesCount * (ChtHeight / LabSize) + 1
Cells(AxisRow + 1, 2).Resize(TickMarkCount, 1).Value = 0
Cells(AxisRow + 1, 3).Resize(TickMarkCount, 1).FormulaR1C1 = "=R[-1]C+" & LabSize
Cells(AxisRow + 1, 3).Value = 0
Cells(AxisRow + 1, 1).Value = 0
Cells(AxisRow + 2, 1).Resize(TickMarkCount - 1, 1).FormulaR1C1 = "=IF(r[-1]c+" & LabSize & " >= " & ChtHeight & ",0,r[-1]c+" & LabSize & ")"
newfinal = Cells(Rows.Count, 1).End(xlUp).Row
Cells(newfinal, 1).Value = ChtHeight
Set Ser = Cht.SeriesCollection.NewSeries
With Ser
.Name = "Y"
.Values = Range(Cells(AxisRow + 1, 3), Cells(newfinal, 3))
.XValues = Range(Cells(AxisRow + 1, 2), Cells(newfinal, 2))
.ChartType = x1XYScatter
.MarkerStyle = xlMarkerStyleNone
End With
For i = 1 To TickMarkCount
Ser.Points(i).HasDataLabel = True
Ser.Points(i).DataLabel.Text = Cells(AxisRow + i, 1).Value
Next i
Cht.Legend.LegendEntries(Cht.Legend.LegendEntries.Count).Delete
End Sub
|
最佳答案
芐雨 查看完整内容
出错的是MyFormula = "= & ChtHeight & RC[-1]"
把他修改成你想要的公式就好
|