2012-06-04 238 views
1

我使用Excel VBA創建圖表,我想調整線條顏色和x軸間隔。Excel VBA - 調整顏色線和X軸間隔

下面是代碼:

Sub add_cpu_chart(server_hostname, site) 
    On Error Resume Next 

    Dim rpt_site As String 
    rpt_site = site 

    Set tbl = ThisWorkbook.Sheets(server_hostname).ListObjects(1) 
    strTableName = tbl.Name 
    strTableRange = tbl.Range.Address 

    Dim m_s_name, m_e_name As Date 

    m_s_name = CDate(fromDateStr) 
    m_e_name = CDate(toDateStr) 

    'Create new embedded chart 
    Set shp = ThisWorkbook.Sheets(server_hostname).Shapes.AddChart 

    'Position Shape over range 

    shp.Top = 100 
    shp.Left = 200 

    With shp 
     With .Chart 
      'Specify source data and orientation 
      '.SetSourceData Source:=ActiveSheets.Range(Table1 & "[#All]"), _ 
      'PlotBy:=xlRows 
      .SetSourceData Source:=ThisWorkbook.Sheets(server_hostname).Range(strTableName & "[#All]") 
      .ChartType = xlLineMarkers 
      .SetElement (msoElementChartTitleAboveChart) 
      .HasTitle = True 
      .ChartTitle.Text = "CPU Utilization of " & server_hostname & " in " & _ 
      rpt_site & " (" & Format(m_s_name, "dd-Mmm") & " to " & Format(m_e_name, "dd-Mmm yyyy") & ")" 
      .ChartTitle.Font.Size = 14 
      '.ChartArea.Font.Size = 14 
      .Legend.Delete 

      .SetElement (msoElementPrimaryValueAxisTitleRotated) 
      .Axes(xlValue, xlPrimary).AxisTitle.Text = "CPU Utlization (%)" 
      .Axes(xlValue, xlPrimary).AxisTitle.Font.Size = 10 
      .Axes(xlValue).MinimumScale = 0 
      .Axes(xlValue).MinorUnit = 2 
      .Axes(xlValue).MaximumScale = 100 
      .Axes(xlValue).MajorUnit = 20 
      .Axes(xlValue).TickLabels.NumberFormat = "General" 

      .SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis) 
      .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Date of the Month" 
      .Axes(xlCategory, xlPrimary).AxisTitle.Font.Size = 10 
      '.Axes(xlCategory).MajorUnit = 1 
      .Axes(xlCategory).TickLabels.NumberFormat = "d" 
      With .PlotArea.Format.Fill 
       .Visible = msoTrue 
       .ForeColor.ObjectThemeColor = msoThemeColorBackground1 
       .ForeColor.TintAndShade = 0 
       .ForeColor.Brightness = -0.150000006 
       .Transparency = 0 
       .Solid 
      End With 
     End With 
    End With 
End Sub 

我想線的顏色變爲紅色 - 在2003年的excel - 以及改變2天分離間隔中的數據。目前,數據的格式爲1,2,3,4...我想以1,3 ,5...的形式顯示數據。

+3

'我想說明1,3,5..'都到哪兒去你卡住了?你遇到了什麼錯誤?雖然我懷疑你在開始時出現「On Error Resume Next」錯誤。刪除該行:) –

+0

@Ivan Chan,如果您發現解決您的問題的有用解決方案,請將其標記爲已接受。否則,人們可能會猶豫不決以有用的方式回答你的問題。 – JackOrangeLantern

回答

6

在折線圖中,X軸'minimumscale,maximum scale,majorunit和minorunit是沒有意義的。即使標籤顯示數字,X值也只是沒有解釋數值的類別。

您應該更改爲XY圖表,該圖表允許使用minimumscale,maximum scale,majorunit和minorunit完全控制軸標度。當然,XY圖表允許使用與折線圖相同的系列標記和線條格式,但混淆的術語將永遠存在。

如果由於某種原因,你決定要堅持一個折線圖,您可以使用 ActiveChart.Axes(xlCategory).TickLabelSpacing = 2

+0

從圖形管理員處獲得+1。 thx喬恩 –

+0

我不知道如何接受答案 –