替换范围为

时间:2017-02-10 01:29:08

标签: excel excel-vba excel-formula vba

如何在单元格中查找序列号,并用范围替换它们? 例如:

改变:

  

1,3,5,15,16,17,25,28,29,31 ......

为:

  

... 1,3,5,15-17,25,28-29,31

数字已经排序,即按升序排列。

感谢。

3 个答案:

答案 0 :(得分:4)

我想看一个有趣的问题,不需要循环遍历序列(首先需要排序)检查顺序构建

此功能

  1. 强制字符串到范围地址
  2. 使用ref.child("users").child(userID!).child("signal_received").observeSingleEvent(of: .value, with: { (snapshot) in if(snapshot.value as! String == "true"){ self.view.makeToast("Signal received") snapshot.ref.setValue("true") } }) { (error) in } 将连续的行组合在一起
  3. 操纵字符串以删除列标识符
  4. enter image description here

    循环不是必需的,更短的版本!

    Union

答案 1 :(得分:2)

我想尝试使用 Microsoft365 的 LET() 作为捕获变量的方法的全公式解决方案。

以下解决方案仅将 3 个以上的连续数字视为数字范围,而不是两个。

enter image description here

B1 中的公式:

=LET(X,FILTERXML("<t><s>"&SUBSTITUTE(A1,",","</s><s>")&"</s></t>","//s"),Y,TRANSPOSE(FILTERXML("<t><s>"&SUBSTITUTE(A1,",","</s><s>")&"</s></t>","//s[preceding::*[1]+1=.][following::*[1]-1=.]")),SUBSTITUTE(TEXTJOIN(",",,FILTERXML("<t><s>"&TEXTJOIN("</s><s>",,IF(MMULT(--(X=Y),SEQUENCE(COUNTA(Y),,,0)),"-",X))&"</s></t>","//s[.*0=0 or (.='-' and preceding::*[1]*0=0)]")),",-,","-"))

答案 2 :(得分:0)

虽然基于给定范围/区域的答案很有意思,但它存在一些缺陷:

  • 仅限于255个字符的输入字符串
  • 相对较慢

这是一个基于数组循环的基本方法。它可以处理长字符串。在我的测试中,它大约运行1/3。它还有不要求对输入进行排序的好处

Function NumOut2(strIn As String) As String
    Dim arrIn() As String
    Dim arrBuckets() As Long
    Dim i As Long
    Dim InRange As Boolean
    Dim mn As Long, mx As Long

    arrIn = Split(strIn, ", ")
    mn = arrIn(0)
    mx = arrIn(0)
    For i = 1 To UBound(arrIn)
        If arrIn(i) < mn Then
            mn = arrIn(i)
        ElseIf arrIn(i) > mx Then
            mx = arrIn(i)
        End If
    Next

    ReDim arrBuckets(mn To mx)
    For i = 0 To UBound(arrIn)
        arrBuckets(arrIn(i)) = arrIn(i)
    Next
    NumOut2 = LBound(arrBuckets)
    InRange = False
    For i = LBound(arrBuckets) + 1 To UBound(arrBuckets)
        If arrBuckets(i) > 0 Then
            If arrBuckets(i) = arrBuckets(i - 1) + 1 Then
                If InRange Then

                Else
                    InRange = True
                    NumOut2 = NumOut2 & "-"
                End If
            Else
                If InRange Then
                    NumOut2 = NumOut2 & arrBuckets(i - 1) & ", " & arrBuckets(i)
                Else
                    NumOut2 = NumOut2 & ", " & arrBuckets(i)
                End If
            End If
        Else
            If InRange Then
                NumOut2 = NumOut2 & arrBuckets(i - 1)
            End If
            InRange = False
        End If
    Next

End Function