如何处理 VBA 字符串中的多个空格?
How do I deal with multiple spaces in a VBA string?
我正在将 Excel 单元格中的字符串中的单词提取到一个集合中,以便我可以拆分为多个列。有些单词之间有多个 space。在这种情况下,我想在提取的拆分范围中插入一个空白列,换句话说,我想在我的集合中插入一个空白项目。我可以很好地提取单词,但由于某种原因,我无法正确评估具有多个 space 的实例。 VBA 似乎将任何单个 space 解释为多个 space。
我知道Range有TextToColumns方法,但是不适合我的需求
这是字符串的一小部分:
Shift 2 Shift 1 Shift 2 Shift 1 Shift 2 Shift 1 Shift 2 Week 21 Week 22 Week 23 Week 24 Week 25 Week 26 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35
此子集的预期输出将是以下项目的集合。第 35 周和第 27 周之间有一个空字符串:
"Shift 2", "Shift 1", "Shift 2" ... "Week 34", "Week 35", " ", "Week
27", "Week28" ...
我尝试使用 InStr 查找多个 space:'secondCaracterPos' 的值增加是因为它是循环的一部分。
If InStr(secondCharacterPos, multioutput(Range("a3")), " ") Then secondDateCollection.Add " ": secondCharacterPos = secondCharacterPos + 10
以及:
If InStr(secondCharacterPos, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 1, multioutput(Range("a3")), " ") _
And InStr(secondCharacterPos + 2, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 3, multioutput(Range("a3")), " ") _
And InStr(secondCharacterPos + 4, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 5, multioutput(Range("a3")), " ") _
And InStr(secondCharacterPos + 6, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 7, multioutput(Range("a3")), " ") _
And InStr(secondCharacterPos + 8, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 9, multioutput(Range("a3")), " ") _
Then secondDateCollection.Add " ": secondCharacterPos = secondCharacterPos + 10
我也尝试过 MID 的短格式和长格式,如上所述。
我怀疑我忽略了一些非常小的东西。任何帮助将不胜感激。
这似乎有效:
s = "Shift 2 Shift 1 Shift 2 Shift 1 Shift 2 Shift 1 Shift 2 Week 21 Week 22 Week 23 Week 24 Week 25 Week 26 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35"
? """" & Replace(Replace(s, Space(11), ""","" "","""), Space(2), """,""") & """"
"Shift 2","Shift 1","Shift 2","Shift 1","Shift 2","Shift 1","Shift 2","Week 21","Week 22","Week 23","Week 24","Week 25","Week 26","Week 27","Week 28 Week 29","Week 30","Week 33","Week 34","Week 35"," ","Week 27","Week 28 Week 29","Week 30","Week 33","Week 34","Week 35"," ","Week 27","Week 28 Week 29","Week 30","Week 33","Week 34","Week 35"," ","Week 27","Week 28 Week 29","Week 30","Week 33","Week 34","Week 35"
我正在将 Excel 单元格中的字符串中的单词提取到一个集合中,以便我可以拆分为多个列。有些单词之间有多个 space。在这种情况下,我想在提取的拆分范围中插入一个空白列,换句话说,我想在我的集合中插入一个空白项目。我可以很好地提取单词,但由于某种原因,我无法正确评估具有多个 space 的实例。 VBA 似乎将任何单个 space 解释为多个 space。
我知道Range有TextToColumns方法,但是不适合我的需求
这是字符串的一小部分:
Shift 2 Shift 1 Shift 2 Shift 1 Shift 2 Shift 1 Shift 2 Week 21 Week 22 Week 23 Week 24 Week 25 Week 26 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35
此子集的预期输出将是以下项目的集合。第 35 周和第 27 周之间有一个空字符串:
"Shift 2", "Shift 1", "Shift 2" ... "Week 34", "Week 35", " ", "Week 27", "Week28" ...
我尝试使用 InStr 查找多个 space:'secondCaracterPos' 的值增加是因为它是循环的一部分。
If InStr(secondCharacterPos, multioutput(Range("a3")), " ") Then secondDateCollection.Add " ": secondCharacterPos = secondCharacterPos + 10
以及:
If InStr(secondCharacterPos, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 1, multioutput(Range("a3")), " ") _
And InStr(secondCharacterPos + 2, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 3, multioutput(Range("a3")), " ") _
And InStr(secondCharacterPos + 4, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 5, multioutput(Range("a3")), " ") _
And InStr(secondCharacterPos + 6, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 7, multioutput(Range("a3")), " ") _
And InStr(secondCharacterPos + 8, multioutput(Range("a3")), " ") And InStr(secondCharacterPos + 9, multioutput(Range("a3")), " ") _
Then secondDateCollection.Add " ": secondCharacterPos = secondCharacterPos + 10
我也尝试过 MID 的短格式和长格式,如上所述。
我怀疑我忽略了一些非常小的东西。任何帮助将不胜感激。
这似乎有效:
s = "Shift 2 Shift 1 Shift 2 Shift 1 Shift 2 Shift 1 Shift 2 Week 21 Week 22 Week 23 Week 24 Week 25 Week 26 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35 Week 27 Week 28 Week 29 Week 30 Week 33 Week 34 Week 35"
? """" & Replace(Replace(s, Space(11), ""","" "","""), Space(2), """,""") & """"
"Shift 2","Shift 1","Shift 2","Shift 1","Shift 2","Shift 1","Shift 2","Week 21","Week 22","Week 23","Week 24","Week 25","Week 26","Week 27","Week 28 Week 29","Week 30","Week 33","Week 34","Week 35"," ","Week 27","Week 28 Week 29","Week 30","Week 33","Week 34","Week 35"," ","Week 27","Week 28 Week 29","Week 30","Week 33","Week 34","Week 35"," ","Week 27","Week 28 Week 29","Week 30","Week 33","Week 34","Week 35"