单元格中是否具有收缩序列或数字范围的功能?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单元格中是否具有收缩序列或数字范围的功能?相关的知识,希望对你有一定的参考价值。

我有一个电子表格,其中包含以逗号分隔的年份范围,在其他用途​​中可能很难阅读,例如:

1900, 1901, 1902, 1904, 1905, 1906, 1910, 1911, 1912, 1913, 1914, 1915, etc.

理想情况下,我会将范围缩小/截断以使其更清晰:

1900–1902, 1904–1906, 1910–1915, etc.

我如何最好地在Excel中做到这一点?

答案

您可以将其放入函数中,因此可以在VBA中使用它,甚至可以用作公式:

因此,您所需要做的就是将输入序列除以", ",然后遍历数字以测试当前数字f(i)是否与上一个数字f(i-1)连续。如果它们不是连续的,则需要结束当前范围并开始新的范围。

只有一个棘手的部分,那就是避免将单个年份范围显示为…, 1918-1918, …并改为输出…, 1918, …

Option Explicit

Public Function ReduceConsecutiveNumbersToRanges(ByVal InputSequence As String) As String
    Dim InputArr() As String
    InputArr = Split(InputSequence, ", ")

    Dim OutputSeqStart As Long  'current range start
    OutputSeqStart = InputArr(0)

    Dim OutputSequence As String

    'start output sequence
    OutputSequence = OutputSeqStart

    'find gaps to write ranges
    Dim i As Long
    For i = LBound(InputArr) + 1 To UBound(InputArr)
        If Not InputArr(i) = InputArr(i - 1) + 1 Then
            'current number is not in sequence so we need to end current sequence and start a new one
            If InputArr(i - 1) = OutputSeqStart Then 'check if it is a single-year-range only to avoid outputs like `…, 1918-1918, …` and output `…, 1918, …` instead
                OutputSequence = OutputSequence & ", " & InputArr(i)
                OutputSeqStart = InputArr(i)
            Else
                OutputSequence = OutputSequence & "-" & InputArr(i - 1) & ", " & InputArr(i)
                OutputSeqStart = InputArr(i)
            End If
        End If
    Next i

    'end output sequence if it is not a single-year range
    If Not InputArr(i - 1) = OutputSeqStart Then
        OutputSequence = OutputSequence & "-" & InputArr(i - 1)
    End If

    ReduceConsecutiveNumbersToRanges = OutputSequence
End Function

Public Sub TestFunction()
    Debug.Print ReduceConsecutiveNumbersToRanges("1900, 1901, 1902, 1904, 1905, 1906, 1910, 1911, 1912, 1913, 1914, 1915, 1918, 1920, 1921")
    'output will be: 1900-1902, 1904-1906, 1910-1915, 1918, 1920-1921
End Sub

它将减少

1900, 1901, 1902, 1904, 1905, 1906, 1910, 1911, 1912, 1913, 1914, 1915, 1918, 1920, 1921

1900-1902, 1904-1906, 1910-1915, 1918, 1920-1921
另一答案

涉及一些替代,但是如果您不希望包含宏,我相信以下作品:

=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXTJOIN(", ",1,IF(ISNUMBER(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s"),FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s[not(preceding::*[1] = .-1 and following::*[1] = .+1)]"),0)),FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s")," "))," ,","-"),", ","|"),"-","")),"| ","-"),"|",", ")

如果您认为连续两年是一个范围,并且您希望它们由连字符连接起来,则:

=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXTJOIN(", ",1,IF(ISNUMBER(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s"),FILTERXML("<t><s>"&TEXTJOIN("</s><s>",1,IF(ISNUMBER(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s"),FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s[not(preceding::*[1] = .-1 and following::*[1] = .+1)]"),0)),FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s")," "))&"</s></t>","//s[following::*=.+1]"),0)),FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s")&"-",IF(ISNUMBER(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s"),FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s[not(preceding::*[1] = .-1 and following::*[1] = .+1)]"),0)),FILTERXML("<t><s>"&SUBSTITUTE(A1,", ","</s><s>")&"</s></t>","//s")," "))),"-, ","-")," ,","#"),", ","|"),"#"," ")),"| ","-"),"|",", ")

是的.....这太疯狂了!...但是这里是结果:

enter image description here

请注意通过Ctrl Shift Enter

进入

以上是关于单元格中是否具有收缩序列或数字范围的功能?的主要内容,如果未能解决你的问题,请参考以下文章

滚动时 Tableview 单元格中标签的自动收缩功能不起作用

具有变化范围的自动填充/填充公式

是否有一个 VBA 代码允许修改数字,同时在单元格中保持单词不变?

怎么在excel中一个单元格中显示出自动计算的标记背景颜色的单元格数值的和

excel - 自动填充递增多个数字

如何在下一个空单元格中复制和粘贴动态表值?