动态选择要在VBA功能中使用的单元格

时间:2019-01-18 15:10:59

标签: excel vba function dynamic range

我在Excel工作表中写了下面要动态使用的函数,该函数将评估在公式中选择的变量的值,并基于该值,在引用单元格中的单元格中执行简单的计算值不同的列,但在同一行。

我知道下面的代码有效,但是如何重写范围,以便可以将函数动态用于同一列的每一行?

Function AddedValue(TabSize As Integer)

Select Case TabSize

    Case 2
        AddedValue = Range("K3") * (Range("N3") * (Range("H3") * 0.001))

    Case 4
        AddedValue = Range("K3") * (Range("O3") * (Range("H3") * 0.001))

    Case 6
        AddedValue = Range("K3") * (Range("P3") * (Range("H3") * 0.001))

    Case 8
        AddedValue = Range("K3") * (Range("Q3") * (Range("H3") * 0.001))

    Case 10
        AddedValue = Range("K3") * (Range("R3") * (Range("H3") * 0.001))

End Select
End Function

5 个答案:

答案 0 :(得分:2)

您可以使用Application.Caller来检测正在运行功能的单元格:

Public Function AddedValue(TabSize As Integer)
    Application.Volatile
    r = Application.Caller.Row
    With Application.Caller.Parent.Rows(r)
        firstcell = .Cells(1, 11) ' column K
        Select Case TabSize
            Case 2
                secondcell = .Cells(1, 14) ' column N
            Case 4
                secondcell = .Cells(1, 15) ' column O
            Case 6
                secondcell = .Cells(1, 16) ' column P
            Case 8
                secondcell = .Cells(1, 17) ' column Q
            Case 10
                secondcell = .Cells(1, 18) ' column R
            End Select
        thirdcell = .Cells(8) ' column H
        AddedValue = firstcell * (secondcell * (thirdcell * 0.001))
    End With
End Function

或者,如果TabSize按照您的问题提出的建议是一致的-您也可以“计算”其中的哪一列:

Public Function AddedValue2(TabSize As Integer)
    Application.Volatile
    r = Application.Caller.Row
    With Application.Caller.Parent.Rows(r)
        firstcell = .Cells(1, 11) ' column K
        secondcell = .Cells(1, 13 + (TabSize / 2)) ' dynamic column from TabSize
        thirdcell = .Cells(8) ' column H
        AddedValue2 = firstcell * (secondcell * (thirdcell * 0.001))
    End With
End Function

答案 1 :(得分:0)

您可以为该行添加输入,然后将该行连接到您的范围字符串。像这样:

Function AddedValue(TabSize As Integer, iRow as Long)

Select Case TabSize

    Case 2
        AddedValue = Range("K" & iRow) * (Range("N" & iRow) * (Range("H" & iRow) * 0.001))

    Case 4
        AddedValue = Range("K" & iRow) * (Range("O" & iRow) * (Range("H" & iRow) * 0.001))

    Case 6
        AddedValue = Range("K" & iRow) * (Range("P" & iRow) * (Range("H" & iRow) * 0.001))

    Case 8
        AddedValue = Range("K" & iRow) * (Range("Q" & iRow) * (Range("H" & iRow) * 0.001))

    Case 10
        AddedValue = Range("K" & iRow) * (Range("R" & iRow) * (Range("H" & iRow) * 0.001))

End Select
End Function

答案 2 :(得分:0)

好吧,快速简便的方法是将行作为参数传递给函数。

Function AddedValue(TabSize As Integer, row As Long)

Select Case TabSize
    Case 2
        AddedValue = Range("K" & row) * (Range("N" & row) * (Range("H" & row) * 0.001))
    Case 4
        AddedValue = Range("K" & row) * (Range("O" & row) * (Range("H" & row) * 0.001))
    Case 6
        AddedValue = Range("K" & row) * (Range("P" & row) * (Range("H" & row) * 0.001))
    Case 8
        AddedValue = Range("K" & row) * (Range("Q" & row) * (Range("H" & row) * 0.001))
    Case 10
        AddedValue = Range("K" & row) * (Range("R" & row) * (Range("H" & row) * 0.001))
End Select
End Function

答案 3 :(得分:0)

这必须是VBA吗?此仅公式解决方案应为您工作:

=$H3*0.001*$K3*INDEX($N3:$R3,,1)

最后一个1是所需的该范围(N:R)中列的索引。因此1 =“使用列N”和2 =“使用列O”等。您可以简单地复制公式并粘贴行,它将自动调整行号,同时保持列数不变。一样。

此外,如果需要的话,您可以使用单元格引用而不是对“ Selected Column / TabSize”号进行硬编码(例如,本例中A1单元格中的“ Selected Column / TabSize”号):

=$H3*0.001*$K3*INDEX($N3:$R3,,A1)

答案 4 :(得分:0)

动态单元格计算

Option Explicit

Function AddedValue(TabSize As Long, row As Long) As Double

    Const cValue As String = "2,4,6,8,10"
    Const cStr1 As String = "K"
    Const cStr2 As String = "N,O,P,Q,R"
    Const cStr3 As String = "H"
    Const cDbl1 As Double = 0.001

    Dim vntV As Variant
    Dim vnt2 As Variant
    Dim i As Long

    vntV = Split(cValue, ",")
    vnt2 = Split(cStr2, ",")

    For i = 0 To UBound(vntV)
        If TabSize = CLng(Trim(vntV(i))) Then
            AddedValue = Range(cStr1 & row) * Range(Trim(vnt2(i)) & row) _
                    * Range(cStr3 & row) * cDbl1
            Exit For
        End If
    Next

End Function