2017-06-14 165 views
1

我在Excel 2013中使用了一个记录宏来选择数据并制作条形图。我的确切操作是我选择了A1,Ctrl + Shift +向下箭头,Ctrl + Shift +向右箭头,插入2d条形图,按下绿色的+符号表示图表元素,选定的轴名称,命名轴标题和图表标题,并停止录制宏。当代码到达标记y(值)轴的点时,我得到一个424错误的对象。我试过重命名这个第一和第二,并且无论如何都得到相同的错误。下面是实际的代码:运行时错误424 - 对象要求的错误

Sub BarGraph() 
' 
' BarGraph Macro 
' 

' 
    Range("A1").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range(Selection, Selection.End(xlToRight)).Select 
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select 
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$D$5") 
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis) 
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis) 
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Sales"  'This is the line that the failure starts at 
Selection.Format.TextFrame2.TextRange.Characters.Text = "Total Sales" 
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 
ActiveChart.Axes(xlCategory).AxisTitle.Select 
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Region" 
Selection.Format.TextFrame2.TextRange.Characters.Text = "Region" 
With Selection.Format.TextFrame2.TextRange.Characters(1, 6).ParagraphFormat 
    .TextDirection = msoTextDirectionLeftToRight 
    .Alignment = msoAlignCenter 
End With 
With Selection.Format.TextFrame2.TextRange.Characters(1, 6).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.ChartArea.Select 
ActiveChart.ChartTitle.Select 
ActiveChart.ChartTitle.Text = "Total Sales" 
Selection.Format.TextFrame2.TextRange.Characters.Text = "Total Sales" 
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 = 14 
    .Italic = msoFalse 
    .Kerning = 12 
    .Name = "+mn-lt" 
    .UnderlineStyle = msoNoUnderline 
    .Spacing = 0 
    .Strike = msoNoStrike 
End With 
Range("K7").Select 
End Sub 

我还试图通过选择添加图表元素,轴标题,主垂直从设计选项卡逐个添加标题,并得到了同样的问题。 我真的不知道这个问题会是什么,所以一些帮助将非常感谢,谢谢。

回答

3

你得到这个错误,因为你试图写入标题,由于它是不是能够找到它,它给你一个Object required error (424)。 试试这个

ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True 
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Sales" 

也请避免使用ActiveChart/Select等使用对象。

有趣的阅读上How to avoid using Select in Excel VBA macros

+0

感谢帮助,它现在正在工作。 – Anthrochange

相关问题