简体   繁体   English

Excel VBA中的split命令在数组中的每个字符之间添加空格

[英]Split command in Excel VBA adding a space between every character in array

I am reading a text file and splitting the data string at each space using the following code. 我正在读取一个文本文件,并使用以下代码在每个空格处拆分数据字符串。

 L = f.ReadLine
        If L = "" Then GoTo errorpoint:
            On Error GoTo errorpoint:
            sl = Split(L, " ", -1)

However, the array I get back takes, for example, the word "Contact" in the text file and turns it into " C ontact " in a single cell of my array (see below) and I'm not sure why. 但是,我返回的数组在文本文件的单个单元格中使用了例如文本文件中的“ Contact”一词,并将其转换为“ C ontact”(请参见下文),我不确定为什么。

Data Output in VBA Watch Window: VBA监视窗口中的数据输出:

VBA监视窗口中的数据输出

I have tried using 我尝试使用

For i = 0 To UBound(sl)
     sl(i) = Replace(sl(i), " ", "")
Next

Afterwards to remove the spaces, but that doesn't appear to be removing the spaces from my data. 之后删除空格,但这似乎并不是从我的数据删除空格。 Any ideas how I can prevent the code from adding a space between every character in the first place? 有什么想法可以防止代码首先在每个字符之间添加空格?

I then need to check whether sl(1) and sl(2) contain the words "Contact" and "Stress" respectively, but that condition cannot be met currently, the cells contain " C ontact " and " S tress ", so I changed the if loop to reflect that, but the condition is still not met. 然后,我需要检查sl(1)sl(2)包含单词“ Contact”和“ Stress”,但是当前无法满足该条件,单元格包含“ C ontact”和“ S tress”,所以我更改了if循环以反映这一点,但条件仍不满足。

In addition, I cannot convert my data from String > Double because of the unusual formatting, it seems. 另外,由于格式异常,我无法从String> Double转换数据。

As @LocEngineer pointed out in the comments, during the update of the external software that produces the text file I was reading, the text encoding had been changed to UTF-16 and the additional characters between my data were chr(0) Null values from the ASCII table, not spaces (chr(32) in ASCII). 正如@LocEngineer在评论中指出的那样,在更新生成我正在读取的文本文件的外部软件期间,文本编码已更改为UTF-16,并且我的数据之间的其他字符为chr(0)来自ASCII表,而不是空格(ASCII中的chr(32))。

For i = 0 To UBound(sl)
     sl(i) = Replace(sl(i), Chr(0), "")
Next

This snippet solved the problem. 此代码段解决了问题。

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

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