简体   繁体   中英

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.

Namely, i calculate the range i'm interested in a cell, where i get something like this "sheet1!X17:X37".

Now i want to pass this cell (eg A1) to a udf. For example i want in B1 to have "=myfunction(A1)" as opposed to "=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

间接使用:

=ConcatItNoDuplicities(INDIRECT(A1))

Another approach.

Say A1 contains text that looks like a cell block address. This tiny UDF() does a simple concatenation of the target:

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

在此处输入图片说明

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