Excel: 如何列出区间序列的数字?

Excel: How to list the numbers of a sequence of intervals?

I was looking for a way to do what the title says and just found the follow thread:

But my case is a little different: there is a cell where will be typed some integer numbers separated by commas and/or hyphens, making multiple intervals (pretty much like that 'page range' field found in many print dialog boxes).

For example: 1-3,9-11,17,19,21-22,25

And I need the result to be returned in another cell but with each number separated on lines, like below:

1

2

3

9

10

11

17

19

21

22

25

I can do this using excel native functions, but for only one interval (e.g.: 1-9). For anything more than that the formula doesn't work.

Is there a way to accomplish all of this in VBA?

Could some one please point me a direction?

Any help is much appreciated.

Regards

[更新]

我已经使用代码几个星期了,但刚刚意识到我需要避免零、重复数字以及降序排列的数字。

例如

0,1,3-10,8,12,14,16,16,22,27,24-30,36,42,39

因此,我需要一个#VALUE!如果键入的数字不是按升序排列或者发现零个或重复的数字,则返回错误消息。

我正在编写代码,并且已经解决了上述仅针对输入间隔 (x-y) 的问题, 但我想不出用逗号分隔的离散数字的解决方案。

非常感谢任何帮助。

到目前为止的代码:

Function RANGEX(strInput As String) As String
Dim intCurrent  As Integer
Dim outputArray() As Variant
Dim intCount As Integer

intCount = 1

For Each i In Split(strInput, ",")
    ReDim Preserve outputArray(1 To intCount)

    If InStr(i, "-") > 0 Then
        If CInt(Split(i, "-")(0)) > 0 And CInt(Split(i, "-")(0)) < CInt(Split(i, "-")(1)) Then
            For x = CInt(Split(i, "-")(0)) To CInt(Split(i, "-")(1))
                intCurrent = x
                ReDim Preserve outputArray(1 To intCount)
                outputArray(intCount) = intCurrent
                intCount = intCount + 1
            Next x
        Else
            RANGEX = CVErr(xlErrValue)
        End If
    Else
        ReDim Preserve outputArray(1 To intCount)
        intCurrent = CInt(i)
        outputArray(intCount) = intCurrent
        intCount = intCount + 1
    End If

Next i

RANGEX = Join(outputArray, ",")

End Function

回答你的问题:是的,有可能。

下面是一个工作代码示例。理想情况下,你会抽象出一些重复的部分(调整输出数组的大小并添加下一个元素),但由于你的问题不是关于任何具体的,这里有一些 'just' 有效的东西 - 指向你正确的方向。将您提供的内容放入单元格 A1,所需的输出将写入单元格 B1:

Sub Testing()
Dim strInput As String
Dim strOutput As String
Dim intCurrent  As Integer
Dim outputArray() As Variant
Dim intCount As Integer

intCount = 1

strInput = ActiveSheet.Cells(1, 1)
For Each i In Split(strInput, ",")
    ReDim Preserve outputArray(1 To intCount)
    If InStr(i, "-") > 0 Then
        For x = CInt(Split(i, "-")(0)) To CInt(Split(i, "-")(1))
            intCurrent = x
            ReDim Preserve outputArray(1 To intCount)
            outputArray(intCount) = intCurrent
            intCount = intCount + 1
        Next x
    Else
        ReDim Preserve outputArray(1 To intCount)
        intCurrent = CInt(i)
        outputArray(intCount) = intCurrent
        intCount = intCount + 1
    End If
Next i
strOutput = Join(outputArray, vbCrLf)
ActiveSheet.Cells(1, 2) = strOutput
End Sub

在此之上,对输入做出某些假设(分隔符为“,”)。

当然,您可以将其转换为函数,以便能够直接在 Excel.

中将其用作公式