简体   繁体   English

如何使用VBA从Excel工作表中删除表情符号?

[英]How do I remove emojis from an excel sheet using VBA?

I have a spreadsheet that contains emojis, eg, 😃, and I am looking for a solution to use Excel VBA to replace the emojis with null. 我有一个包含表情符号(例如😃)的电子表格,并且我正在寻找一种使用Excel VBA将表情符号替换为null的解决方案。

Emojis can be removed using the Excel replace action, so I recorded a macro to automate the replace. 可以使用Excel替换操作删除表情符号,因此我记录了一个宏来自动执行替换。 I opened the recorded macro and it was displayed as follows: 我打开了录制的宏,它显示如下:

Sub Remove_Emojis()

    Cells.Replace What:="??", Replacement:="", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
End Sub

The problem is that VBA doesn't recognize emojis (😃) and replaces them with "??", ie, Unicode characters above a certain value are not recognized by VBA. 问题是VBA无法识别表情符号(😃),而是将其替换为“ ??”,即,VBA无法识别高于某个值的Unicode字符。

I tried replacing "??" 我尝试替换“ ??” with ChrW() : ChrW()

Sub Remove_Emojis()

    Cells.Replace What:=ChrW(128515), Replacement:="", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
End Sub

but this results in an error: 但这会导致错误:

Invalid procedure call of argument 参数的无效过程调用

because the ChrW() function does not allow a value above 65535. Note: The ChrW() function works if the value of the argument is within the range -32,767 to 65,535. 因为ChrW()函数不允许大于65535的值。注意:如果参数的值在-32,767到65,535的范围内,则ChrW()函数将起作用。

I expect that there should be support for doing this in VBA given that it can be done in Excel. 我希望应该在VBA中支持这样做,因为它可以在Excel中完成。

I did as small experiment, putting your smiley into excel and let the following code run: 我做了一个小实验,将您的笑脸放入excel,并运行以下代码:

Dim s
s = ActiveCell
Dim i As Long
For i = 1 To Len(s)
    Dim c
    c = Mid(s, i, 1)
    Debug.Print i, c, AscW(c)
Next i

My result was 我的结果是

 1            ?             -10179 
 2            ?             -8701 

So obviously, the single character is split into 2 inside VBA. 因此很明显,单个字符在VBA中被分成2个。 AscW and it's pendant ChrW deal with 16bit, and the emoji is a 32bit char, so in VBA this emoji character is handled as if there are 2 characters in the string AscW及其下垂的ChrW处理16位,表情符号为32位字符,因此在VBA中,此表情符号字符的处理方式就像字符串中有2个字符一样

I added the following code and voilà, the smiley char was gone: 我添加了以下代码,然后笑脸字符消失了:

Dim x
x = ChrW(-10179) & ChrW(-8701)

s = Replace(s, x, "(smiley)")
ActiveCell.Offset(0, 1) = s

Probably you have to experiment with the different emojis you are facing and build a list in your replacing routine. 可能您必须尝试面对的不同表情符号,并在替换例程中建立一个列表。

Thank you to FunThomas for pointing out that emojis are represented as 2 characters in VBA. 感谢FunThomas指出表情符号在VBA中以2个字符表示。 The revised VBA code that works based on this: 修改后的VBA代码可基于此工作:

Sub Remove_Emojis()

    Cells.Replace What:=ChrW(-10197) & ChrW(-8701), Replacement:="", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
End Sub

In my actual solution, I put this into a loop to remove all of the different emojis. 在我的实际解决方案中,我将其放入循环中以删除所有不同的表情符号。

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

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