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
我目前正在尝试将单元格的内容作为参数传递给 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