Histogram using Excel FREQUENCY function

后端 未结 2 1853
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 00:37

In Excel 2010, I have a list of values in column A and a bin size is specified in B1. This allows me to create histograms with N bins using this formul

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-22 00:59

    The only answer I can think of is to use a macro to resize the output range of your formula.

    Here's a simple snippet that illustrates the idea.

    Dim result As Variant
    Dim targetCols As Long
    
    result = Evaluate(fmla)
    With rng
      targetCols = UBound(result, 1) - LBound(result, 1) + 1
      .Resize(1, targetCols).FormulaArray = fmla
    End With
    

    I wrote about a more complete implementation last year - more error-tolerant , 2-dimensional outputs, etc

    EDIT: However... The formula you're using won't work with this approach: it relies on its output range size being known at input. Here's an alternative suggestion that can be automatically resized:

    We can create a set of workable bins with something like this:

    ={(ROW(OFFSET(A1,0,0,CEILING((MAX(A:A)-MIN(A:A))/B1,1)+1,1))-1)*B1}
    

    where as before this is the number of bins

    CEILING((MAX(A:A)-MIN(A:A))/B1,1)+1
    

    which we then use to create a range using OFFSET() (the target doesn't matter since we're not using its values). Then we take the ROW() of each cell in the range (subtracting 1 to get a set of values starting with zero) and multiply by our bin size. You may want to shift the range of values (by adding MIN(A:A) for example).

    The big difference is that this formula doesn't need to be input across a range for the Evaluate() VBA function to be able to produce a range output.

    To get the histogram, either plug the output from the bin formula into FREQUENCY() or drop in the whole formula. The auto-resizing should work either way.

    If you were particularly opposed to running a macro (I have it available via a custom Ribbon button and a hotkey combination) then you could use a Worksheet_Change event to watch for opportunities to apply it. I can't say for sure if that would have any unpleasant side-effects.

提交回复
热议问题