vba 显示多行多列使用什么控件

如题所述

第1个回答  2015-02-10
如果要在Excel中插入较多的行或列,例如要插入上百行,用普通的操作方法有些繁琐,我们可以用VBA代码来轻松实现这一目的。在VBA编辑器的代码窗口中输入下列代码,运行前先在表格中选择要插入行或列的位置,然后运行代码,在提示窗口中输入要插入的行数或列数即可。
插入多行:
Sub InsertMultipleRows()
' 在当前选择的单元格或行之上插入多行
Dim NewRows As Long
Dim CurrentRow As Long
' 检查当前选择的是否为行或单元格
If UCase(TypeName(Selection)) <> "RANGE" Then
MsgBox "请选择行或单元格,以便在其上方插入行。", _
vbInformation
Exit Sub
End If
' 让用户输入要插入的行数
NewRows = Application.InputBox("请输入要插入的行数", _
"插入多行", 1, , , , , 1)
' 如果行数为0或取消
If NewRows <= 0 Then Exit Sub
' 插入相应数量的行
Rows(Selection.Cells(1).Row & ":" & _
Selection.Cells(1).Row + NewRows - 1).Insert Shift:=xlShiftDown
End Sub
插入多列:
Sub InsertMultipleColumns()
' 在当前选择的单元格或列的左方插入多列
Dim NewColumns As Long
Dim CurrentRow As Long
' 检查当前选择的是否为列或单元格
If UCase(TypeName(Selection)) <> "RANGE" Then
MsgBox "请选择行或单元格,以便在其上方插入行。", _
vbInformation
Exit Sub
End If
' 让用户输入要插入的列数
NewColumns = Application.InputBox("请输入要插入的列数", _
"插入多列", 1, , , , , 1)
' 如果列数为0或取消
If NewColumns <= 0 Then Exit Sub
' 插入相应数量的列
For i = 1 To NewColumns
Columns(Selection.Cells(1).Column).Insert Shift:=xlShiftToRight
Next
End Sub本回答被提问者和网友采纳
相似回答