如果标题不包含,则删除列

时间:2017-09-27 17:35:35

标签: excel vba excel-vba

如果使用一个单词,以下代码可以工作,但是,我希望它能找到两个单词。因此,如果在第3行中找到的列标题不包含“Homer”或“Marge”,则删除整个列。

Sub DontDelete()

Dim currentColumn As Integer

'Delete if the cell doesn't contain "Homer" OR "Marge"
If InStr(3, _
   ActiveSheet.UsedRange.Cells(3, currentColumn).Value, _
       "Homer", vbBinaryCompare) = 0 Then

ActiveSheet.Columns(currentColumn).Delete

End If

End Sub

如何将OR "Marge"合并到此声明中?

1 个答案:

答案 0 :(得分:2)

您可以使用And

If InStr(3, ActiveSheet.UsedRange.Cells(3, currentColumn).Value, _
       "Homer", vbBinaryCompare) = 0 And InStr(3, ActiveSheet.UsedRange.Cells(3, currentColumn).Value, _
       "Marge", vbBinaryCompare) = 0 Then
ActiveSheet.Columns(currentColumn).Delete
End If