Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将字符串拆分为多个单元格_Excel_Vba_Split - Fatal编程技术网

Excel 将字符串拆分为多个单元格

Excel 将字符串拆分为多个单元格,excel,vba,split,Excel,Vba,Split,我有个问题 我有这个: (“布弗斯;奥特曼;212544;卡萨”) 我想将此单元格拆分为多个单元格,如: 单元[1]=boufous单元[2]=othman单元[3]=212544 cell[4]=casa MS中每个olMailItem的 //代码在这里 i=i+1 下一项 假设olMailItem是字符串,下面是代码 Dim str1, str2 olMailItem = "boufous;othman;212544;casa" str1 = Split(olMailItem, ";") s

我有个问题

我有这个:

(“布弗斯;奥特曼;212544;卡萨”)

我想将此单元格拆分为多个单元格,如:

单元[1]=boufous
单元[2]=othman
单元[3]=212544
cell[4]=casa

MS中每个olMailItem的

//代码在这里
i=i+1
下一项

假设olMailItem是字符串,下面是代码

Dim str1, str2
olMailItem = "boufous;othman;212544;casa"
str1 = Split(olMailItem, ";")
str2 = UBound(str1)
For i = 0 To str2
    Debug.Print str1(i)
Next i

如果您必须使用VBA,我个人喜欢通过两种方式进行工作。想象一下以下数据:

1.

根据给出的注释,您可以使用名为Split的函数。下面是一个小脚本,它展示了如何通过一个小循环来实现这一点:

Sub UseSplit()

Dim arr As Variant
Dim lr As Long, x As Long

With Sheet1 'Change CodeName accordingly
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    arr = .Range("A1:A" & lr)
    For x = LBound(arr) To UBound(arr)
        .Cells(x, 2).Resize(1, UBound(Split(arr(x, 1), ";")) + 1) = Split(arr(x, 1), ";")
    Next x
End With

End Sub

2.

第二种方法是利用内置函数,使用
TextToColumns
函数将分隔文本写入其他列。这不会涉及循环。下面是一个小例子:

Sub UseSplit()

Dim rng As Range
Dim lr As Long

With Sheet1 'Change CodeName accordingly
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    Set rng = .Range("A1:A" & lr)
    rng.TextToColumns Destination:=.Range("B1"), Semicolon:=True
End With

End Sub
这样做的好处是,
Split
将返回一个字符串值数组,而text-to-columns将不会:

本来是数字的值实际上是数字


问题仍然是,您真的需要通过VBA工作吗?不管是哪种方式,祝你的项目好运=)

你可以利用请看看这个有用的关于如何使用拆分函数
我想将此单元格拆分为多个单元格,如:…
为什么VBA而不是Excel的
数据选项卡|文本到列
使用
作为分隔符?