0

假设我在 Excel 中有这样的列:

15
14
13
10
9
6
1

我需要插入一个值,当一个数字被跳过时,一个新的单元格被插入到它的位置,它不必是一个数字,它可能只是“FALSE”。但它需要在不添加整行的情况下执行此操作,只需添加该单元格和值。这可能吗?

4

1 回答 1

1

我现在正在玩,但是如果您循环并使用下面的内容,它将起作用:

Cells(1,1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

使用 VBA 使用以下代码。右键单击工作表选项卡并单击查看代码,将其粘贴并运行它:

Sub add_rows(ByVal myCol As String)
    With ActiveSheet
        Dim l As Long, i As Long, myCol As String
        myCol = "E" '<~~ alter this for your column
        l = .Columns(myCol).Find("*", SearchDirection:=xlPrevious).Row '<~~ get last row in column
        i = 1

        Do While i <= l

            If i > 1 Then '<~~ not first row?
                Dim c1, c2 'get cell values
                c1 = .Cells(i - 1, myCol).Value
                c2 = .Cells(i, myCol).Value

                If c1 - 1 > c2 Then
                        'shift cells down by the numbers missing
                        .Cells(i, myCol).Resize(c1 - c2 - 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                        'set to false
                        .Cells(i, myCol).Resize(c1 - c2 - 1) = False

                        'increase variable by number of inserted rows
                        l = l + c1 - c2 - 1 
                        i = i + c1 - c2 - 1
                End If
            End If

            i = i + 1
        Loop
    End With
End Sub
于 2013-05-04T00:35:45.940 回答