Excel UDF giving value error if selected range is not from a single sheet

huangapple go评论81阅读模式
英文:

Excel UDF giving value error if selected range is not from a single sheet

问题

以下是您要翻译的内容:

"The following code is made to concatenate cells with a delimiter.
The Ref here can be a continuous range or a group of ranges put inside a bracket. The only limitation i'm
getting is that if the group of cells contain range from two different sheets the UDF gives value error.

Function CONCATENATEMULTIPLE(Ref As Range, Separator As String) As String
Dim Cell As Range
Dim Result As String
Dim plc2remove As Long
plc2remove = Len(Separator)

For Each Cell In Ref
If Not Cell.Value = "" Then
 Result = Result & Cell.Value & Separator
 End If
Next Cell
If Result = "" Then
CONCATENATEMULTIPLE = "NO DATA TO SHOW"
Else
CONCATENATEMULTIPLE = Left(Result, Len(Result) - plc2remove)
End If
End Function

I think the solution is to identify .parent property of each cell when iterating through it but I cannot make it work.

Concatenation works when group of ranges are from single sheet like this

But doesn't work when range from another sheet is also included like this"

请注意,代码部分未进行翻译。

英文:

The following code is made to concatenate cells with a delimiter.
The Ref here can be a continuous range or a group of ranges put inside a bracket. The only limitation i'm
getting is that if the group of cells contain range from two different sheets the UDF gives value error.

Function CONCATENATEMULTIPLE(Ref As Range, Separator As String) As String
Dim Cell As Range
Dim Result As String
Dim plc2remove As Long
plc2remove = Len(Separator)


For Each Cell In Ref
If Not Cell.Value = "" Then
 Result = Result & Cell.Value & Separator
 End If
Next Cell
If Result = "" Then
CONCATENATEMULTIPLE = "NO DATA TO SHOW"
Else
CONCATENATEMULTIPLE = Left(Result, Len(Result) - plc2remove)
End If
End Function


I think the solution is to identify .parent property of each cell when iterating through it but I cannot make it work.

Concatenation works when group of ranges are from single sheet like this

But doesn't work when range from another sheet is also included like this

答案1

得分: 0

你可以这样做:

Function myConcat(Sep As String, ParamArray Ref() As Variant) As String
    Dim s, c, m As String
    m = ""
    For Each s In Ref
        For Each c In s.Cells
            m = m & c.Value
            If Len(c.Value) > 0 Then m = m & Sep
        Next
    Next
    If Len(m) = 0 Then
        myConcat = "无数据可显示"
    Else
        myConcat = Left(m, Len(m) - Len(Sep))
    End If
End Function

它可以这样调用:

=myConcat(", ",Sheet1!A1,Sheet2!A2,Sheet1!A3,Sheet2!A1,A1:A2)

注意 - 这些范围不需要放在括号内。

英文:

You could do something like this:

Function myConcat(Sep As String, ParamArray Ref() As Variant) As String
    Dim s, c, m As String
    m = ""
    For Each s In Ref
        For Each c In s.Cells
            m = m & c.Value
            If Len(c.Value) > 0 Then m = m & Sep
        Next
    Next
    If Len(m) = 0 Then
        myConcat= "NO DATA TO SHOW"
    Else
        myConcat = Left(m, Len(m) - Len(Sep))
    End If
End Function

It can be called with something like:

=myConcat(", ",Sheet1!A1,Sheet2!A2,Sheet1!A3,Sheet2!A1,A1:A2)

Note - the ranges are not gathered in brackets.

huangapple
  • 本文由 发表于 2023年3月1日 14:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定