单个单元格上的拆分字符串 - VBA功能

时间:2016-10-12 15:28:20

标签: excel string vba list

我需要帮助拆分由";"分隔的字符串。并列出如下例所示的列表:

for (int i = list.size() - 1; i >= 0; i--) {
    System.out.println(list.get(i));
}

如何使用VBA功能实现此目的?

提前致谢,

MD

1 个答案:

答案 0 :(得分:0)

我喜欢在午餐时间写下与工作无关的代码:

Sub NamesToList()
    'Assumes that a cell is selected which contains a string which
    'looks something like "Name1;Name2;Name3"
    'The sub splits that list and then puts the values immediately
    'below the selected cell. Warning: this doesn't check if there is
    'room for those values and will overwrite existing values with no
    'warning

    Dim A As Variant, n As Long, R As Range
    Set R = Selection
    A = Split(R.Value, ";")
    n = UBound(A)
    Range(R.Offset(1), R.Offset(n + 1)).Value = Application.WorksheetFunction.Transpose(A)
End Sub

如果在选择A1时调用此子,则名称将自动插入A2:A4。没有进行错误检查,因此您可能应该调整此代码以确保它没有例如覆盖任何值。