VBA EXCEL删除行的方法
'Const sheetName As String = "电脑中心"
Private Sub CommandButton1_Click()
ActiveCell.EntireRow.Delete
End Sub
Private Sub CommandButton2_Click()
Dim minY
Dim maxY
minY = Selection.Row
maxY = Selection.Rows.Count + Selection.Row - 1
'从光标所在的行到另一个指定的行
'ActiveWorkbook.Worksheets("sheetName").Range("A" & CStr(minY), "A" & CStr(maxY)).EntireRow.Delete
ActiveSheet.Range("A" & CStr(minY), "A" & CStr(maxY)).EntireRow.Delete
End Sub
Private Sub CommandButton3_Click()
'TextBox1.Text = ActiveCell.Row
TextBox1.Text = Selection.Row
TextBox2.Text = Selection.Rows.Count + Selection.Row - 1
End Sub
Private Sub CommandButton4_Click()
Dim r
Dim x, y
r = TxtRow.Text
Dim i
'隔r行 删除一行
x = Selection.Row
y = Selection.Rows.Count + Selection.Row - 1
For i = x To y
i = i + r - 1
' ActiveWorkbook.Worksheets("sheetName").Range("A" & i, "A" & i).EntireRow.Delete
ActiveSheet.Range("A" & i, "A" & i).EntireRow.Delete
Next i
End Sub
精典之作:
''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''
''copy这里的代码
''1. 将宏的安全设置改为最低 Excel菜单-->工具-->宏-->安全
''2. 请将控制复制到所需的页面.
''3. 打开编辑器的设计模式
''4. 将各控制改名为代码相应的名字, 右击控件-->properties-->Name
''''---->a. 要删除的行数 控件名 TxtDel
''''---->b. 间隔行数 控件名 TxtRow
''''---->c. 起始行 控件名 TxtStart
''''---->d. 终止行 控件名 TxtEnd
''''---->e. 执行按钮 控件名 CBtnExecute
''5. 退出设计模式。OK了.
''''''''''''''''''''''''''''''
Private Sub CBtnExecute_Click()
Dim r
Dim x, y
Dim i
Dim d
r = TxtRow.Text
'隔r行 删除d行 输入 行号--行号
d = TxtDel.Text - 1
x = TxtStart.Text + CInt(r)
y = TxtEnd.Text
For i = x To y Step r
' ActiveWorkbook.Worksheets("sheetName").Range("A" & i, "A" & i).EntireRow.Delete
ActiveSheet.Range("A" & i, "A" & i + d).EntireRow.Delete
Next i
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''