我在一个单元格中的excel中有以下数据
Pankaj sharma;#234;#khushal verma;#786;#parul datta;#456;#
我想使用VBA将其转换为此:
pankaj sharma
khushal verma
OP的评论代码:
Sub test1()
Dim rng As Range
Dim InputRng As Range, OutRng As Range
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection.Range("F3")
Set InputRng = Application.InputBox("Range(single cell) :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Arr = VBA.Split(InputRng.Range("A1").Value, ";#",-1,1)
OutRng.Resize(UBound(Arr) - LBound(Arr) + 1).Value = Application.Transpose(Arr)
End Sub
答案 0 :(得分:1)
以下是两种基于您的代码的实现。
首先使用Split
:
Sub TestSplit()
Dim Arr() As String
Dim Res() As String
Dim m As Long, i As Long
Dim InputRng As Range, OutRng As Range
Dim xTitleId As String
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection.Range("F3")
Set InputRng = Application.InputBox("Range (single cell) :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Arr = VBA.Split(InputRng.Range("A1").Value, ";#", -1, 1)
m = UBound(Arr) \ 2
ReDim Preserve Res(m)
For i = 0 To m
Res(i) = Arr(i * 2)
Next
OutRng.Resize(UBound(Res) - LBound(Res) + 1).Value = Application.Transpose(Res)
End Sub
第二个有后期绑定VBScript.RegExp
:
Sub TestRegExp()
Dim oMatch As Object
Dim InputRng As Range, OutRng As Range
Dim xTitleId As String
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection.Range("F3")
Set InputRng = Application.InputBox("Range (single cell) :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "([\s\S]*?);#\d{3,3};#"
For Each oMatch In .Execute(InputRng.Range("A1").Value)
OutRng.Cells(1, 1).Value = oMatch.SubMatches(0)
Set OutRng = OutRng.Offset(1, 0)
Next
End With
End Sub
答案 1 :(得分:0)
Split()和Join()是用于简单解析/格式化的标准VBScript工具,它使用分隔符,而不是分隔符。差异:如果&#34 ;;#"是一个分隔符,行
"pankaj sharma;#234;#khushal verma;#786;#parul datta;#456;#"
包含7个元素,其中最后一个为空。规则:n个分隔符分隔n + 1个元素。如果&#34 ;;#"被视为分隔符,行中有6个元素。规则:n分隔符分隔n个元素。
您的数据是结构化的 - 不仅仅是元素列表,而是一对配对列表(每个配对包括一个名称(?)和一个数字)。这意味着:该行应代表3对元素和&#34 ;;#"必须是分隔符。
如何将Split()/ Join()应用于具有分隔符格式的数据?
在代码中:
>> s0 = "pankaj sharma;#234;#khushal verma;#786;#parul datta;#456;#"
>> a = Split(s0, ";#")
>> WScript.Echo ">" & Join(a, "|") & "<"
>> ReDim Preserve a(UBound(a) - 1)
>> s1 = Join(a, ";#")
>> WScript.Echo s1
>> s1 = s1 & ";#"
>> WScript.Echo s1
>>
>pankaj sharma|234|khushal verma|786|parul datta|456|<
pankaj sharma;#234;#khushal verma;#786;#parul datta;#456
pankaj sharma;#234;#khushal verma;#786;#parul datta;#456;#
>> WScript.Echo CStr(s0 = s1)
>>
True
或者,您可以在Split()之前从输入字符串中删除尾随分隔符。更好:从一开始就使用分隔符。
如何处理数据的子结构?如果你有g组n个子元素(例如3组对),则使用n步循环。在代码中:
>> For i = 0 To UBound(a) Step 2
>> WScript.Echo a(i)
>> WScript.Echo a(i + 1)
>> WScript.Echo "----"
>> Next
>>
pankaj sharma
234
----
khushal verma
786
----
parul datta
456
----
>>
要访问单个元素,请像这样计算索引:从零开始的所需组数* n +从零开始的所需元素数。例如。 khushal verma的数字元素=&gt; 1 * 2 + 1 = 3:
>> WScript.Echo a(1 * 2 + 1)
>>
786
>>