我使用的应用程序在一个单元格中导出数组中的数据字符串,如下所示:
"data1"= value1; "data2"= value2; "data3"= value3; "data4"= value4; "data5"= value5;
我想要一个等式或宏或者可以像这样拆分它的东西:
----------------------------------------------------------------
| | data0 | data1 | data2 | data3 | data4 | data5 |
----------------------------------------------------------------
| 1 | ARRAY | value1 | value2 | value3 | value4 | value5 |
----------------------------------------------------------------
| 2 | ARRAY | value1 | value2 | value3 | value4 | value5 |
----------------------------------------------------------------
| 3 | ARRAY | value1 | value2 | value3 | value4 | value5 |
----------------------------------------------------------------
. . . . and so on.
这可能吗?
以下是文档中的字符串:
"search-terms1" = Chinese jade; "search-terms2" = Chinese archers; "search-terms3" = Antique jade; "search-terms4" = Asian jade; "search-terms5" = China history;
它始终是search-terms1 - search-terms5,但每行的'values'会有所不同。
Windows XP上的Excel 2003
答案 0 :(得分:3)
这将从A2开始,然后关闭多少行有数据。它将值拆分为右侧的列。
Sub SplitSearch()
Dim vaTerms As Variant
Dim vaSearch As Variant
Dim rCell As Range
Dim vaOutput As Variant
Dim i As Long
For Each rCell In Sheet1.Range("A2", Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp)).Cells
vaSearch = Split(rCell.Value, ";")
ReDim vaOutput(1 To 1, 1 To UBound(vaSearch))
For i = LBound(vaSearch) To UBound(vaSearch)
vaTerms = Split(vaSearch(i), "=")
If UBound(vaTerms) > -1 Then
vaOutput(1, i + 1) = Trim(vaTerms(UBound(vaTerms)))
End If
Next i
rCell.Offset(0, 1).Resize(1, UBound(vaOutput, 2)).Value = vaOutput
Next rCell
End Sub
答案 1 :(得分:1)
你可能应该对完整的数组字符串进行搜索和替换,用换行符替换分号,\ n或者vbCrLf BEFORE 将数组字符串放入excel,因为单元格字符串大小如果您在字符串中提取大量数据,则限制可能会导致内容截断。 (东西可能被切断,你不会知道)
少一些可能无法正确导入您正在处理的所有结果。
使用换行符替换分号后导入文本,或使用'='作为分隔符打开文件
答案 2 :(得分:0)
首先在“;”处拆分字符串,而不是在“=”处拆分。下面的代码可以帮助您入门:
Dim s As String
Dim parts As Variant
s = """data1""= value1; ""data2""= value2;"
parts = Split(s, ";")
For i = 0 To UBound(parts)
Debug.Print parts(i)
' TODO: Split part along "=" and insert into cell
Next i