|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
全数组操作,速度有效提升。- Sub aa() '1有2没有
- With Sheet1
- r1 = .[b65536].End(3).Row: arr1 = .Range("b2:e" & r1)
- r2 = .[j65536].End(3).Row: arr2 = .Range("j2:m" & r2)
- End With
- Set d = CreateObject("scripting.dictionary")
- Set d1 = CreateObject("scripting.dictionary")
- Set d2 = CreateObject("scripting.dictionary")
- For i = 1 To UBound(arr2) '把2中字轨对应行号存入字典
- zg = Trim(arr2(i, 1)) '字轨
- d(zg) = d(zg) & "," & i
- If Not d2.exists(zg) Then '字典d2为数组trr用。数组trr存放各字轨1有2没有的所有数字。
- k = k + 1
- d2(zg) = k
- End If
- Next
-
- For i = 1 To UBound(arr1) '把1中字轨对应行号存入字典
- zg = Trim(arr1(i, 1)) '字轨
- d1(zg) = d1(zg) & "," & i
- Next
-
- Dim crr(0 To 5000000) As Integer
- dk = d.keys
- ReDim trr(1 To d.Count, 1 To 5000000) As Long '数组trr存放各字轨1有2没有的所有数字。
- For i = 0 To UBound(dk)
- zg = dk(i)
- xrr = Split(d(zg), ",")
- For j = 1 To UBound(xrr)
- k = Val(xrr(j))
- s1 = Val(arr2(k, 3)): s2 = Val(arr2(k, 4)) '2中第k行的起始终止号码
- For p = s1 To s2
- crr(p) = 1 '2中存在号码p,数组crr的第p项赋值为1
- Next
- Next
-
- yrr = Split(d1(zg), ",")
- xl = d2(zg): trr(xl, 1) = zg 'xl为此字轨在trr中的行数,trr(xl,1)存字轨名
- For j = 1 To UBound(yrr)
- k = Val(yrr(j))
- s1 = Val(arr1(k, 3)): s2 = Val(arr1(k, 4)) '1中第k行的起始终止号码
- For p = s1 To s2
- If crr(p) = 0 Then
- trr(xl, 2) = trr(xl, 2) + 1 'trr(xl,2)对应字轨1有2没有的数字个数
- trr(xl, trr(xl, 2) + 2) = p 'trr(xl,3)开始存对应字轨1有2没有的所有数字
- End If
- Next
-
- Next
- Erase crr '清空crr
- Next
-
- k = 0
- Dim brr(1 To 100000, 1 To 4)
- For i = 1 To UBound(trr)
- zg = trr(i, 1)
- ux = trr(i, 2) + 2 'trr第i行的最大列
- For j = 3 To ux '按要求输出
- k = k + 1
- brr(k, 1) = zg
- brr(k, 2) = trr(i, j)
- If trr(i, ux) - trr(i, j) = ux - j Then '最后一个数和当前数比,如果递增,直接填充后结束大循环
- brr(k, 3) = trr(i, ux)
- brr(k, 4) = ux - j + 1
- Exit For
- End If
- For t = j + 1 To ux '当前数后面所有数和当前数比,如果不递增,填充后结束小循环,当前数跳到不递增的那个数之前一个
- If trr(i, t) - trr(i, j) <> t - j Then
- brr(k, 3) = trr(i, t - 1)
- brr(k, 4) = t - j
- j = t - 1
- Exit For
- End If
- Next
- Next
- Next
- With Sheet3
- .Range("a:d").Clear
- .[a1].Resize(1, 4) = Array("字轨", "起始号码", "终止号码", "份数")
- .[a2].Resize(k, 4) = brr
- .Range("b2:c" & k + 1).NumberFormatLocal = "00000000"
- .Activate
- End With
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|