Excel列表/范围扩展

时间:2019-01-03 22:39:43

标签: excel

我可以在excel中做以下任何事情吗? 例: 6001至6003 8020-8024 假设上面有几百行。 我希望它以excel形式编写: 6001 6002 6003 8020 8021 8023 8024

2 个答案:

答案 0 :(得分:0)

特殊连接对象

enter image description here

代码

Function SPECC(CellRange As Range) As String

    Const cStrDel1 As String = " -- "   ' Source First Delimiter
    Const cStrDel2 As String = " "      ' Source Second Delimiter
    Const cStrDel3 As String = " "      ' Target Delimiter

    Dim vnt1 As Variant     ' Source Array
    Dim vnt2 As Variant     ' Source Split Array
    Dim lng1 As Long        ' First String Value
    Dim lng2 As Long        ' Second String Value
    Dim i As Integer        ' Source Array Counter
    Dim j As Integer        ' Values Counter
    Dim strB As String      ' String Builder

    vnt1 = Split(CellRange.Cells(1, 1), cStrDel1)
    If UBound(vnt1) = 0 Then
        On Error Resume Next
        SPECC = CLng(vnt1(0))
        Exit Function
    End If
    For i = 0 To UBound(vnt1)
        If i > 0 Then
            lng1 = CLng(vnt2(1))
          Else
            lng1 = CLng(vnt1(0))
        End If
        vnt2 = Split(vnt1(i + 1), cStrDel2)
        lng2 = CLng(vnt2(0))
        If lng1 <= lng2 Then
            For j = lng1 To lng2
                GoSub BuildString
            Next
          Else
            For j = lng1 To lng2 Step -1
                GoSub BuildString
            Next
        End If
        If UBound(vnt2) = 0 Then Exit For
    Next
    SPECC = strB

Exit Function

BuildString:
    If strB <> "" Then
        strB = strB & cStrDel3 & j
      Else
        strB = j
    End If
    Return

End Function

答案 1 :(得分:-1)

您是否要将这些值连接在一起?如果是这样,那么这里有一个UDF可以做到这一点:

Function RangeConCat(rng As Range, Separator As String) As String
Dim Cell As Range
Dim s As String
    For Each c In rng
        s = s & c.Value & Separator
    Next c
RangeConCat = Left(s, Len(s) - Len(Separator))
End Function