简体   繁体   中英

How do I change a reference to a chart objects in excel vba

I have a problem with a macro I've recorded in excel.

The macro:

  1. inserts 2 graphs

  2. selects the 1st graph

  3. Puts certain data on a secondary vertical axis

The macro works up to the below line and gives the error:

ActiveSheet.ChartObjects("Chart 17").Activate

Does anyone know a way I can get rid of the "Chart 17" part of it and replace it with something that does not need to reference Chart 17 or any numbered chart?

Thanks in Advance

EDIT - Apologies, below is the code for creating the graph which works and the graph editing which doesnt

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

You will want to use the index of the Chart Object. If it will always be the 1st one, the code is different than if it is always the second to last one.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM