不在搜索字符串中-仅在搜索字符串中有效

时间:2019-05-31 13:22:45

标签: excel vba

希望您能提供帮助。 我有A和B栏 在A中,我需要将所有包含“Forsikringspræmie”的文本替换为“”(空白)

此代码有效。子KSV_Remove_Forsikringspræmie()

但是在B列中,我需要相反的内容。 SubKSV_Remove_Not_Forsikringspræmie()代码可以正常工作。

Sub KSV_Remove_Forsikringspræmie()
    Dim KSV_SrchRng As Range, cel As Range
    Set KSV_SrchRng = Range("A1:A99")
    For Each cel In KSV_SrchRng
      If InStr(1, cel.Value, "Forsikringspræmie") > 0 Then
         cel.Value = ""
      End If
    Next cel
End Sub

Sub KSV_Remove_Not_Forsikringspræmie()
    Dim KSV_SrchRng As Range, cel As Range
    Set KSV_SrchRng = Range("B1:B99")
    For Each cel In KSV_SrchRng
      If InStr(1, cel.Value, Not "Forsikringspræmie") Then
         cel.Value = ""
      End If
    Next cel
End Sub

2 个答案:

答案 0 :(得分:2)

我认为您可能只是将Not放在错误的位置。试试这个:

Sub KSV_Remove_Not_Forsikringspræmie()
    Dim KSV_SrchRng As Range, cel As Range
    Set KSV_SrchRng = Range("B1:B99")
    For Each cel In KSV_SrchRng
      If Not (InStr(1, cel.Value, "Forsikringspræmie")) Then
         cel.Value = ""
      End If
    Next cel
End Sub

答案 1 :(得分:1)

为回应您对Greg帖子的评论(评论读为:,现在有一个小错误,它删除了每个单元格,因为包含“Forsikringspræmie”的单元格已被编号,例如“Forsikringspræmie1,Forsikringspræmie2”等。...和通配符剂量工作。)...如果要替换并保留单元格中的数字,请尝试使用Replace函数,例如:

With Columns(1)
    .Replace what:="Forsikringspræmie", replacement:="", searchorder:=xlByColumns, MatchCase:=False
End With