将Excel单元格中的字母数字字符串扩展为逗号分隔的单个字母数字

时间:2014-11-12 23:18:38

标签: string excel comma

我正在寻找一种方法,将包含诸如“C365-C370”之类字符串的单元格放入包含该范围的单元格中,该单元格以空格或逗号分隔,例如“C365,C366,C367” ,C368,C369,C370“。

或许,更复杂的是,采用具有单个字母数字组合的单元格,例如 “C362,C365-C370,C374,C382”进入含有这些扩散并通过空格或逗号分隔的细胞,如“C362 C365 C366 C367 C368 C369 C370 C374 C382”。

提前致谢, 大卫

1 个答案:

答案 0 :(得分:0)

选择包含以下数据的单元格:

C100-C110

并运行这个简单的宏:

Sub SpreadThem()
    Dim r As Range, N1 As Long, N2 As Long, i As Long
    Dim st As String, CH As String
    For Each r In Selection
        If InStr(r.Text, "-") > 0 Then
            ary = Split(r.Text, "-")
            N1 = Mid(ary(0), 2)
            N2 = Mid(ary(1), 2)
            CH = Left(ary(0), 1)
            For i = N1 To N2
                st = st & "," & CH & i
            Next i
            st = Mid(st, 2)
            r.Value = st
        End If
    Next r
End Sub

内容将更改为:

C100,C101,C102,C103,C104,C105,C106,C107,C108,C109,C110