Excel宏循环用于将前一行中的列连接到上面一行中的列

时间:2010-11-24 15:00:55

标签: excel-vba vba excel

`我有一个由数据文件创建的excel文件,其中备注字段分成其他行。我需要获取前进的行列并将它们连接到第一行的列备注字段。我需要一个将通过该文件的宏将这些前进的行列连接到第一行列并删除前一行。

示例:(由于我是新用户,无法附加图片)

ColumnA B列C列D列

行ID日期ID编号说明

2        1/21/2010  2010000135        Music too loud in room. 
         Additionally, you have a pending Notice of Charge.
4        1/21/2010  2010000182        Blasting music in your room. 
         Finding notes 
         I explained discplinary process

我需要它看起来像:

2        1/21/2010  2010000135        Music too loud in room. Additionally, you have a pending Notice of Charge.
4        1/21/2010  2010000182        Blasting music in your room. Finding notes I explained discplinary process

备忘录描述行在不同的行数中分解,但我可以将备忘录行分解的行分开,因为行ID为空。如何在整个电子表格中将多行的B列连接到E(描述)列,并在将这些行连接到宏中的描述字段后删除这些行,直到它到达文件末尾?

2 个答案:

答案 0 :(得分:2)

假设第1列中的索引,第2列中的日期和第3列中的文本溢出到下一行中的第2列(意味着中断文本行中的第1列为空)...

Sub Beautify()
Dim R As Range, Idx As Long

    Set R = Selection
    Idx = 1

    Do While Idx < R.Rows.Count                           ' count dynamically changes as we delete rows
        If R(Idx + 1, 1) = "" Then                        ' found a break line looking 1 down
            R(Idx, 3) = R(Idx, 3) & " " & R(Idx + 1, 2)   ' append to current
            R(Idx + 1, 1).EntireRow.Delete                ' delete following but do not count up Idx
        Else
            Idx = Idx + 1                                 ' this one is clean, advance
        End If
    Loop
End Sub

选择完整列表并运行宏....

before Beautify()
=================
1   01.01.2010  Text 1
2   01.10.2010  Text 1
    Text 2  
3   01.10.2010  Text 1
    Text 2  
    Text 3  
4   01.01.2010  Text 1
5   01.10.2010  Text 1
    Text 2  

after Beautify()
=================
1   01.01.2010  Text 1
2   01.10.2010  Text 1 Text 2
3   01.10.2010  Text 1 Text 2 Text 3
4   01.01.2010  Text 1
5   01.10.2010  Text 1 Text 2

答案 1 :(得分:1)

行ID不能为空。行已被

拆分
1) <Alt>+<enter> at the end of each line or 

2) your columns are too narrow.  

For getting rid of the Alt+Enter

Select the range, press Ctrl+H, hold down the ALT key and
type in "0010" (no quotes) on your numeric keypad. Press
TAB and in the "Replace With" type a space. 
Press "Replace All".