Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1844|回复: 3

[已解决]计算相邻连续的范围,求算法

[复制链接]
发表于 2017-8-4 14:11 | 显示全部楼层 |阅读模式
QQ图片20170804140552.png
运行宏后
r1=range(cells(2,1),cells(5,1))
r2=range(cells(6,1),cells(12,1))
最佳答案
2017-8-4 14:53
  1. Sub aaa()
  2. Dim i&, k&, r1, r2
  3. For i = 2 To [a65536].End(3).Row - 1
  4.   For k = i + 1 To [a65536].End(3).Row
  5.     If Cells(k, 1) <> Cells(i, 1) Then Exit For
  6.   Next k
  7.   If i = 2 Then
  8.     r1 = Range(Cells(i, 1), Cells(k - 1, 1))
  9.   Else
  10.     r2 = Range(Cells(i, 1), Cells(k - 1, 1))
  11.   End If
  12.   i = k - 1
  13. Next i
  14. End Sub
复制代码
发表于 2017-8-4 14:53 | 显示全部楼层    本楼为最佳答案   
  1. Sub aaa()
  2. Dim i&, k&, r1, r2
  3. For i = 2 To [a65536].End(3).Row - 1
  4.   For k = i + 1 To [a65536].End(3).Row
  5.     If Cells(k, 1) <> Cells(i, 1) Then Exit For
  6.   Next k
  7.   If i = 2 Then
  8.     r1 = Range(Cells(i, 1), Cells(k - 1, 1))
  9.   Else
  10.     r2 = Range(Cells(i, 1), Cells(k - 1, 1))
  11.   End If
  12.   i = k - 1
  13. Next i
  14. End Sub
复制代码
回复

使用道具 举报

发表于 2017-8-4 15:19 | 显示全部楼层
  1. Sub aaa()
  2.     arr = Range("a1:a" & [a65536].End(3).Row)
  3.     ReDim rng(UBound(arr)) As Range
  4.     r = 2
  5.     For i = 3 To UBound(arr)
  6.         If arr(i, 1) <> arr(r, 1) Then
  7.             n = n + 1
  8.             r = i
  9.         Else
  10.             Set rng(n) = Cells(r, 1).Resize(i - r + 1)
  11.         End If
  12.     Next i
  13.     For k = 0 To n
  14.         If Not rng(k) Is Nothing Then msg = msg & "," & rng(k).Address(0, 0)
  15.     Next
  16.     MsgBox "相邻连续范围为:" & Mid(msg, 2)
  17. End Sub
复制代码

工作簿1.rar

9.09 KB, 下载次数: 3

回复

使用道具 举报

 楼主| 发表于 2017-8-4 16:09 | 显示全部楼层
谢谢两位老师,代码已经调试通过。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-3-28 19:16 , Processed in 0.295595 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表