简体   繁体   English

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

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

I'm currently trying to pass to a user defined function in excel the contents of an cell as argument. 我目前正在尝试将excel的内容作为参数传递给用户定义的函数。

Namely, i calculate the range i'm interested in a cell, where i get something like this "sheet1!X17:X37". 即,我计算出我对某个单元格感兴趣的范围,在该单元格中会得到类似“ sheet1!X17:X37”的信息。

Now i want to pass this cell (eg A1) to a udf. 现在我想将此单元格(例如A1)传递给udf。 For example i want in B1 to have "=myfunction(A1)" as opposed to "=myfunction(sheet1!X17:X37). 例如,我希望B1中具有“ = myfunction(A1)”,而不是“ = myfunction(sheet1!X17:X37)”。

Any ideas? 有任何想法吗?

My function is like this: 我的功能是这样的:

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

Best T 最佳T

间接使用:

=ConcatItNoDuplicities(INDIRECT(A1))

Another approach. 另一种方法。

Say A1 contains text that looks like a cell block address. A1包含看起来像一个单元块地址的文本。 This tiny UDF() does a simple concatenation of the target: 这个微小的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