简体   繁体   中英

Excel VBA: Dynamically Changing Function Based on Column

Heylo, I am trying to write an excel function that takes a user-selected range and performs different calculations based on the column the cell being populated lines up with. The screenshot below shows the setup of the columns.

在此处输入图片说明

I want to set AA5 to be "=myFunction($AA1:$AD4)", and then I want click-and-drag to use the autofill feature to populate AB5, AC5, and AD5 with the same "=myFunction($AA1:$AD4)" but this myFunction will do different things based on which cell is being populated during the autofill.

I know how to do this in a subroutine where the user would select the first open cell AA5, and is prompted for the range to use for calculations. I would do something along the lines of:

Sub CalcCells()
   Dim myRange As Range
   Set myRange = Application.InputBox("Select the cells you want to use...", Type:=8)

   Dim numColumn As Long
   For numColumn = 0 To myRange.Columns.Count - 1
      Select Case numColumn
         Case Is = 0
            ActiveCell.Offset(0, numColumn).Formula = "=SUM(" + myRange.Columns(1) + ")"
         Case Is = 1
            ActiveCell.Offset(0, numColumn).Formula = "=SUMPRODUCT(" + myRange.Columns(1) + "," + myRange.Columns(2) + ")"
         Case Is = 2
            ActiveCell.Offset(0, numColumn).Formula = "=SUMPRODUCT(" + myRange.Columns(1) + "," + myRange.Columns(3) + ")/SUM(" + myRange.Columns(1) + ")"
         Case Is = 3
            ActievCell.Offset(0, numColumn).Formula = "=SUMSQ(" + myRange.Columns(4) + ")"
      End Select
   Next numColumn
End Sub

So basically I want to do exactly this, but I want it to be a function that when I click and drag and autofill AB5:AD5 it knows which column the cell lines up with and performs a calculation based on that, and uses it as an argument/parameter almost. It will not always be 4 rows either, so it needs to be capable of accommodating varying row counts, but the .Columns should work with that as long as the user selects only the same datatype.

Is this possible and how can I do it? Thank you for any help in advance. I've done a lot of searching and I don't know if I'm not searching the right way, but i cannot find anything that really helps.

What about something like this? Basically, you get the column of the cell you enter the formula into with Application.Caller.Column . Then inputRange.Column gives you the leftmost column of your input range. Based on the difference of the two, you know which worksheet function you want to use. If the difference is 0, your formula is entered in the 1st column, so you use Sum . If the difference is 1, you use Sumproduct , and so on.

Function SummarizeCells(inputRange As Range) As Double

    Dim col As Long
    col = Application.Caller.Column - inputRange.Column

    Select Case col
        Case 0
            SummarizeCells = WorksheetFunction.Sum(inputRange.Columns(1))
        Case 1
            SummarizeCells = WorksheetFunction.SumProduct(inputRange.Columns(1), inputRange.Columns(2))
        Case 2
            SummarizeCells = WorksheetFunction.SumProduct(inputRange.Columns(1), inputRange.Columns(3)) / WorksheetFunction.Sum(inputRange.Columns(1))
        Case 3
            SummarizeCells = WorksheetFunction.SumSq(inputRange.Columns(4))
    End Select

End Function

A sample view here:

产量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM