如果使用一个单词,以下代码可以工作,但是,我希望它能找到两个单词。因此,如果在第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"
合并到此声明中?
答案 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