回答編集履歴
4
ロジック解説の追加
answer
CHANGED
@@ -82,3 +82,11 @@
|
|
82
82
|
実行結果
|
83
83
|

|
84
84
|
|
85
|
+
**ロジック**
|
86
|
+
最終行の1行上から順に前に移動して読み込む(最終行は空白行挿入する必要はない)
|
87
|
+
|
88
|
+
上へ移動するときに、
|
89
|
+
空白行をカウントしていく
|
90
|
+
2以上の数値がでたら、そこからカウントした空白行を引いた行数分挿入する、その後空白行カウントを0に初期化する
|
91
|
+
それ以外は、空白行カウントを0に初期化する
|
92
|
+
|
3
コード修正、画像変更
answer
CHANGED
@@ -59,13 +59,13 @@
|
|
59
59
|
|
60
60
|
Application.ScreenUpdating = False '処理終了まで画面表示はそのまま
|
61
61
|
|
62
|
-
|
62
|
+
'最終行の1行上から上へ読み込む
|
63
|
-
For i = Cells(Rows.Count, intCol).End(xlUp).Row - 1 To intStart Step -1
|
63
|
+
For i = Cells(Rows.Count, intCol).End(xlUp).Row - 1 To intStart Step -1
|
64
64
|
Select Case Cells(i, intCol).Value
|
65
65
|
Case ""
|
66
66
|
cntBlank = cntBlank + 1 '空白行カウント
|
67
67
|
Case Is >= 2
|
68
|
-
AddCnt = Cells(i, intCol).Value - cntBlank '追加する行数計算
|
68
|
+
AddCnt = Cells(i, intCol).Value - cntBlank - 1 '追加する行数計算
|
69
69
|
If AddCnt > 0 Then
|
70
70
|
Range(Rows(i + 1), Rows(i + AddCnt)).Select
|
71
71
|
Selection.Insert '選択された行数分追加
|
@@ -79,5 +79,6 @@
|
|
79
79
|
Application.ScreenUpdating = True
|
80
80
|
End Sub
|
81
81
|
```
|
82
|
+
実行結果
|
82
|
-
|
83
|
+

|
83
84
|
|
2
サンプルコードの追加
answer
CHANGED
@@ -26,4 +26,58 @@
|
|
26
26
|
Next i
|
27
27
|
```
|
28
28
|
|
29
|
-
`Cells(Rows.Count, intCol).End(xlUp).Row` でデータのある最終行が取得できます。
|
29
|
+
`Cells(Rows.Count, intCol).End(xlUp).Row` でデータのある最終行が取得できます。
|
30
|
+
|
31
|
+
追記
|
32
|
+
---
|
33
|
+
細かい仕様は不明ですが、やりたいことは下記のようなことかな。
|
34
|
+
|
35
|
+
N列にある数値分の空白行のその行の下に挿入する。
|
36
|
+
ただし、空白行がすでにある場合は、それを含めて指定の行数になるように挿入する。
|
37
|
+
|
38
|
+
```
|
39
|
+
Sub InsertRow1()
|
40
|
+
|
41
|
+
Dim i As Long
|
42
|
+
Dim intStart As Long
|
43
|
+
Dim intCol As Long
|
44
|
+
Dim cntBlank As Long
|
45
|
+
Dim AddCnt As Long
|
46
|
+
Dim msg_1 As String
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
intStart = 2 '開始する行数
|
51
|
+
intCol = 14 '数字を読み込む列
|
52
|
+
i = intStart '追加する行数の先頭位置
|
53
|
+
Dim j As Integer '追加する行数の中に既に空白行があったらその行数分
|
54
|
+
|
55
|
+
|
56
|
+
msg_1 = "N列に指定されている員数-1行を追加しますか"
|
57
|
+
|
58
|
+
If MsgBox(msg_1, vbYesNo) = vbNo Then Exit Sub '行を追加してよいかのポップアップを出す
|
59
|
+
|
60
|
+
Application.ScreenUpdating = False '処理終了まで画面表示はそのまま
|
61
|
+
|
62
|
+
|
63
|
+
For i = Cells(Rows.Count, intCol).End(xlUp).Row - 1 To intStart Step -1 '最終行の1行上から読み込む
|
64
|
+
Select Case Cells(i, intCol).Value
|
65
|
+
Case ""
|
66
|
+
cntBlank = cntBlank + 1 '空白行カウント
|
67
|
+
Case Is >= 2
|
68
|
+
AddCnt = Cells(i, intCol).Value - cntBlank '追加する行数計算
|
69
|
+
If AddCnt > 0 Then
|
70
|
+
Range(Rows(i + 1), Rows(i + AddCnt)).Select
|
71
|
+
Selection.Insert '選択された行数分追加
|
72
|
+
End If
|
73
|
+
cntBlank = 0
|
74
|
+
Case Else
|
75
|
+
cntBlank = 0
|
76
|
+
End Select
|
77
|
+
Next i
|
78
|
+
|
79
|
+
Application.ScreenUpdating = True
|
80
|
+
End Sub
|
81
|
+
```
|
82
|
+
実行結果
|
83
|
+
|
1
書式の改善
answer
CHANGED
@@ -26,4 +26,4 @@
|
|
26
26
|
Next i
|
27
27
|
```
|
28
28
|
|
29
|
-
Cells(Rows.Count, intCol).End(xlUp).Row でデータのある最終行が取得できます。
|
29
|
+
`Cells(Rows.Count, intCol).End(xlUp).Row` でデータのある最終行が取得できます。
|