2016-06-08 5 views
0

차트에 축 레이블을 추가하려고합니다. 아래는 내 코드의 일부입니다.Excel VBA - 차트 축 레이블 서식 지정

첫 번째 코드에서는 주 축 제목을 추가하는 줄을 실행할 때 오류가 발생하지 않습니다. 오류를 등록하지 않더라도 내 차트에는 여전히 축 제목에 대한 텍스트 상자가 없습니다.

둘째, 보조 축 제목을 추가하려고 할 때 내 코드가 오류를 생성하는 이유가 확실하지 않습니다. 매크로 레코더에서이 코드 섹션을 복사 했으므로 구문이 올바른지 확실히 알 수 있습니다.

도움 주셔서 감사합니다.

Dim myChart As Chart, cht As ChartObject 
Dim rngChart As Range, destSht As String 
Dim rngData As Range 

destSht = ActiveSheet.Name 
Set myChart = Charts.Add 
Set myChart = myChart.Location(where:=xlLocationAsObject, Name:=destSht) 

With ActiveSheet.ListObjects("Table1").DataBodyRange 
Set rngData = Union(.Columns(2), .Columns(9), .Columns(10), .Columns(11), .Columns(12)) 
End With 

With myChart 
.SetSourceData Source:=rngData, PlotBy:=xlColumns 
.ChartType = xlColumnClustered 
.ChartStyle = 209 
.FullSeriesCollection(1).Name = "AHT" 
.SetElement (msoElementChartTitleAboveChart) 
    .ChartTitle.Text = "AHT/Transfer Metrics" 

'*****PROBLEM SECTION***** 
.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis) 
'*****Below is the line of code that is giving me an error***** 
.SetElement (msoElementSecondaryValueAxisTitleAdjacentToAxis) 
'******    ***** 

With .FullSeriesCollection(2) 
    .Name = "Target AHT" 
    .ChartType = xlLine 
    .AxisGroup = 1 
    .Select 
     With Selection.Format.Line 
      .Weight = 1 
     End With 
End With 
With .FullSeriesCollection(3) 
    .ChartType = xlLineMarkers 
    .AxisGroup = 2 
    .Name = "Transfers" 
    .Select 
     With Selection.Format.Line 
      .Weight = 1.75 
     End With 
End With 
With .FullSeriesCollection(4) 
    .ChartType = xlLine 
    .AxisGroup = 2 
    .Name = "Target Transfers" 
    .Select 
     With Selection.Format.Line 
      .Weight = 1 
     End With 
End With 
End With 

답변

1

아직 보조 축을 만들지 않았기 때문에 이것이라고 생각합니다. 모든 시리즈의 서식을 지정한 후 해당 두 줄을 넣으십시오.

+0

두 번째로 설명한 문제가 해결되었습니다. 그러나 여전히 기본 축 제목 상자를 채우지 않습니다. 나는 이것에 대해 오류가 발생하지 않는다, 그것은 단지 내 코드를 무시하는 것 같습니다. –

2
With .Axes(xlValue, xlPrimary) 
     .HasTitle = True 
     .AxisTitle.Text = "AHT (s)" 
    End With