我正在设置一个宏来生成图表。我在生成示例图表时记录了一个宏,但现在我需要让宏独立于图表的名称(在这种情况下为Chart 9
)
Sheets("statistics").Select
Sheets("statistics").Range("A101:C106").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnStacked
ActiveChart.SetSourceData Source:=Range("statistics!$A$101:$C$106")
ActiveChart.ChartArea.Select
ActiveSheet.Shapes("Chart 9").Name = "waterfall"
ActiveChart.Location Where:=xlLocationAsObject, Name:="summary"
ActiveSheet.ChartObjects("waterfall").Activate
ActiveSheet.Shapes("waterfall").IncrementLeft 80
ActiveSheet.Shapes("waterfall").IncrementTop -2200
ActiveSheet.ChartObjects("waterfall").Activate
ActiveSheet.Shapes("waterfall").ScaleWidth 1.6025463692, msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes("waterfall").ScaleHeight 1.6084106153, msoFalse, msoScaleFromTopLeft
ActiveSheet.ChartObjects("waterfall").Activate
ActiveChart.Legend.Select
Selection.Delete
ActiveSheet.ChartObjects("waterfall").Activate
ActiveChart.SeriesCollection(1).Select
Selection.Format.Fill.Visible = msoFalse
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).Points(6).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
ActiveChart.SeriesCollection(2).Points(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
ActiveChart.SeriesCollection(2).Points(5).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
.Solid
End With
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.SeriesCollection(2).Points(1).Select
ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(2).Points(1).Select
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection(2).Select
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleHorizontal)
Selection.Caption = "hrs"
ActiveChart.Axes(xlValue).AxisTitle.Select
Selection.Left = 7
Selection.Top = 13.028
我试过了
Sheets("statistics").Range("A101:C106").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnStacked
ActiveChart.SetSourceData Source:=Range("statistics!$A$101:$C$106")
ActiveChart.ChartArea.Select
Set ThisChart = ActiveChart
ActiveSheet.Shapes(ThisChart).Name = "waterfall"
但它不起作用
答案 0 :(得分:2)
尝试下面的代码,它会遍历所有现有的ChartObjects
"统计信息"工作表,如果找到名称为&#34的图表对象;图9"它会将其重命名为" waterfall"。
注意:您可以使用类似的方法创建图表,而无需使用Select
,ActiveSheet
和ActiveChart
。
<强>代码强>
Option Explicit
Sub RenameExistingChart()
Dim ChtObj As ChartObject
For Each ChtObj In Worksheets("statistics").ChartObjects
If ChtObj.Name = "Chart 9" Then
ChtObj.Name = "waterfall"
End If
Next ChtObj
End Sub
修改1 :使用ChtObj
创建图表:
Set ChtObj = Worksheets("statistics").ChartObjects.Add(Left:=100, Top:=100, _
Width:=100, Height:=100) ' <-- just default settings , modify later
With ChtObj
.Chart.ChartType = xlColumnStacked
.Chart.SetSourceData Source:=range("statistics!$A$101:$C$106")
.Name = "waterfall"
With .Chart.SeriesCollection(2).Format.Fill ' modify fill for series (2)
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
.Chart.SeriesCollection(1).ApplyDataLabels ' add data lables to series (1)
End With
答案 1 :(得分:1)
您可以使用以下内容:
Sub ChartStuff()
Dim cht As Shape
Range("A101:A106").Select
ActiveSheet.Shapes.AddChart.Select
Set cht = ActiveSheet.Shapes(1)
cht.Name = "waterfall"
End Sub
希望这有帮助!
答案 2 :(得分:1)
在VBA中处理图表有点复杂。
当您使用Addchart
时,选择将是ChartArea
ChartArea
是图表的一部分,该图表是ChartObject
的一部分
您看到的图表名称实际上是ChartObject
您可以这样做:
Range("A101:A106").Select
ActiveSheet.Shapes.AddChart.Select
Dim ca As ChartArea, ch As Chart, co As ChartObject
Set ca = Selection
Set ch = ca.Parent
ch.ChartType = xl3DColumn
Set co = ch.Parent
co.Name = "waterfall"
Debug.Print ca.Name, ch.Name, co.Name
答案 3 :(得分:0)
创建一个从子内部调用的函数,该函数发送活动图表的名称,例如:
Function actchart(ActiveChart As String) As String
actchart = ActiveChart
End Function
然后从您的子目录中,例如,替换为您拥有的位置:
ActiveSheet.Shapes("Chart 9").Name = "waterfall"
与
ActiveSheet.Shapes(actchart(ActiveChart.Parent.Name)).Name = "waterfall"
这对我也有同样的问题!希望对您有所帮助。