在单元格B2中,我有一长串以“ ATT TAG:”开头的列表。我想知道是否可以将每个新的“ ATT TAG:”的数据分为几列。我的数据如下所示:
[![在此处输入图片描述] [1]] [1]
我尝试使用InStr和Left和right这样的函数,但我认为可能会有更好的方法来拆分此类数据。
[1]:
答案 0 :(得分:0)
您已经用VBA标记了问题,但是您在问题中引用了公式函数,因此我不清楚您是要查找函数还是VBA。话虽如此,我将继续介绍您如何标记问题。
要使用VBA分割字符串,可以使用Split()函数。
示例:
Dim wordArray() As String
Dim inputString As String
inputString = "ATT TAG: HELLO, ATT TAG: WORLD, ATT TAG: ZOMBIES"
wordArray() = Split(inputString, "ATT TAG:")
从那里开始,使用数组创建并填充您认为合适的新列。
更新:
假设您有一些看起来像这样的数据;一切都卡在一个单元格中,并用新行分割:
|-----------------|---|---|---|---|
| ATT TAG: Value1 | | | | |
| ATT TAG: Value2 | | | | |
| ATT TAG: Value3 | | | | |
|-----------------|---|---|---|---|
|-----------------|---|---|---|---|
|-----------------|---|---|---|---|
您可以遍历数组,然后将结果粘贴到新列中:
Sub splitTest()
' declare variables
Dim wordArray() As String
Dim inputString As String
Dim pasteValue As String
Dim lastColumn As Long
' get the input from cell A1
inputString = Range("A1")
' split the input into an array
' using "ATT TAG:" as the delimiter
wordArray() = Split(inputString, "ATT TAG:")
' loop through the array
For Each element In wordArray
' find the last used column from row 1
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
' clean up the value to be pasted; remove whitespace
' and any newlines -- represented by "Chr(10)"
pasteValue = Replace(Trim(element), Chr(10), "")
' set the last used column to the element value
Cells(1, lastColumn + 1).Value = pasteValue
Next element
End Sub
哪个会产生:
|-----------------|--------|--------|--------|
| ATT TAG: Value1 | | | |
| ATT TAG: Value2 | Value1 | Value2 | Value3 |
| ATT TAG: Value3 | | | |
|-----------------|--------|--------|--------|
|-----------------|--------|--------|--------|
|-----------------|--------|--------|--------|
答案 1 :(得分:0)
当excel TextToColumn
中已经有工具构建时,为什么要使事情变得复杂呢?
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:=";", TrailingMinusNumbers:=True
OR
Selection.TextToColumns Destination:=Range("B8"), DataType:=xlFixedWidth, _
OtherChar:=":", FieldInfo:=Array(Array(0, 1), Array(7, 1))
End Sub