过滤掉Excel Vba中的特定单词

时间:2014-04-04 13:43:02

标签: string excel vba filter

我想过滤掉特定的单词和字符串。 例如, “这是一个示例文件。” 在excel vba =“file”中过滤掉的单词。 请帮忙,急需此代码。 谢谢。

1 个答案:

答案 0 :(得分:0)

Instr("This is a sample file.", "file")将为您提供第一次出现" file"的位置在较长的字符串中,如果不存在则为0。

您可以使用LeftMidRight删除字符串的元素。 Len为您提供字符串的长度。

把所有这些放在一起:

Sub Test()
    Dim s1 As String
    Dim s2 As String
    Dim pos As Integer

    s1 = "This is a sample file."
    s2 = "file"
    pos = InStr(s1, s2)
    If pos > 0 Then
        Debug.Print Left(s1, pos - 1) & Mid(s1, pos + Len(s2))
    End If
End Sub