2011-12-22 119 views
1

由於我的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

回答

2

要回答你直接的問題,也有一對夫婦的錯誤LastNonEmptyInCol

Function LastNonEmptyInCol(cell_ As Range) 
    On Error Resume Next 
    Dim tmp As Range 
    Set tmp = cell_ '<< The problem occurs here ' <<<<< use Set 
    Do Until Not IsEmpty(tmp) Or tmp.Row = 1  ' <<<<< use tmp not cell_ 
     Set tmp = tmp.Offset(-1, 0)    ' <<<<< use Set 
    Loop 
    LastNonEmptyInCol = tmp.Value 
End Function 

這麼說,我認爲這是一個非常低效的解決方案,而並不完全解決您的所述問題

個結果將

A | B | C | D | Concat 
    -----+-----+-----+-----+--------- 
    1 | 2 | X | 5 | 12X5 
     |  | f | 3 | 12f3 
     | 5 | R | 12 | 15R12 
    Z | 3 | T |  | Z3T12 
     | G |  |  | ZGT12 

下面是另一個版本,這可能是更好的

Function MyJoinLastInColIfEmpty(range_ As Range, delim_ As String) 
    Dim vData As Variant 
    Dim cl As Range 
    Dim i As Long 
    Dim result As Variant 

    vData = range_ 

    For i = 1 To UBound(vData, 2) 
     If vData(1, i) = "" Then 
      Set cl = range_.Cells(1, i).End(xlUp) 
      If cl <> "" Then 
       vData(1, i) = cl.Value 
      End If 
     Else 
      Exit For 
     End If 
    Next 
    For i = 1 To UBound(vData, 2) 
     result = result & vData(1, i) & delim_ 
    Next 

    MyJoinLastInColIfEmpty = Left(result, Len(result) - Len(delim_)) 
End Function 
+0

感謝您的解決方案,我的工作正在進行中,因此沒有達到我的預期。然而,我仍然有一個問題:我怎麼知道什麼時候使用'Set'(在你的例子中'vData = range_'沒有工作)? – gregseth 2011-12-22 22:26:53

+0

當你想分配一個對象時(比如分配一個指針),使用'Set'例如'Set Range1 = Range2'使'Range1'引用與Range2相同的範圍對象。在'vData = Range_'中將'range_'的默認屬性的值複製到變量'vData'中。範圍的默認屬性是'.Value',所以這裏'vData'將會從範圍中獲取值。如果'range_'參照> 1個單元格,它將是一個變體陣列 – 2011-12-22 22:32:05

+0

知道了!非常感謝 :)。 – gregseth 2011-12-23 09:54:38

1

我真的不試着去了解整個事情,但由於TMP是(範圍)對象,則必須使用
Set tmp = ....

+0

錯,好的......但是爲什麼?更重要的是,我怎麼知道是否需要'Set'? – gregseth 2011-12-22 22:28:50

+0

所有對象變量都需要VBA中的「設置」。 – 2011-12-22 22:38:06

0

的幫助行/列這可以用公式來實現:

- 放置在細胞F1陣列進入(Ctrl+Shift+Enter )然後滾動到您擁有的許多單元格:

{=INDEX(A$1:A1,MAX(IF(ISBLANK(A$1:A1),0,ROW(A$1:A1))))} 

- 放置在單元格K1中,指的是F1中的第一個單元格,此處不需要數組。

=IF(ISBLANK(A1),IF(SUM(NOT(ISBLANK(INDEX($A1:A1,0)))+0)>0,"",F1),F1) 

- 放置任何你想要的結果。 MCONCATUDF function found in a free Add-in written in C++,CONCATENATE是Excel的內置公式。

=MConCat(K1:N1)=CONCATENATE(K1,L1,M1,N1)

我個人的偏好是雖然VBA方式。我敢肯定比我聰明的人可以想出一些更好的公式。

相關問題