Excel图形强制数据源

时间:2016-12-01 13:57:26

标签: vba excel-vba excel

道歉,标题有点误导。而不是在excel中切换行/列(选择图形,然后在设计选项卡上,单击“切换行/列”),我想最初强制它,减少更改的需要。

我的代码如下,添加了行/列自动切换的另一列:

Sub InsertBar(rngToPrint As Range, lngTopleft As String, BottomLeft As String)
    Dim strRange As String
    Dim rngChart As Range
    Dim myChart As Chart

    lngStartRow = Sheets(rngToPrint.Worksheet.Name).Range(lngTopleft).Row
    lngEndRow = Sheets(rngToPrint.Worksheet.Name).Range(BottomLeft).Row

    Sheets(rngToPrint.Worksheet.Name).Activate
    'Correct
    'Sheets(rngToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$D$" & CStr(lngEndRow)).Select

    'Shows Flipped Axis
    Sheets(rngToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$E$" & CStr(lngEndRow)).Select

    Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart

    With myChart
        .ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
        .HasTitle = True
        .ChartTitle.Text = rngToPrint.Worksheet.Name & " Receiving Sim Stats - (Today Only)"
        .SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
        .SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
        .SeriesCollection(3).Name = Range("D" & lngStartRow - 1).Value

    'Dataseries which has just been added
        .SeriesCollection(4).Name = Range("E" & lngStartRow - 1).Value
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

对于其他感兴趣的人,我最终找到的答案是:

    With myChart
        .PlotBy = xlColumns

或者如果您想以另一种方式切换:

myChart.PlotBy = xlRows