excel将单元格内容传递给udf作为范围参数

时间:2017-11-30 19:18:46

标签: excel vba user-defined-functions

我目前正在尝试将excel中用户定义的函数传递给单元格的内容作为参数。

即,我计算了我对单元格感兴趣的范围,在这里我得到类似“sheet1!X17:X37”的内容。

现在我想将这个单元格(例如A1)传递给udf。例如,我想在B1中使用“= myfunction(A1)”而不是“= myfunction(sheet1!X17:X37)。

有什么想法吗?

我的功能是这样的:

Public Function ConcatItNoDuplicities(ByVal cellsToConcat As Range) As String
    ConcatItNoDuplicities = ""
    If cellsToConcat Is Nothing Then Exit Function
    Dim oneCell As Range
    Dim result As String
    For Each oneCell In cellsToConcat.Cells
        Dim cellValue As String
        cellValue = Trim(oneCell.Value)
        If cellValue <> "" Then
            If InStr(1, result, cellValue, vbTextCompare) = 0 Then _
                result = result & cellValue & ","
        End If
    Next oneCell
    If Len(result) > 0 Then _
        result = Left(result, Len(result) - 1)
    ConcatItNoDuplicities = result
End Function
最佳

Ť

2 个答案:

答案 0 :(得分:0)

使用间接:

=ConcatItNoDuplicities(INDIRECT(A1))

答案 1 :(得分:0)

另一种方法。

A1 包含看起来像单元格块地址的文本。这个小的UDF()执行目标的简单连接:

Public Function TakeOneStepBeyond(rng1 As Range) As String
    Dim rng2 As Range, r As Range

    Set rng2 = Range(rng1.Value)

    TakeOneStepBeyond = ""
    For Each r In rng2
        TakeOneStepBeyond = TakeOneStepBeyond & r.Value
    Next r
End Function

enter image description here