繁体   English   中英

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

[英]excel pass cell content to udf as range argument

我目前正在尝试将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

最佳T

间接使用:

=ConcatItNoDuplicities(INDIRECT(A1))

另一种方法。

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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