ExcelVBA删除指定列含有指定字符的所在的行 |
---|
=====前面学习相关内容====
=====end====
1.用Find、Findnext,再删除,
2.用SpecialCells(xlCellTypeConstants, 16)快速定位
以上两种方法都可以不用理会“关键字符”在那一列的情况下执行,
【问题】
有人提出,程序运行时能否输入指定字符,输入指定列,再进行删除。可以的,(其实以上两种方法的适应广泛度还比较高),既然有人提出,就写一个吧 |
---|
【思路】
666,参考以前两篇吧 |
---|
【代码】
Sub yhd_ExcelVBA删除指定列含有指定字符的所在的行()
Dim titleRow As Integer
Dim rng As Range, InputRng As Range, DeleteRng As Range
Dim DeleteStr As String '字符串型数据
xTitleId = "请输入内容" '选择区域弹窗的名字
On Error Resume Next
titleRow = Application.InputBox("标题行数 :", xTitleId, Type:=1)
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("指定列 :", xTitleId, InputRng.Address, Type:=8)
DeleteStr = Application.InputBox("包含指定字符", xTitleId, Type:=2) '删除的行的关键字
On Error GoTo 0
If Val(titleRow) = 0 Or DeleteStr = "" Then MsgBox "输入数据不正确,将退出": Exit Sub
If InputRng.Columns.Count > 1 Then
MsgBox "指定列只能是一列哦,将退出"
Exit Sub
Else
colNum = InputRng.Column
End If
LastRow = Cells.Find("*", , , , 1, 2).Row
For i = titleRow + 1 To LastRow
If InStr(Cells(i, colNum), DeleteStr) Then
If DeleteRng Is Nothing Then
Set DeleteRng = Rows(i)
Else
Set DeleteRng = Application.Union(DeleteRng, Rows(i))
End If
End If
Next
If DeleteRng Is Nothing Then DeleteRng.EntireRow.Delete
End Sub