Since my previous post was closed, but the problem remains, I’ll rephrase it here. I’ve came up with the following:
Function JoinLastInColIfEmpty(range_ As Range, delim_ As String)
Dim cell As Range, result As String, current As String
For Each cell In range_
current = LastNonEmptyInCol(cell)
If current <> "" Then
result = result & current & delim_
End If
Next
If Not IsEmpty(result) Then
result = Left(result, Len(result) - Len(delim_))
End If
JoinLastInColIfEmpty = result
End Function
Function LastNonEmptyInCol(cell_ As Range)
Dim tmp As Range
tmp = cell_ '<< The problem occurs here
Do Until Not IsEmpty(tmp) Or tmp.Row = 1
tmp = tmp.Offset(-1, 0)
Loop
LastNonEmptyInCol = tmp.Value
End Function
The problem is that the function never ends, so my questions are:
- What is wrong with my script?
- What should I do to solve my problem?
To answer your direct question, there are a couple of errors in
LastNonEmptyInColThat said, I think it is a very inefficient solution, and does not quite solve your stated problem
results will be
Here’s another version which might be better