excel 在所选位置增加整行整列vba代码

例如所选b5,运行宏后,在b5位置增加整行整列
如果是增加n列呢比如是6列

Sub InsertColumnsAndRows()
Dim i As Long
Dim j As Long
Dim k As Long

i = Selection.Range("A1").Row
j = Selection.Range("A1").Column
For k = 1 To 6 '如果插入6行6列
Cells(i, j).EntireRow.Insert
Cells(i, j).EntireColumn.Insert
Next k
End Sub追问

如果是n行和,弹出inputbox,怎么改写呢

追答

Sub InsertColumnsAndRows()
Dim i As Long
Dim j As Long
Dim k As Long
Dim r As Long, c As Long

r = Abs(Int(Application.InputBox("输入增加的行数:", "增加行列", , , , , , 1)))
c = Abs(Int(Application.InputBox("输入增加的列数:", "增加行列", , , , , , 1)))

i = Selection.Range("A1").Row
j = Selection.Range("A1").Column

If r > 0 Then
For k = 1 To r
Cells(i, j).EntireRow.Insert
'Cells(i, j).EntireColumn.Insert
Next k
End If
If c > 0 Then
For k = 1 To c
'Cells(i, j).EntireRow.Insert
Cells(i, j).EntireColumn.Insert
Next k
End If
End Sub

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-02-08
Selection.EntireRow.Insert
Selection.EntireColumn.Insert
第2个回答  2012-02-08
ActiveCell.Select
Selection.EntireRow.Insert
Selection.EntireColumn.Insert
相似回答