在Excel中重新排列表格

时间:2016-09-22 19:07:15

标签: excel

在Excel中,我需要转换像这样的条目:


+------------+-------------------+--------+
|    Date    |      Details      | Amount |
+------------+-------------------+--------+
| 15/02/2016 | Payment type      |  37.42 |
+------------+-------------------+--------+
|            | Payment details 1 |        |
+------------+-------------------+--------+
|            | Payment details 2 |        |
+------------+-------------------+--------+
|            | Payment details 3 |        |
+------------+-------------------+--------+
对此:

+------------+--------------+--------+-------------------+-------------------+-------------------+
|    Date    |   Details    | Amount |                   |                   |                   |
+------------+--------------+--------+-------------------+-------------------+-------------------+
| 15/02/2016 | Payment type |  37.42 | Payment details 1 | Payment details 2 | Payment details 3 |
+------------+--------------+--------+-------------------+-------------------+-------------------+

长话短,左侧孤儿行应添加到父行的右侧,每行作为新列。

我怎样才能轻松完成?

2 个答案:

答案 0 :(得分:0)

试试这段代码:

Option Explicit
Sub move_details()
Dim rw, last_rw, offset, cl As Integer

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
rw = 1
cl = 4
While Cells(rw, 2) <> ""
    If Trim(Cells(rw, 1)) = "" Then
        Cells(rw - 1, cl) = Cells(rw, 2)
        Rows(rw).Delete
        cl = cl + 1
    Else
        rw = rw + 1
        cl = 4
    End If
Wend

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

答案 1 :(得分:0)

管理自己编写,我只需要测试一些,以确保一切正常:

Option Explicit
Sub move_details()
Dim i As Long
Dim notEmpty As Long
Dim wantedRow As Long
Dim offset As Long
Dim a As Variant
offset = 6
notEmpty = 2
wantedRow = 3
For i = 1 To Rows.Count
If IsEmpty(Cells(i, 2).Value) Then
    a = Cells(i, wantedRow).Value
    Cells(notEmpty, offset).Value = a
    offset = offset + 1
    Rows(i).EntireRow.Delete
    i = i - 1
Else
    offset = 6
    notEmpty = i
End If
Next i
End Sub