本帖最后由 志哥 于 2012-12-17 20:47 编辑
蓝桥玄霜 发表于 2010-10-18 12:50
实例3 A列中显示1 ~ 1000中被6除余1和余5 的数字
一、问题的提出:
有1、2、3…1000一千个数字,要求编写 ...
首先感谢LZ的帖子教我学会了很多知识。下面根据示例代码简化,抛砖引玉。
示例3 代码简化,根据狼版 的代码改编
- Sub 余1余5()
- Dim dic As Object, i As Long, arr
- Set dic = CreateObject("Scripting.Dictionary")
- For i = 1 To 1000
- If Abs(i Mod 6 - 3) = 2 Then dic.Add i, ""
- Next
- arr = WorksheetFunction.Transpose(dic.keys)
- Cells(1, 2).Resize(UBound(arr), 1) = arr
- Set dic = Nothing
- End Sub
复制代码
示例6代码简化
- Sub shili6()
- Dim d As Object, a, b, i&
- Dim ss$, n!
- 'Me.UsedRange.Offset(3, 0) = ""
- Cells(3, 2).Resize(1, 8) = Split("位置,大系统编号,小系统编号,项目名称,比例相同,楼层数,长度明细,数量", ",")
- Range(Cells(4, 2), Cells(Rows.Count, 9)).ClearContents
- a = Sheet1.Range(Sheet1.[a4], Sheet1.[i65536].End(xlUp))
- Set d = CreateObject("scripting.dictionary")
- ReDim b(1 To UBound(a), 1 To 8)
- For i = 1 To UBound(a)
- ss = a(i, 1) & a(i, 2) & a(i, 4) & a(i, 5) & a(i, 6) & a(i, 8)
- If Not d.Exists(ss) Then
- n = n + 1
- d.Add ss, n
- b(n, 1) = a(i, 2): b(n, 2) = a(i, 5): b(n, 3) = a(i, 6): b(n, 4) = a(i, 4)
- b(n, 5) = a(i, 1): b(n, 6) = a(i, 8): b(n, 7) = a(i, 9)
- b(d(ss), 8) = b(d(ss), 7)
- Else
- b(d(ss), 7) = b(d(ss), 7) & "+" & a(i, 9)
- b(d(ss), 8) = (b(d(ss), 8) + a(i, 9))
- End If
- Next
- For i = 1 To d.Count
- b(i, 8) = b(i, 5) * b(i, 6) * b(i, 8) / 100
- Next
- [b4].Resize(n, 8) = b
- End Sub
复制代码
示例10代码简化,少了几个循环,速度更快。
- Sub 示例10()
- Dim i&, Myr&, Arr
- Dim d, x, rng
- Application.ScreenUpdating = False
- Set d = CreateObject("Scripting.Dictionary")
- Sheet1.Activate
- Myr = [a65536].End(xlUp).Row
- Range("A1:C" & Myr).Sort Key1:=Range("C2"), Order1:=xlAscending, Key2:=Range( _
- "A2"), Order2:=xlAscending, Key3:=Range("B2"), Order3:=xlDescending, _
- Header:=xlYes 'AC列升序,B列降序
- Arr = Range("a2:c" & Myr)
- [e:g].ClearContents
- j = 2
- For i = 1 To UBound(Arr)
- x = Arr(i, 1) & "|" & Arr(i, 3)
- If Not d.exists(x) Then
- Cells(j, 5).Resize(1, 3) = WorksheetFunction.Index(Arr, i, 0)
- j = j + 1
- End If
- d(x) = ""
- Next
- Set d = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码
|