说我在Excel中有一个这样的列:
15
14
13
10
9
6
1
我需要插入一个值,当跳过一个数字时,一个新的单元格被插入到它的位置,它不一定是一个数字,它可能只是“FALSE”。但它需要这样做而不添加整行,只需添加单元格和值。这可能吗?
答案 0 :(得分: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