ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=Sheet1!$C$4"
ActiveChart.FullSeriesCollection(1).Values = "=Sheet1!$D$4:$AC$4"
ActiveChart.FullSeriesCollection(1).XValues = "=Sheet1!$D$3:$AC$3"
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Consumption"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Consumption"
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 = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 12
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.Axes(xlCategory).AxisTitle.Select
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Weeks"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Weeks"
With Selection.Format.TextFrame2.TextRange.Characters(1, 5).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 5).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 12
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
Range("H7").Select
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveSheet.Shapes("Chart 2").IncrementLeft -206.25
ActiveSheet.Shapes("Chart 2").IncrementTop -33.75
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveSheet.Shapes("Chart 2").ScaleWidth 2.3979166667, msoFalse, _
msoScaleFromTopLeft
ActiveChart.SetElement (msoElementDataLabelTop)
嘿大家,我用记录宏构建了一个图表。问题是宏不能构造其中一个轴标题,因此它给出了错误。另请注意,构建图表时,它没有轴标题。当您按“+”时,您可以添加轴标题,您将有两个。在代码中它似乎也试图添加,但实际上它根本不起作用。由于我看不到任何代码错误,我找不到任何解决方案。也许你们可以看到一些东西,或者你们知道更好的方法来添加标题。感谢
答案 0 :(得分:1)
Hakandeep!
不确定这是如何在没有任何帮助的情况下花了14个小时,但是你走了。这应该可以完全解决您的问题。
Sub MakinAChart()
Dim mychart As ChartObject
Set mychart = ThisWorkbook.Sheets(1).ChartObjects("Chart 1")
With mychart.Chart
.Axes(1, 1).HasTitle = True
.Axes(1, 1).AxisTitle.Text = "bottom text" '1 = xlCategory, 1 = xlPrimary
.Axes(2, 1).HasTitle = True
.Axes(2, 1).AxisTitle.Text = "left text" '2 = xlValue, 1 = xlPrimary
End With
End Sub
图表通常默认启用x轴标题。但是y轴不是,除非你点击它。由于Excel的图表UI的行为方式,当通过录制宏观看时,它不会隐式将其设置为true。
您也可以在命名轴之前的任何位置插入ActiveChart.Axes(2, 1).HasTitle = True
。良好的形式也是为你的x轴做同样的事情。
干杯!