Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 从excel中的列表创建从开始到结束的数字组_List_Excel_Grouping_Vba - Fatal编程技术网

List 从excel中的列表创建从开始到结束的数字组

List 从excel中的列表创建从开始到结束的数字组,list,excel,grouping,vba,List,Excel,Grouping,Vba,总而言之,我需要做的是与这篇文章相反的事情: 换句话说,我在a列中有一个数字列表,我希望在B列和C列中从开始到结束的数字范围内对它们进行分组 i、 e (执行VBA代码后) 如果无法对该值进行分组,则它将是相同的开始和结束编号(如上面示例中的N°25所示)我的快速试用 Sub Ranges() With ActiveWorkbook.Worksheets("Sheet1") Dim r As Integer Dim beg As Integer Dim en As In

总而言之,我需要做的是与这篇文章相反的事情:

换句话说,我在a列中有一个数字列表,我希望在B列和C列中从开始到结束的数字范围内对它们进行分组 i、 e

(执行VBA代码后)

如果无法对该值进行分组,则它将是相同的开始和结束编号(如上面示例中的N°25所示)

我的快速试用

Sub Ranges()
With ActiveWorkbook.Worksheets("Sheet1")
    Dim r As Integer
    Dim beg As Integer
    Dim en As Integer
    i = 1
    r = 1
    beg = .Cells(1, 1).Value
    en = beg
    While .Cells(i, 1).Value
        If .Cells(i, 1).Value > en + 1 Then
            .Cells(r, 2).Value = beg
            .Cells(r, 3).Value = en

            beg = .Cells(i, 1).Value
            en = beg
            r = r + 1
        Else
            en = .Cells(i, 1).Value
        End If
        i = i + 1
    Wend
    .Cells(r, 2).Value = beg
    .Cells(r, 3).Value = en
End With
End Sub
结果

A   B   C
1   1   3
2   6   9
3   12  12
6   33  38
7   44  45
8   55  56
9   66  67
12      
33      
34      
35      
36      
37      
38      
44      
45      
55      
56      
66      
67      
A       B       C
10001   10001   10003
10002   10006   10009
10003   100012  100012
10006   100033  100038
10007   100044  100045
10008   100055  100056
10009   100066  100067
100012      
100033      
100034      
100035      
100036      
100037      
100038      
100044      
100045      
100055      
100056      
100066      
100067    
尝试:

s作为范围
设置s=范围(“A1:A10”)
r只要
r=1
p只要
p=s.单元(1,1).值
对于s中的每个c
'添加范围的开始
如果是c-1p那么
'结束最后一个范围
如果r>1,则
单元(r,3)=p
r=r+1
如果结束
“开始新的范围
单元(r,2)=c
如果结束
p=c
环
单元格(r,3)=s单元格(s计数,1)

塔莫·埃尔文的想法很正确,但最多只能处理32k的数字。如果改为将整数改为Long,脚本可以处理更大的数字,同时删除许多人看到的“溢出”错误

Sub Ranges()
With ActiveWorkbook.Worksheets("Sheet1")
    Dim r As Long
    Dim beg As Long
    Dim en As Long
    i = 1
    r = 1
    beg = .Cells(1, 1).Value
    en = beg
    While .Cells(i, 1).Value
        If .Cells(i, 1).Value > en + 1 Then
            .Cells(r, 2).Value = beg
            .Cells(r, 3).Value = en

            beg = .Cells(i, 1).Value
            en = beg
            r = r + 1
        Else
            en = .Cells(i, 1).Value
        End If
        i = i + 1
    Wend
    .Cells(r, 2).Value = beg
    .Cells(r, 3).Value = en
End With
End Sub
结果

A   B   C
1   1   3
2   6   9
3   12  12
6   33  38
7   44  45
8   55  56
9   66  67
12      
33      
34      
35      
36      
37      
38      
44      
45      
55      
56      
66      
67      
A       B       C
10001   10001   10003
10002   10006   10009
10003   100012  100012
10006   100033  100038
10007   100044  100045
10008   100055  100056
10009   100066  100067
100012      
100033      
100034      
100035      
100036      
100037      
100038      
100044      
100045      
100055      
100056      
100066      
100067    

你在哪里解释了哪些数字组合在一起?看起来你每做三个数字,但是“25”有点让你觉得不对劲。那么,解释一下它们是如何分组的。只是好奇@大卫:我认为op需要每组连续数字的开始和结束,例如1-3、6-8、25、28-30。@glh-ahhh你看到了一个我没有看到的模式:)问题是我们只认为一个给定的模式适用于这里,我们没有证据证明这一点。op也没有指定模式。嗨,我试着在一长串值(几乎整个列)上使用它,过了一会儿,它确实给了我一些结果,效果很好,但突然它显示“运行时错误'6':溢出”没有解决这个问题,有什么建议吗?是的,我认为如果列一直填充到末尾,并且当循环尝试查找sheets max行之外的值时,这会做一些有趣的事情。“While”循环可以使用另一个条件来检查“i”在递增时的值。如果超出边界,则退出循环。
A       B       C
10001   10001   10003
10002   10006   10009
10003   100012  100012
10006   100033  100038
10007   100044  100045
10008   100055  100056
10009   100066  100067
100012      
100033      
100034      
100035      
100036      
100037      
100038      
100044      
100045      
100055      
100056      
100066      
100067