由於我的previous post已關閉,但問題依然存在,因此我將在此處對其進行更改。我用下面的上前:Excel:連接列中的最後一個非空單元格
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
的問題是,該函數永遠不會結束,所以我的問題是:
- 有什麼不對我的腳本?
- 我該怎麼做才能解決my problem?
感謝您的解決方案,我的工作正在進行中,因此沒有達到我的預期。然而,我仍然有一個問題:我怎麼知道什麼時候使用'Set'(在你的例子中'vData = range_'沒有工作)? – gregseth 2011-12-22 22:26:53
當你想分配一個對象時(比如分配一個指針),使用'Set'例如'Set Range1 = Range2'使'Range1'引用與Range2相同的範圍對象。在'vData = Range_'中將'range_'的默認屬性的值複製到變量'vData'中。範圍的默認屬性是'.Value',所以這裏'vData'將會從範圍中獲取值。如果'range_'參照> 1個單元格,它將是一個變體陣列 – 2011-12-22 22:32:05
知道了!非常感謝 :)。 – gregseth 2011-12-23 09:54:38