Public Function concat(r As Range) As String
concat = ""
For Each rr In r
concat = concat & rr.Value
Next rr
End Function
我已经嵌入了以上功能,但是需要解决以下问题:
我有700个单独的英语单词(从A1到A700),我想把它组合到一个单个单元格中(通过使用= concat(A1:A700)但每个单词后面都有空格。意思是说当前我我正在使用所述函数,然后在单个单词之间没有出现空格,这就是问题所在。
答案 0 :(得分:4)
我觉得很难写这个
Public Function concat(r As Range) As String
concat = ""
For Each rr In r
concat = concat & rr.Value & " "
Next rr
concat = Trim(concat) 'Removes leading and trailing spaces from a string.
End Function
答案 1 :(得分:3)
在rr.Value
Public Function concat(r As Range) As String
concat = ""
For Each rr In r
concat = concat & rr.Value & " "
Next rr
End Function
答案 2 :(得分:0)
For Each rr In r
concat = concat & rr.Value & " "
Next rr
答案 3 :(得分:0)
我知道这是一个古老的问题,但有一个微妙的观点,没有其他答案得到解决。 OP要求在单词之间留出空格,并且没有说明在最后一个单词之后的空格是可接受的还是期望的。在rr.Value前面移动空格的赋值而不是之后意味着最后的字符串以最后一个单词的最后一个字符结束,而不是以空格结尾。解决尾随空间并不困难,但在某些情况下这可能很重要。
Public Function concat(r As Range) As String
concat = ""
For Each rr In r
if not isnull(concat) then
' most of the time concat is not null, so use that to
' identify the route to take because of this evaluation
concat = concat & " " & rr.Value
else
' when concat is null, avoid padding with the space
concat = rr.Value
end if
Next rr
End Function
答案 4 :(得分:0)
我稍微修改了Thinkingcap的解决方案。谢谢你的帮助。
Public Function concat(r As Range) As String
concat = ""
For Each rr In r
If rr.Value = "" Then GoTo Skip
concat = concat & rr.Value & " "
Skip:
Next rr
concat = Trim(concat) 'Removes leading and trailing spaces from a string.
End Function