首页 文章

VBA复制单元格的内容减去另一个单元格的内容

提问于
浏览
-1

我试过谷歌搜索答案,但不幸的是,在这种情况下我无法找到满足我特定需求的任何东西 .

我有两个单元格,后者由一个单词组成,也可以在第一个单元格中找到 . 我想将所有内容从单元格1复制到单元格3减去单元格2的内容 .

例如:

Cell 1

这是样本文本

Cell 2

样品

Cell 3

这是文本

有没有办法通过VB宏或甚至简单的公式来做到这一点?

在此先感谢,任何帮助表示赞赏 .

3 回答

  • 0

    阐述了SJR对 SUBSTITUTE 的建议:

    使用以下公式: =TRIM(SUBSTITUTE(A1,B1,"")) . 把它放在C1中并复制列 . 这将从第1列中的字符串中删除第2列中的单词. TRIM 函数删除了删除单词的额外空格 . 如果一个单词可以作为另一个单词的子集出现(例如,单词是'test'但你的字符串是'Test, testing, 1, 2, 3'),你可以在过滤单词的末尾添加一个空格,以便'testing'不匹配 .

    如果单词出现多次,除非您指定要删除的匹配项,否则将删除所有匹配项 . 将 (A1,B1,"") 更改为 (A1,B1,"",1) 将仅删除第一个匹配的匹配项 . 所以'Test1 test2 test3'将改为'1 test2 test3'

  • 0

    试试这个:

    Sub removeYourString()
       Dim withParts As String
       Dim removeParts As String
       Dim withoutParts As String
    
       withParts = Range("A1").Value
       removeParts = Range("A2").Value & " "
       withoutParts = Replace(withParts, removeParts, "")
       Range("A3").Value = withoutParts
    End Sub
    

    删除所有列

    Sub removeYourString()
       Dim withParts As String
       Dim removeParts As String
       Dim withoutParts As String
       Dim i As Integer
    
       For i = 1 To 100
            withParts = Range("A" & i).Value
            removeParts = Range("B" & i).Value & " "
            withoutParts = Replace(withParts, removeParts, "")
            Range("C" & i).Value = withoutParts
       Next i
    End Sub
    

    或者使用公式并放入c1到c100

    =SUBSTITUTE(A1,B1 & " ","")
    
  • 0

    您可以在cell3中解决此公式:

    =CONCATENATE(LEFT([Cell1],FIND([Cell2],[Cell1],FIND([Cell2],[Cell1])+1)-1),RIGHT([Cell1],FIND([Cell2],[Cell1])+LEN([Cell2])+1))
    

相关问题