VBA宏将柱形图中的垂直轴更改为选择中的最小值和最大值

时间:2013-05-31 20:03:34

标签: excel vba

我想根据我的选择以编程方式生成柱形图。但是,我希望垂直轴值是选择中的最小值和最大值。我认为这可以通过WorksheetFunction.Max(DataRange)获得虽然这似乎调整水平轴,但我不确定垂直轴值的来源。

例如,如果这是我选择的数据enter image description here 下面的宏生成的图表如下所示:

enter image description here

但是,我希望垂直轴为1-5,水平轴为频率值(即该数字出现的次数)。我该怎么做?

另外,我是Excel的新手,所以如果你看到其他方面的改进,我会很感激输入。

Sub GenerateGraph()

    Dim MyChart As Chart
    Dim DataRange As Range
    Set DataRange = Selection

    Set MyChart = Charts.Add
    MyChart.SetSourceData Source:=DataRange
    ActiveChart.ChartType = xlBarClustered

    With ActiveChart.Axes(xlValue, xlPrimary)
        .MaximumScale = WorksheetFunction.Max(DataRange)
        .MinimumScale = WorksheetFunction.Min(DataRange)
        .MajorUnit = 1

    End With

2 个答案:

答案 0 :(得分:3)

如果您在Excel中加载了Analysis Toolpak,则可以在创建图表之前将数据转换为直方图。

在功能区的“数据”选项卡上,“分析”面板中将显示“数据分析”。单击此按钮,然后从列表中选择直方图。

将启动一个向导,询问数据范围,bin范围和输出范围。您可以事先设置您的bin范围,在您的情况下只是数字1到5.当您的数据变得更复杂时,您可以使用MINMAX工作表函数来帮助确定二进制位。

enter image description here

您将在上图中注意到,bin范围定义为在实际数据上方有1个空白单元格。 Excel需要这个额外的行,但我不确定为什么。 编辑空白行是您可以使用列标题标记您的垃圾箱。

获得输出(绿色单元格)后,您可以轻松地将其绘制为条形图。

你可以在vba代码中完成所有这些(如果你想要的话),但它涉及一些严肃的vba编码。我建议坚持使用Excel的内置功能,除非你真的需要自动化整个过程。

修改

代码项目文章/提示/技巧位于here,几乎可以让您自动化解决方案。

答案 1 :(得分:0)

对于子孙后代,我创建了一个生成直方图的宏,假设箱数= 5(如对调查问题的回答)。

' Make a histogram from the selected values.
' The top value is used as the histogram's title.
Sub MakeHistogramFinal()
Dim src_sheet As Worksheet
Dim new_sheet As Worksheet
Dim selected_range As Range
Dim title As String
Dim r As Integer
Dim score_cell As Range
Dim num_scores As Integer
Dim count_range As Range
Dim new_chart As Chart

    ' Add a new sheet.
    Set selected_range = Selection
    Set src_sheet = ActiveSheet
    Set new_sheet = Application.Sheets.Add(After:=src_sheet)
    title = InputBox(Prompt:="Enter Title for Histogram", _
          title:="Title Submission Form", Default:="Morning Session Summary")
    new_sheet.Name = title


    ' Copy the scores to the new sheet.
    new_sheet.Cells(1, 1) = "Data"
    r = 2
    For Each score_cell In selected_range.Cells
        new_sheet.Cells(r, 1) = score_cell
        r = r + 1
    Next score_cell
    num_scores = selected_range.Count


    'Creates the number of bins to 5
    'IDEA LATER: Make this number equal to Form data
    Dim num_bins As Integer
    num_bins = 5

    ' Make the bin separators.
    new_sheet.Cells(1, 2) = "Bins"
    For r = 1 To num_bins
        new_sheet.Cells(r + 1, 2) = Str(r)
    Next r

    ' Make the counts.
    new_sheet.Cells(1, 3) = "Counts"
    Set count_range = new_sheet.Range("C2:C" & num_bins + 1)

    'Creates frequency column for all counts
    count_range.FormulaArray = "=FREQUENCY(A2:A" & num_scores + 1 & ",B2:B" & num_bins & ")"

    'Make the range labels.
    new_sheet.Cells(1, 4) = "Ranges"
    For r = 1 To num_bins
        new_sheet.Cells(r + 1, 4) = Str(r)
        new_sheet.Cells(r + 1, 4).HorizontalAlignment = _
            xlRight
    Next r

    ' Make the chart.
    Set new_chart = Charts.Add()
    With new_chart
        .ChartType = xlColumnClustered
        .SetSourceData Source:=new_sheet.Range("C2:C" & _
            num_bins + 1), _
            PlotBy:=xlColumns
        .Location Where:=xlLocationAsObject, _
            Name:=new_sheet.Name
    End With

    With ActiveChart
        .HasTitle = True
        .HasLegend = False
        .ChartTitle.Characters.Text = title
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, _
            xlPrimary).AxisTitle.Characters.Text = "Scores"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text _
 _
            = "Count"

        ' Display score ranges on the X axis.
        .SeriesCollection(1).XValues = "='" & _
            new_sheet.Name & "'!R2C4:R" & _
            num_bins + 1 & "C4"

    End With
    ActiveChart.SeriesCollection(1).Select
    With ActiveChart.ChartGroups(1)
        .Overlap = 0
        .GapWidth = 0
        .HasSeriesLines = False
        .VaryByCategories = False

    End With

    r = num_scores + 2
    new_sheet.Cells(r, 1) = "Average"
    new_sheet.Cells(r, 2) = "=AVERAGE(A1:A" & num_scores & _
        ")"
    r = r + 1
    new_sheet.Cells(r, 1) = "StdDev"
    new_sheet.Cells(r, 2) = "=STDEV(A1:A" & num_scores & ")"
End Sub