EXCEL的一些问题,VBA类的宏

这段代码每次只能输一个词来删除,求修改下能一次输入多个词来删除的方法
Sub deletewords()
For i = 2 To 501
‘i表示起始行和终止行,比如第一个词条是第二行,最后一个词条是第501行
If InStr(Range("B" & CStr(i)).Text, "删除的词,怎么样能一次输入多个") > 0 Then
‘这里面这个B,是你的词所在的列,如果你的词不是B列,自己换个字母
Rows(CStr(i) & ":" & CStr(i)).Select
Selection.Delete shift:=xlUp
i = i - 1
End If
Next i
End Sub
谢谢,感激不尽。

两个你自己看吧

第一个

Sub deletewords()
Dim words()
x = InputBox("输入删除单词的个数(最小为1)", "删除单词个数", 1)
If Application.Evaluate("=isnumber(" & x & ")") = False Then
    MsgBox ("请不要输入文本,程序即将结束")
 肆谨段   Exit Sub
ElseIf x = 0 Then
    MsgBox ("输入的个数为0,程序即将结束")
    Exit Sub
End If
ReDim words(1 To x)
For i_words = 1 To x
    words(i_words) = InputBox("请输入需删除的单词", "删除的单词")
    If words(i_words) = "" Then
        MsgBox ("未输入单词,程序即将结束")
        Exit Sub
    End If
Next i_words
For i_1 = 1 To x
For i = 2 To 501
    If InStr(Range("B" & CStr(i)).Text, words(i_1)) > 0 Then
        Rows(CStr(i) & ":" & CStr(i)).Select
        Selection.Delete shift:=xlUp
        i = i - 1
    End If
Next i
Next i_1
End Sub

 

第二个

 Sub deletewords_2()
Dim words()
ReDim words(1 To Cells(65536, 15).End(xlUp).Row)
For i_words = 1 To Cells(65536, 15).End(xlUp).Row
    words(i_words) = Cells(i_words, 15)
Next i_words
For i_1 = 1 To Cells(65536, 15).End(xlUp).Row
For i = 2 To 501
    晌兄If InStr(Range("B" & CStr(i)).Text, words(i_1)) > 0 Then
        Rows(CStr(i) & ":" & CStr(i)).Select
        Selection.Delete shift:=xlUp
        i = i - 1
    End If
Next i
Next i_1
End Sub

 

 

第一个是需要你事先手动输入删除单词的个数及相应的单词

第二个是需要你事先将要删除的单词复制到O(从o1单元格开始)列然后运行程序即可,如果你想将需删除的单词复制到其它列则 Cells(65536, 15).End(xlUp).Row 中的15改为其它列的列号即可(注意一共有两处)A列——1 B列——2 C列——3 ... O列——15  以此类推

同时需要改的还是 Cells(i_words, 15) 这里边的15

 

上边的两个我只进行了简单测试,暂时裂誉没有发现问题,如有问题你在追问吧


方法有很多,比如elseif语句,如下
Sub deletewords()
Dim i As Long
For i = 2 To 501
If InStr(Range("B" & i).Value, "第一个删除词") > 0 Then
Rows(i & ":" & i).Delete shift:=xlUp
i = i - 1
ElseIf InStr(Range("B" & i).Value, "第二个删除词") > 0 Then
Rows(i & ":" & i).Delete shift:=xlUp
i = i - 1
ElseIf InStr(Range("亮启B" & i).Value, "第3个删除词") > 0 Then
Rows(i & ":" & i).Delete shift:=xlUp
i = i - 1
Else
End If
Next i
End Sub
另外脊激,还可以使用range的find方法。
最好的方法当然是使用正敬野如则表达式。那要加载正则才可以的。