在Excel中将多行合并为一行

时间:2012-10-15 15:00:05

标签: excel vba excel-vba concatenation cisco

我想使用Excel将多行合并为一行。我已经使用CONCATENATE函数完成了这个,但这次我需要自动化该过程,因为我在文件上有几个条目。 我有一个从思科CME中提取的Ephone IP电话信息,其中每个电话号码信息如下:

ephone-1[0] Mac: TCP socket:[57] activeLine:0 whisperLine:0 REGISTERED in SCCP ver 20/17 max_streams=1
mediaActive:0 whisper_mediaActive:0 startMedia:0 offhook:0 ringing:0 reset:0 reset_sent:0 paging 0 debug:0 caps:8 
IP:---------- * 35419 6941  keepalive 54113 max_line 4 available_line 4
button 1: cw:1 ccw:(0 0) 
dn 1  number ------- CH1   IDLE         CH2   IDLE         shared 
Preferred Codec: g711ulaw 
Lpcor Type: none Username: ---- Password: ------ 


ephone-2[1] Mac:-------- TCP socket:[77] activeLine:0 whisperLine:0 REGISTERED in SCCP ver 20/17 max_streams=1
mediaActive:0 whisper_mediaActive:0 startMedia:0 offhook:0 ringing:0 reset:0 reset_sent:0 paging 0 debug:0 caps:8 
IP:------- * 35189 6941  keepalive 117528 max_line 4 available_line 4
button 1: cw:1 ccw:(0 0) 
 dn 2  number ------ CH1   IDLE         CH2   IDLE         shared 
Preferred Codec: g711ulaw 
Lpcor Type: none 

每个ephone在文件上由一个或两个空行分隔。有大约350个条目,我想自动化该过程。这个过程应该是把ephone的每一行都合并成一行,这样最后我就会有350行包含350个电话的信息。

有人知道如何在Excel上制作这个吗?我非常感谢你的帮助。

祝你好运

1 个答案:

答案 0 :(得分:2)

此代码应该执行此操作,合并并删除多余的行

Sub ConsolidateRows_NoMatch()
'takes rows and consolidate one or many cells, based on one or many cells matching with above or below rows.

Dim lastRow As Long, i As Long, j As Long
Dim colMatch As Variant, colConcat As Variant

'turn off updates to speed up code execution
With application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

lastRow = range("A" & Rows.Count).End(xlUp).Row 'get last row

For i = lastRow To 1 Step -1 'loop from last Row to one

    If Len(Cells(i, 1)) > 0 Then
        If Left(Cells(i, 1), 6) <> "ephone" Then
            Cells(i - 1, 1) = Cells(i - 1, 1) & Cells(i, 1)
        Else
            GoTo nxti:
        End If
    End If

    Rows(i).Delete
nxti:
Next

With application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
相关问题