2017-06-14 313 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

相關問題