简体   繁体   English

Excel宏 - 删除以某些字符开头的单词

[英]Excel Macro- remove a word that starts with certain characters

In a VB macro for Excel, how do I remove all occurances of a word that starts with a certain string? 在用于Excel的VB宏中,如何删除以某个字符串开头的单词的所有出现?

Eg: 例如:

The string reads: xxxx $AUD543.43 yyyy 该字符串显示: xxxx $AUD543.43 yyyy

I want to search for anything in a string that starts with $AUD and remove the whole word before the next space 我想在以$AUD开头的字符串中搜索任何内容,并在下一个空格之前删除整个单词

So the example above should result in: 所以上面的例子应该导致:

xxxx yyyy

use Regex. 使用正则表达式。 Add a reference to Microsoft VBScript Regular Expressions in VBA >> Tools >> Options. 在VBA >>工具>>选项中添加对Microsoft VBScript正则表达式的引用。

Dim txt As String
txt = "$Audthisfew is$Aud $Auda test $Aud"

Set regEx = New RegExp

With regEx
    .Global = True
    .Pattern = "((^, )\$Aud)"
    Debug.Print .Replace(txt, "")
End With

It would be remiss of me not to remind you of the expectation that you show "what you (have) tried". 如果不提醒你期望你表现出“你曾经尝试过的东西”,那将是我的疏忽。 (Which I do to avoid me being yelled at for answering the question.) My duty thus having been done, I now move on to it. (我这样做是为了避免让我因为回答这个问题而大喊大叫。)我的责任已经完成,我现在继续谈谈。

You actually don't necessarily need VBA code to do this; 实际上,您不一定需要VBA代码才能执行此操作; you could do it with the Find() function albeit more clumsily and I wouldn't recommend it for a really large sheet. 你可以用Find()函数做到这一点,虽然更笨拙,我不推荐它用于一个非常大的工作表。 Still, VBA code you have specified, and that you shall have. 仍然是你指定的VBA代码,你应该拥有。 Change the range to match the one that you'll be searching. 更改范围以匹配您要搜索的范围。 ALSO, you should note that you have only one space between the x's and y's in your example but that varies from your request that it be the word starting with $AUD and ending BEFORE the next space. 另外,你应该注意到你的例子中x和y之间只有一个空格,但这与你的请求不同,即以$ AUD开头并在下一个空格之前结束。 If you want only one space, please adjust the formulas accordingly. 如果您只想要一个空格,请相应调整公式。

Sub ReplaceText()

Dim rng As Excel.Range
Dim s_Contents As String
Dim l_FindAUD As Long, l_FindSpace As Long

For Each rng In ActiveSheet.UsedRange

    s_Contents = rng.Value

    'Does the $AUD expression exist in this cell?
    l_FindAUD = InStr(1, s_Contents, "$AUD", vbTextCompare)

    If l_FindAUD > 0 Then

        'If so, is it followed by a space?
        l_FindSpace = InStr(l_FindAUD, s_Contents, " ")

        If l_FindSpace > 0 Then

            'If so, take all of the content up to but not including the $
            'and all of the contents from the space onwards, merge them
            'together and write to the cell.
            s_Contents = Left$(s_Contents, l_FindAUD - 1) & Mid$(s_Contents, l_FindSpace)
            rng.Value = s_Contents
        End If

    End If

Next


End Sub

Although not exactly what you ask for, but you could also use an Excel formula to achieve this. 虽然不完全符合您的要求,但您也可以使用Excel公式来实现这一目标。 Assuming your text is in A1, the formula would be: 假设您的文本在A1中,则公式为:

=TRIM(LEFT(A1,FIND("$AUD",A1)-1))&RIGHT(A1,LEN(A1)-FIND(" ",A1,FIND("$AUD",A1))+1)

My example is used for delete the last few words of a cell 我的例子用于删除单元格的最后几个单词

For example: 例如:

in cell A1: ABCDE[Acct:12345] 在单元格A1: ABCDE[Acct:12345]

in cell A2: FGHIJ[Acct:67890] 在单元格A2中: FGHIJ[Acct:67890]

in cell A3: KLMNO 在小区A3: KLMNO

Wanna delete all the words begin with "[Acct:", note not every cell includes "[Acct:" 想删除所有以“[Acct:”开头的单词,注意不是每个单元格都包含“[Acct:”

Now in column B, insert the below function, this will return with the words on the left on "[Acct:" =LEFT(A1,FIND("[Acct:",A1)-1) 现在在B列中,插入以下函数,这将返回左侧的单词“[Acct:”= LEFT(A1,FIND(“[Acct:”,A1)-1)

Result: AB ABCDE[Acct:12345] ABCDE FGHIJ[Acct:67890] FGHIJ KLMNO #VALUE! 结果:AB ABCDE [帐号:12345] ABCDE FGHIJ [帐号:67890] FGHIJ KLMNO #VALUE! Now in column C, insert below function, this will check whether column B is #VALUE! 现在在C列中,插入下面的函数,这将检查B列是否为#VALUE! and then return with what we want =IF(ISERROR(C1)=TRUE,A1,B1)) 然后以我们想要的东西返回= IF(ISERROR(C1)= TRUE,A1,B1))

Result: 结果:

     A                          B                C

   ABCDE[Acct:12345]          ABCDE            ABCDE

   FGHIJ[Acct:67890]          FGHIJ            FGHIJ

   KLMNO                      #VALUE!          KLMNO

Then you can copy the three columns and paste with value, and delete column A and B , column C will be the final result you want. 然后你可以复制三列并粘贴值,并删除列A和B,列C将是你想要的最终结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM