如何使用UBound分隔列中的string并将分隔的string放入VBA中的另一列

我有一个电话号码列(如D列),其中一些用分号(;)或冒号(,)分隔两个电话号码。
我想分割这两个电话号码,并把第二个列向左(如B列),留下原来的D列单元格中的第一个。 如果单元格中只有一个电话号码,则保持独立。
我知道它可能会使用UBound,并拆分数组? 但是,如何?
有人可以帮我吗?
谢谢。

如果你完全肯定他们的电话号码只能是两个电话号码,那么这个应该可以做到,至less对我来说是这样:

 Option Explicit Sub test() Dim chkRng As Range Dim cell As Range Dim arr As Variant Set chkRng = Range("D1:D" & (Range("D" & Rows.Count).End(xlUp).Row)) For Each cell In chkRng If InStr(1, cell.Value, ",") > 0 Then arr = Split(cell.Value, ",") ElseIf InStr(1, cell.Value, ";") > 0 Then arr = Split(cell.Value, ";") Else GoTo continue End If cell.Offset(, -2).Value = Trim(arr(1)) cell.Value = Trim(arr(0)) continue: Next cell End Sub 

尝试这个

 Sub rep() For Each c In Sheets("Sheet1").Range("D1:D65").Cells ' Change the range that you want If InStr(c.Value, ",") > 0 Then phonNumb = Split(c, ",") Range("D" & c.Row) = phonNumb(0) Range("C" & c.Row) = phonNumb(1) ElseIf InStr(c.Value, ";") > 0 Then phonNumb = Split(c, ";") Range("D" & c.Row) = phonNumb(0) Range("C" & c.Row) = phonNumb(1) End If Next c End Sub 

有可能是一个更简单的方法来做到这一点,但你可以使用Instr函数来定位分号(;)或逗号(,),然后将它们分开。 例如下面的工作:

 Sub SeparateNumbers() Dim Rng As Range Set Rng = ActiveSheet.Range("D1:D" & ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row) For Each Cell In Rng If InStr(1, Cell.Value, ";") Then Cell.Offset(0, -2).Value = Trim(Mid(Cell.Value, InStr(1, Cell.Value, ";") + 1, (Len(Cell.Value) - (InStr(1, Cell.Value, ";"))))) Cell.Value = Mid(Cell.Value, 1, InStr(1, Cell.Value, ";") - 1) End If If InStr(1, Cell.Value, ",") Then Cell.Offset(0, -2).Value = Trim(Mid(Cell.Value, InStr(1, Cell.Value, ",") + 1, (Len(Cell.Value) - (InStr(1, Cell.Value, ","))))) Cell.Value = Mid(Cell.Value, 1, InStr(1, Cell.Value, ",") - 1) End If Next Cell End Sub