Excel VBA:显示数字的水平轴类别,但源数据具有文本标签

时间:2017-05-25 06:35:35

标签: excel vba excel-vba charts

我正在动态地将数据分配给图表。数据本身也是动态生成的。我的问题是,即使我的源数据包含字符串标签(例如Dept1,Dept2),图表也只在水平轴上显示数字(1,2)。我已经执行了“选择数据”的手动操作。在图表上,确保水平类别标签包含对标签的引用。下面的图片应该更好地解释它: Sample Image

我稍后粘贴了我的VBA代码。这是我在表格中执行的步骤:

  1. 向用户提供表格'选择要查看的数据 - 更新某些单元格中的值会动态从另一个工作表中提取数据并将其粘贴到我的主工作表中。数据总是两列 - 一列包含标签,另一列包含相关值。
  2. 当主页中的数据更新时,我会找到范围大小并将其作为源数据传递给图表,包括标签。
  3. 根据用户选择,图表类型在群集列和行之间有所不同。
  4. 以下是CSV格式的示例数据。请注意,我没有传递标题 样本1:

    Dept1,2105  
    Dept2,1569  
    Dept3,1891  
    Dept4,265  
    

    示例2:

    42614,693.5  
    42644,610.5  
    42675,851.5  
    42705,852 
    

    在样本2中,标签的格式为Mmm-yy日期(9月16日至12月16日)。

    这是VBA代码:

    Sub drawGraph(viewType As String)
    
    'This sub will be called by the sub responsible for prepping data
    'Each view format needs a separate type of visualisation to be shown
    'this sub will figure out which graph to show and show it
    
    Dim data As Range
    Dim firstRow As Long
    Dim lastRow As Long
    Dim cht As ChartObject
    Dim mainSheet As Worksheet
    Dim formats As Worksheet
    Dim formatRow As Long
    Dim formatCol As Long
    
    On Error GoTo errExit
    
    'Hide all graph columns
    Set mainSheet = ThisWorkbook.Worksheets("Main Sheet")
    mainSheet.Select
    mainSheet.Range(Cells(1, 12), Cells(1, 51)).EntireColumn.Hidden = True
    
    'View Type 1 has no graph, escape with if condition
    If Not viewType = "View Type 1" Then
        'Pre-prepared view types (graph, text, etc.) are placed in certain hidden columns, each spanning 9 columns. 
        'We need to find the starting column of the view type that we need.
        Set formats = ThisWorkbook.Worksheets("Formats Base")
        formats.Select
        formats.Columns("A:A").Select
        formatRow = Selection.Find(What:=viewType, _
                                After:=Range("A1"), _
                                LookAt:=xlPart, _
                                LookIn:=xlFormulas, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious, _
                                MatchCase:=False).Row
        formatCol = formats.Cells(formatRow, 4)
        mainSheet.Activate
        'unhide 9 columns
        'all graphs are designed to fit within 9 columns
        'Might not be the most efficient way to do this but it's the only one I know
        Union(Columns(formatCol), Columns(formatCol + 1), Columns(formatCol + 2), _
        Columns(formatCol + 3), Columns(formatCol + 4), Columns(formatCol + 5), _
        Columns(formatCol + 6), Columns(formatCol + 7), Columns(formatCol + 8), Columns(formatCol + 9)).EntireColumn.Hidden = False
    
        'Need to assign data to chart
        'View Type 2 does not have a graph, escape with if condition
        If Not viewType = "View Type 2" Then
            mainSheet.Cells(6, 22).Value = ""
            Set cht = mainSheet.ChartObjects("dataGraph")
            Select Case viewType
                Case "View Type 3"
                    cht.chart.chartType = xlColumnClustered
                Case "View Type 4 - Months"
                    cht.chart.chartType = xlLine
                Case "View Type 5"
                    cht.chart.chartType = xlColumnClustered
            End Select
            firstRow = 4
            lastRow = mainSheet.Range("H1").CurrentRegion.Rows.Count
            Set data = mainSheet.Range(Cells(firstRow, 8), Cells(lastRow, 9))
            cht.chart.SetSourceData Source:=data
    
        ElseIf viewType = "View Type 2" Then
            mainSheet.Cells(6, 22).Value = "=$I$3/$V$4"
        End If
    End If
    
    
    errExit:
    Application.ScreenUpdating = True
    
    End Sub
    

0 个答案:

没有答案