0
軸名稱
我有下面的表中「CPU_STAT」工作表:Excel的VBA腳本以包括在圖形
我有一個「CPU_STAT_GRAPH」工作在其中我在拉同一工作簿我將用於生成圖表的列。
使用的代碼:
Sub test()
Dim sourceColumn1 As Range, targetColumn1 As Range
Dim sourceColumn2 As Range, targetColumn2 As Range
Dim sourceColumn3 As Range, targetColumn3 As Range
Set sourceColumn1 = Workbooks("KPI_stats_v1.xls").Worksheets("CPU_STAT").Columns("D")
Set targetColumn1 = Workbooks("KPI_stats_v1.xls").Worksheets("CPU_STAT_GRAPH").Columns("A")
Set sourceColumn2 = Workbooks("KPI_stats_v1.xls").Worksheets("CPU_STAT").Columns("G")
Set targetColumn2 = Workbooks("KPI_stats_v1.xls").Worksheets("CPU_STAT_GRAPH").Columns("B")
Set sourceColumn3 = Workbooks("KPI_stats_v1.xls").Worksheets("CPU_STAT").Columns("I")
Set targetColumn3 = Workbooks("KPI_stats_v1.xls").Worksheets("CPU_STAT_GRAPH").Columns("C")
sourceColumn1.Copy Destination:=targetColumn1
sourceColumn2.Copy Destination:=targetColumn2
sourceColumn3.Copy Destination:=targetColumn3
Range("A1:C5").Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmooth).Select
ActiveChart.SetSourceData Source:=Range("CPU_STAT_GRAPH!$A$1:$C$5")
ActiveChart.SetElement (msoElementPrimaryValueGridLinesNone)
ActiveChart.SetElement (msoElementPrimaryCategoryGridLinesNone)
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
ActiveChart.ChartTitle.Select
ActiveChart.ChartTitle.Text = "CPU Utilization"
Selection.Format.TextFrame2.TextRange.Characters.Text = "CPU Utilization"
With Selection.Format.TextFrame2.TextRange.Characters(1, 15).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
End Sub
在 「CPU_STAT_GRAPH」 的輸出是:如預期
一切。我需要的只是正確地重命名圖形的軸。縱座標爲Percent
,橫座標爲Hour
。
如何將其納入代碼?也正如你所看到的,我的代碼是一個非常基本的代碼,它沒有明確定義左邊的數據使用的X軸和Y軸。如何優化我的代碼用最少的代碼行,並使其更加智能化:)
[新手VBA編碼:)]