简体   繁体   English

Excel文件包含无法删除的无效隐藏字符

[英]Excel file contains invalid hidden characters that can't be removed

I have a peculiar problem with hidden characters in an Excel spreadsheet which uses VBA to create a text file. 我在使用VBA创建文本文件的Excel电子表格中存在隐藏字符的特殊问题。 I've attached a link to a test version of the file, and I'll explain as best I can the issue. 我已将链接附加到该文件的测试版本,并且将尽我所能解释该问题。

The file creates a plain txt file that can be used to feed data into a System we use. 该文件将创建一个纯文本文件,该文件可用于向我们使用的系统中馈送数据。 It works well normally, however we've been supplied approximately 15,000 rows of data, and at random points throughout there are hidden characters. 它正常运行良好,但是已经提供了大约15,000行数据,并且在随机点处始终存在隐藏字符。

In the test file, there's 1 row and it's cell B11 that has hidden characters at the beginning and end of the value. 在测试文件中,有1行,是单元格B11,该单元格的值的开头和结尾处都有隐藏的字符。 If you put your cursor at the end of it, and press the backspace key, it will look as if nothing has happened, but actually you've just deleted one of the characters. 如果将光标放在光标的末尾,然后按Backspace键,看起来好像什么都没发生,但实际上您只是删除了其中一个字符。

As far as Excel is concerned, those hidden characters are question marks, but they're not, as text stream would parse those, but it doesn't, and instead throws up an invalid procedure call error. 就Excel而言,那些隐藏的字符是问号,但不是,因为文本流会解析它们,但不是,而是抛出无效的过程调用错误。

I've tried using Excel's CLEAN formula, I've tried the VBA equivalent, tried using 'Replace', but nothing seems to recognise those characters. 我尝试使用Excel的CLEAN公式,尝试使用VBA等效项,尝试使用“替换”,但是似乎没有任何字符可以识别这些字符。 Excel is convinced they're just question marks, even an ASCII character call gives me the same answer (63), but replace doesn't replace them as question marks, it just omits them! Excel坚信它们只是问号,即使是ASCII字符调用也能给我相同的答案(63),但是replace不会将它们替换为问号,而是会忽略它们!

Any help on this, even if it's just a formula I could apply would be appreciated. 即使这只是我可以应用的公式,对此也可以提供任何帮助。 In the interests of data protection the data in the file is fake by the way, it's nobody's real NI number. 为了保护数据,顺便说一下,文件中的数据是伪造的,它不是真实的NI编号。

The excel file with vba code is here 带有VBA代码的Excel文件在这里

This VBA macro could be run on its own or in conjunction with the ClearFormatting macro. 此VBA宏可以单独运行,也可以与ClearFormatting宏一起运行。 It did strip out the rogue unichars from the sample. 它确实从样本中去除了流氓单子。

Sub strip_Rogue_Unichars()
    Dim uc As Long
    With Cells(11, 1).CurrentRegion
        For uc = 8000 To 8390
            .Replace what:=ChrW(uc), replacement:=vbNullString, lookat:=xlPart
            DoEvents
        Next uc
    End With
End Sub

There's probably a better way to do this and being able to restrict the scope of the Unicode characters to search and replace would obviously speed things up. 可能有更好的方法来执行此操作,并且能够限制Unicode字符的搜索和替换范围显然会加快处理速度。 Turning off .EnableEvents , .ScreenUpdating , etc would likewise help. 关闭.EnableEvents.ScreenUpdating等同样有帮助。 I believe the calculation was already at manual. 我相信计算已经是手动完成了。 I intentionally left a DoEvents in the loop as my first run was several thousand different unichars. 我故意将DoEvents留在循环中,因为我的第一次运行是数千种不同的单字符。

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

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