VBAmacros将柱形图中的垂直轴更改为select中的最小值和最大值

我想根据我的select以编程方式生成柱状图。 但是,我想垂直轴值是select中的最小值和最大值。 我认为这是可以通过WorksheetFunction.Max(DataRange)虽然这似乎调整水平轴,我不知道垂直轴值来自哪里。

例如,如果这是我select的数据 在这里输入图像说明 由下面的macros产生的图表如下所示:

在这里输入图像说明

但是,我想垂直轴为1-5,水平轴为频率值(即发生了多less次)。 我该怎么做呢?

此外,我是新来的Excel,所以如果你看到其他地方的改进,我会很感激input。

 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 

如果在Excel中加载了分析工具包,则可以在创build图表之前将数据转换为直方图。

在function区的“数据”选项卡上,“分析”面板中将显示“数据分析”。 点击它,然后从列表中select直方图。

将启动一个向导,要求input数据范围,容器范围和输出范围。 您可以预先设置您的垃圾箱范围,在您的情况下,这只是数字1到5.当您的数据变得更加复杂时,您可以使用MINMAX工作表函数来帮助确定垃圾箱。

在这里输入图像说明

你会注意到在上面的图片中,bin范围是用实际数据上面的1个空白单元定义的。 Excel需要这额外的行,但我不知道为什么。 编辑空白行是这样,你可以用列标题来标记你的垃圾箱。

一旦你有输出(绿色单元格),你可以很容易地作为一个条形图。

如果你想(我有过去),你可以做所有这些vba代码,但它涉及一些严重的vba编码。 我会build议坚持Excel的内置function,除非你真的需要自动化整个过程。

编辑

有一个代码项目文章/提示/技巧位于这里 ,应该让你几乎所有的方式来自动化您的解决scheme。

为了后人,我创build了一个产生直方图的macros,假设箱的数量= 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