根据条件在列中连接单元格

时间:2014-11-11 17:57:49

标签: excel vba

我希望在列中连接单元格。模型以模型编号开头,没有任何.该模型的选项以.开头我希望marco一直连接到下一个模型编号的开头(下一个单元格不是以.开头,然后重复过程,我在下面提供了一个例子。任何帮助将不胜感激!

当前状态

abc

.bcr

.kjl

.plk

ghy

.ytr

.ihgew

.rth

.u

.lpn

trh

.pjkjh

.dsgyudd

.hg

.gfd

未来状态

abc.bcr.kjl.plk

ghy.ytr.ihgew.rth.u.lp

trh.pjkjh.dsgyudd.hg.gfd

1 个答案:

答案 0 :(得分:0)

试试这个:

Option Explicit
Sub ConditionalConcatenate()
'col A contain the texts and col B in corresponding row hold concatenated result

Dim strDone As String
Dim rngg As Range, lastcolA As Range, rngused As Range
Dim counted As Integer, i As Integer, j As Integer

Range("B:B").ClearContents
strDone = ""
Set rngg = ActiveSheet.Range("A:A")
Set lastcolA = rngg.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious)
Set rngused = Range("A1:A" & lastcolA.Row)
counted = rngused.Rows.Count

For i = 1 To counted
    If Left(rngused(i), 1) <> "." Then
    strDone = strDone & rngused(i)
        For j = 1 To counted
            If Left(rngused(i).Offset(j, 0), 1) = "." Then
            strDone = strDone & rngused(i).Offset(j, 0)
            ElseIf Left(rngused(i).Offset(j, 0), 1) <> "." Then
            Range("B" & i).Value = strDone
            strDone = ""
            Exit For
            End If
        Next
    End If
Next

Set rngg = Nothing
Set lastcolA = Nothing
Set rngused = Nothing
End Sub
相关问题