Excel 宏循环将后续行中的列连接到上方行中的列中

标签 excel vba

`我有一个由数据文件创建的 Excel 文件,其中备注字段分解为其他行。我需要获取后续行列并将它们连接到第一行的列备注字段。我需要一个宏来遍历文件,将这些后续行列连接到第一行列并删除后续行。

示例:(无法附加图像,因为我是新用户)

A 列 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 列(描述),并在将这些行连接到宏中的描述字段后将其删除,直到到达文件末尾?

最佳答案

假设第一列中有一个索引,第二列中有一个日期,第三列中有一个文本,该文本溢出到下一行的第二列(意味着中断文本行中的第一列为空)...

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

关于Excel 宏循环将后续行中的列连接到上方行中的列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4268170/

相关文章:

excel - VBA 如何打开另一个工作簿?

php - 如何从 mySQL 数据库创建格式化的 Excel 列表?

asp.net - 在 asp.net 中编辑 Excel 文件

python - 尝试使用 Python 获取 Excel 差异时如何修复 Key 错误

vba - 在 Excel 中运行 SAS 存储过程两次运行后失败

vba - 关闭时 Access 崩溃

VBA 验证。添加方法失败

c# - 以编程方式检索Excel库的版本

excel - 检查宏是否使用 VBA 运行

excel - VBA/如何过滤精确字符串上的数组?