次のような Excel の列があるとします。
15
14
13
10
9
6
1
そして、数値がスキップされたときに新しいセルがその場所に挿入される値を挿入する必要があります。数値である必要はなく、単に「FALSE」になる可能性があります。ただし、行全体を追加せずに、そのセルと値だけを追加する必要があります。これは可能ですか?
次のような Excel の列があるとします。
15
14
13
10
9
6
1
そして、数値がスキップされたときに新しいセルがその場所に挿入される値を挿入する必要があります。数値である必要はなく、単に「FALSE」になる可能性があります。ただし、行全体を追加せずに、そのセルと値だけを追加する必要があります。これは可能ですか?
私は今遊んでいますが、ループして以下を使用すると動作します:
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