2016-08-02 102 views
2

我想用excel vba創建堆積柱狀圖。 input data創建堆積柱狀圖的問題

以下提到的是我的excel vba代碼,用於爲相應的輸入數據生成堆棧柱狀圖。

 Sub to_draw_chart() 
     ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select 
     ActiveChart.SetSourceData Source:=Range("Sheet2!$A$1:$P$4") 
     ActiveChart.Axes(xlValue).Select 
     ActiveChart.Axes(xlValue).MaximumScale = 1000 
     ActiveChart.Axes(xlValue).MajorUnit = 250 

     ActiveChart.ChartArea.Select 
     ActiveChart.Axes(xlCategory).Select 

     ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale 
     ActiveChart.Axes(xlCategory).CategoryType = xlAutomatic 
     Application.CommandBars("Format Object").Visible = False 
     ActiveChart.FullSeriesCollection(2).Select 
     ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale 
     Selection.Format.Fill.Visible = msoFalse 
     ActiveChart.FullSeriesCollection(5).Select 
     Selection.Format.Fill.Visible = msoFalse 
     ActiveChart.FullSeriesCollection(12).Select 
     Selection.Format.Fill.Visible = msoFalse 
     ActiveChart.FullSeriesCollection(15).Select 
     Selection.Format.Fill.Visible = msoFalse 

     ActiveChart.ChartArea.Select 
     ActiveChart.ChartTitle.Select 
     ActiveChart.ChartTitle.Text = "Chart " 
     Selection.Format.TextFrame2.TextRange.Characters.Text = "Chart " 
     End Sub 

outputExpected_VS_output_getting

但是當我運行這個宏,我得到的輸出是不同的。問題出在x軸上。我的X軸應該是D0,D1,D2(正如你可以在圖像「Output Expected」中看到的那樣,但是它不同,我也附加了當我運行vba代碼時得到的輸出(第二幅圖像)

我不理解爲什麼我的X軸得到改變這確實影響了代碼和輸出圖形。

手動當我不使用代碼,然後我得到正確的輸出做。

我要去哪裏錯誤?

+0

看到我的答案在下面(另一個選項來處理圖表對象) –

+0

是的,它的工作。非常感謝你。 – user28

+0

然後請upvote和標記爲答案 –

回答

0

您需要切換行/列加入ActiveChart.PlotBy = xlColumns。以下內容應解決問題。

Sub to_draw_chart() 
    ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select 
    ActiveChart.SetSourceData Source:=Range("Sheet2!$A$1:$P$4") 
    ActiveChart.PlotBy = xlColumns 
    ActiveChart.Axes(xlValue).Select 
    ActiveChart.Axes(xlValue).MaximumScale = 1000 
    ActiveChart.Axes(xlValue).MajorUnit = 250 

    ActiveChart.ChartArea.Select 
    ActiveChart.Axes(xlCategory).Select 

    ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale 
    ActiveChart.Axes(xlCategory).CategoryType = xlAutomatic 
    Application.CommandBars("Format Object").Visible = False 
    ActiveChart.FullSeriesCollection(2).Select 
    ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale 
    Selection.Format.Fill.Visible = msoFalse 
    ActiveChart.FullSeriesCollection(5).Select 
    Selection.Format.Fill.Visible = msoFalse 
    ActiveChart.FullSeriesCollection(12).Select 
    Selection.Format.Fill.Visible = msoFalse 
    ActiveChart.FullSeriesCollection(15).Select 
    Selection.Format.Fill.Visible = msoFalse 

    ActiveChart.ChartArea.Select 
    ActiveChart.ChartTitle.Select 
    ActiveChart.ChartTitle.Text = "Chart " 
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Chart " 
    End Sub 
+0

非常感謝你。它的工作:) – user28

+1

@ user28然後請upvote和標記爲答案 –

1

由於我沒有使用一些代碼行,所以不確定你提供的代碼是什麼樣的期望的圖表。

還有另一種選擇,「更乾淨」和更有效的方式來處理圖表(沒有必要一直選擇它們,我想你是用MACRO錄像機來做的)。

無論如何,看我的代碼如下,它給了我像手動步驟一樣的確切結果(就像在你的附加圖片中一樣)。

Option Explicit 

Sub to_draw_chart() 

Dim Sht1     As Worksheet 

' modify to your sheet name 
Set Sht1 = ThisWorkbook.Sheets("Sheet1") 

' change Left, Top, Width , Height according to your needs 
Sht1.Shapes.AddChart(xlColumnStacked, 200, 200, 500, 500).Select 

With ActiveChart 
    .SetSourceData Source:=Range("Sheet2!$A$1:$P$4") 
    .Axes(xlValue).MaximumScale = 1000 
    .Axes(xlValue).MajorUnit = 250 

    .HasTitle = True 
    .ChartTitle.Text = "Chart " 
    .ChartTitle.Format.TextFrame2.TextRange.Characters.Text = "Chart " 

    .Axes(xlCategory).CategoryType = xlCategoryScale 
    .Axes(xlCategory).CategoryType = xlAutomatic 

' not sure what is the purpose with the lines below ?  
'   Application.CommandBars("Format Object").Visible = False 
'   ActiveChart.FullSeriesCollection(2).Select 
'   ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale 
'   Selection.Format.Fill.Visible = msoFalse 
'   ActiveChart.FullSeriesCollection(5).Select 
'   Selection.Format.Fill.Visible = msoFalse 
'   ActiveChart.FullSeriesCollection(12).Select 
'   Selection.Format.Fill.Visible = msoFalse 
'   ActiveChart.FullSeriesCollection(15).Select 
'   Selection.Format.Fill.Visible = msoFalse 
End With 

End Sub