VBAmacros来自动化直方图投掷错误400

我写了一个macros来产生一个直方图,给出一定的select。 macros的代码如下所示

Sub HistogramHelper(M As Range) Dim src_sheet As Worksheet Dim new_sheet As Worksheet Dim selected_range As Range Dim r As Integer Dim score_cell As Range Dim num_scores As Integer Dim count_range As Range Dim new_chart As Chart Set selected_range = M Set src_sheet = ActiveSheet Set new_sheet = Application.Sheets.Add(After:=src_sheet) title = selected_range.Cells(1, 1).Value 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 If Not IsNumeric(score_cell.Text) Then 'MsgBox score_cell.Text Else new_sheet.Cells(r, 1) = score_cell End If 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 = xlBarClustered .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 _ _ = "Out of " & num_scores & " responses" ' 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 

我目前正在使用一个看起来像这样的工作簿: 在这里输入图像说明

最后,我想要生成一个自动迭代每列的macros,每列都调用Histogram Helper函数,在多个工作表上生成多个直方图。 现在,我只是试图将两个范围放到HistogramHelper中,如下所示:

 Sub GenerateHistograms() HistogramHelper Range("D3:D30") HistogramHelper Range("E3:E30") End Sub 

但是,在运行macros时,出现一个错误号为400的对话框,其中一个工作表成功生成,工作表标题为Speaker,另一个工作表生成一个数字标题,没有内容。

到底是怎么回事?

编辑 :有问题的工作簿: https : //docs.google.com/file/d/0B6Gtk320qmNFbGhMaU5ST3JFQUE/edit?usp =分享

编辑2-主要的WTF?

为了debugging的目的,我把开头的FOR块切换到了这里:

 For Each score_cell In selected_range.Cells If Not IsNumeric(score_cell.Text) Then MsgBox score_cell.Address 'Find which addresses don't have numbers Else new_sheet.Cells(r, 1) = score_cell End If r = r + 1 Next score_cell 

每当你运行这个程序时,不pipe你把哪个范围作为第二次macros调用(在这种情况下是E3:E30),程序打印出每个单元$ E $ 3- $ E $ 30是一个非文本字符。 为什么哦为什么?

你不需要吗?

 Sheets(title).Activate 

提示:对于这种recursion实现来说,隐含着许多创build/删除操作,并且每天都变得越来越复杂,我不会依赖于“活动”元素(工作表,范围等),而是依赖于特定的元素(表单(“无论什么“))避免问题,并缓解debugging。

————————更新

不,显然,你不需要它。 然后,更新selected_range.Cells(1, 1).Value ,使每个新工作表的值不同,因为这是引发错误的原因:创build两个具有相同名称的工作表。

————————更新2(下载电子表格后)

问题是我的想法:两个工作表创build相同的名称(呃…不完全是:其中一个传播表是打算在一个空variables后被调用)。 而这个问题的原因,我也是这么想的:依靠“主动元素”。 但问题不是在使用ActiveSheet时,而是在传递参数的时候:范围是在没有电子表格的情况下给出的,并且是从最后创build的电子表格中获取的。 因此,解决scheme:

 HistogramHelper Sheets("Sheet1").Range("D3:D30") HistogramHelper Sheets("Sheet1").Range("E3:E30") 

底线:不要依赖复杂情况下的“主动”/未正确定义的元素。