将数字条(列)图轴转换为文本轴VBA

时间:2018-04-18 18:59:42

标签: vba excel-vba excel

大家好我今天写了这篇文章,根据我的工作表中的一些数据创建了一个条形图,但它将我的列数作为数字处理(而是用1-12标记x轴)。有没有办法操纵它以将列作为文本,因此使轴#1; 1月,2月,等等。

这也可能适用于我认为的分箱数据名称。

'Create Range for Y
   Dim rng1Y As Range, rng2Y As Range
   Dim Y_Range As Range

With ThisWorkbook.Sheets("Graphs")
Set rng1Y = .Cells(1, A + 7)

' Prevents Crashing if no data is in column
    If Cells(1, A + 7) = "" Then

    Else

    Set rng2Y = .Cells(1, A + 7).End(xlDown)

    Set Y_Range = .Range(rng1Y.Address & ":" & rng2Y.Address)
    Y_Range.Select

    End If


'Adds a second Series if needed
If Cells(2, A + 8) = "" Then

Else

 'Create Range for X
    Dim rng1X As Range, rng2X As Range
    Dim X_Range As Range

With ThisWorkbook.Sheets("Graphs")
Set rng1X = .Cells(1, A + 8)

'prevents crashing if no data is in column
If Cells(2, A + 8) = "" Then

Else

Set rng2X = .Cells(1, A + 8).End(xlDown)

Set X_Range = .Range(rng1X.Address & ":" & rng2X.Address)
X_Range.Select

End If


 ' Creates full range for Bar graph
Set Total_Range = .Range(Y_Range.Address & ":" & X_Range.Address)

Total_Range.Select

End With
'-------------------------------------------------------------------------

'Build chart

Dim Sh As Worksheet
Dim chrt As Chart

Set chrt = Nothing
Set Sh = ActiveWorkbook.Worksheets("Graphs")
Set chrt = Sh.Shapes.AddChart.Chart
With chrt


    'Titles
    .HasTitle = True
    .ChartTitle.Characters.Text = Cells(i + 12, 2).Value
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Title Here X"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Title here Y"
    .Axes(xlCategory).HasMajorGridlines = True
    .HasLegend = True

    End With
End If

End With
End Sub

0 个答案:

没有答案