英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论