我在excel中记录的宏有问题。
宏:
插入2个图
选择第一个图形
将某些数据放在辅助垂直轴上
该宏运行到下面的行并给出错误:
ActiveSheet.ChartObjects(“图表17”)。激活
有人知道我可以摆脱其中的“图表17”部分,而用不需要引用图表17或任何编号图表的东西代替它的方法吗?
预先感谢
编辑-抱歉,下面的代码用于创建有效的图形,而无效的图形编辑
Sub PLOTGraph()
Range("B:B,D:D,F:F,H:H,J:J,L:L").Select
Range("L1").Activate
ActiveWindow.SmallScroll ToRight:=9
Range("B:B,D:D,F:F,H:H,J:J,L:L,N:N,P:P,R:R,T:T").Select
Range("T1").Activate
ActiveWindow.SmallScroll ToRight:=10
Range("B:B,D:D,F:F,H:H,J:J,L:L,N:N,P:P,R:R,T:T,V:V,X:X,Z:Z,AB:AB,AD:AD").Select
Range("AD1").Activate
ActiveWindow.SmallScroll ToRight:=7
Range( _
"B:B,D:D,F:F,H:H,J:J,L:L,N:N,P:P,R:R,T:T,V:V,X:X,Z:Z,AB:AB,AD:AD,AF:AF,AH:AH,A J:AJ,AL:AL" _
).Select
Range("AL1").Activate
ActiveWindow.SmallScroll ToRight:=8
Range( _
"B:B,D:D,F:F,H:H,J:J,L:L,N:N,P:P,R:R,T:T,V:V,X:X,Z:Z,AB:AB,AD:AD,AF:AF,AH:AH,A J:AJ,AL:AL,AN:AN,AP:AP,AR:AR,AT:AT" _
).Select
Range("AT1").Activate
ActiveWindow.SmallScroll ToRight:=7
Range( _
"B:B,D:D,F:F,H:H,J:J,L:L,N:N,P:P,R:R,T:T,V:V,X:X,Z:Z,AB:AB,AD:AD,AF:AF,AH:AH,A J:AJ,AL:AL,AN:AN,AP:AP,AR:AR,AT:AT,AV:AV" _
).Select
Range("AV1").Activate
ActiveSheet.Shapes.AddChart2(240, xlXYScatterLinesNoMarkers).Select
End Sub
Sub PREPGRAPH()
ActiveSheet.ChartObjects("Chart 17").Activate
ActiveSheet.Shapes("Chart 17").IncrementLeft 307.4175590551
ActiveSheet.Shapes("Chart 17").IncrementTop -7.4175590551
ActiveSheet.ChartObjects("Chart 17").Activate
ActiveSheet.Shapes("Chart 17").ScaleWidth 2.2901131822, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 17").ScaleHeight 2.4446731852, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.ChartObjects("Chart 17").Activate
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(13).Select
ActiveChart.Legend.LegendEntries(12).Select
ActiveChart.FullSeriesCollection(12).Select
ActiveChart.FullSeriesCollection(12).AxisGroup = 2
ActiveSheet.ChartObjects("Chart 17").Activate
ActiveChart.FullSeriesCollection(12).Select
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(12).Select
ActiveChart.FullSeriesCollection(13).Select
ActiveChart.FullSeriesCollection(13).AxisGroup = 2
ActiveSheet.ChartObjects("Chart 17").Activate
ActiveChart.FullSeriesCollection(13).Select
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(12).Select
ActiveChart.FullSeriesCollection(14).Select
ActiveChart.FullSeriesCollection(14).AxisGroup = 2
ActiveSheet.ChartObjects("Chart 17").Activate
ActiveChart.FullSeriesCollection(14).Select
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(12).Select
ActiveChart.FullSeriesCollection(15).Select
ActiveChart.FullSeriesCollection(15).AxisGroup = 2
ActiveSheet.ChartObjects("Chart 17").Activate
ActiveChart.FullSeriesCollection(15).Select
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(12).Select
ActiveChart.FullSeriesCollection(16).Select
ActiveChart.FullSeriesCollection(16).AxisGroup = 2
ActiveSheet.ChartObjects("Chart 17").Activate
ActiveChart.FullSeriesCollection(16).Select
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
ActiveChart.SetElement (msoElementSecondaryValueAxisTitleAdjacentToAxis)
ActiveChart.Axes(xlValue).AxisTitle.Select
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = _
"Voltage (V) & Temperature (?)"
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"Voltage (V) & Temperature (?)"
With Selection.Format.TextFrame2.TextRange.Characters(1, 29).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 7).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
With Selection.Format.TextFrame2.TextRange.Characters(8, 20).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
With Selection.Format.TextFrame2.TextRange.Characters(28, 2).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "Times New Roman"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Italic = msoFalse
.Kerning = 12
.Name = "Times New Roman"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.Axes(xlCategory).AxisTitle.Select
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Time (s)"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Time (s)"
With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.Axes(xlValue, xlSecondary).AxisTitle.Select
ActiveChart.Axes(xlValue, xlSecondary).AxisTitle.Text = "Voltage (V)"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Voltage (V)"
With Selection.Format.TextFrame2.TextRange.Characters(1, 11).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 11).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
End Sub
答案 0 :(得分:0)
您将要使用图表对象的索引。如果始终是第一位,则代码与始终是倒数第二位的代码不同。
Dim i As Integer
If ActiveSheet.ChartObjects.Count > 0 Then
i = ActiveSheet.ChartObjects.Count -1 'second to last
' if always want i = 1, you can just use 1 instead of i
ActiveSheet.ChartObjects(i).Activate
'do whatever with your chart here
Else
MsgBox "no charts"
End If