|
本帖最后由 ·遁去的一· 于 2023-6-3 13:33 编辑
两列数据互相比对,求优化 sub 双向2()的代码,提高速度,本机测试100万记录,100万KEY的情况下,要4分钟跑- Sub 双向2()
- Dim s1, s2, jg1, jg2, jgwz1, jgwz2 '定义比对数据源的位置列变量
- Dim s1arr, s2arr, jg1arr, jg2arr, jgwz1arr, jgwz2arr '定义数据源数组和各个行列的变量
- Dim row1, row2 '定义总行数
- Dim i, k, js, pr, sk, sk1, j '定义循环计数
- Dim dic1(1 To 10) As Object
- Dim t, key, Key_Is_Exist
- t = Timer
- Application.ScreenUpdating = False
- s1 = [b1]
- s2 = [b2]
- jg1 = [d1]
- jg2 = [d2]
- jgwz1 = [f1]
- jgwz2 = [f2]
- row1 = Range(s1 & Rows.count).End(xlUp).Row
- row2 = Range(s2 & Rows.count).End(xlUp).Row
- '清理数据显示区
- Range(jgwz1 & ":" & jgwz1).Clear
- Range(jgwz2 & ":" & jgwz2).Clear
- 'For i = 2 To 1000001
- ' Range(s1 & i) = i
- 'Next
- '将数据源,结果位置分别赋值
- '数据源
- s1arr = Range(s1 & "2:" & s1 & row1)
- s2arr = Range(s2 & "2:" & s2 & row2)
- '提取数据
- jg1arr = Range(jg1 & "2:" & jg1 & row1)
- jg2arr = Range(jg2 & "2:" & jg2 & row2)
- '结果位置
- jgwz1arr = Range(jgwz1 & "2:" & jgwz1 & row1)
- jgwz2arr = Range(jgwz2 & "2:" & jgwz2 & row2)
- '数据2在数据1中有几个相同结果
- For i = 1 To 10 '生成10个字典
- Set dic1(i) = CreateObject("scripting.dictionary")
- Next
- sk1 = 0
- For i = 1 To UBound(s1arr) '循环数据1生成字典
- key = s1arr(i, 1)
- k = sk1 \ 100000 + 1 '计算字典号
- Key_Is_Exist = False
-
- For j = 1 To 10 '在原字典中查找是不是已有
- If dic1(j).exists(key) Then
- Key_Is_Exist = True
- Exit For
- End If
- Next
-
- If Key_Is_Exist Then
- dic1(k)(s1arr(i, 1)) = dic1(k)(s1arr(i, 1)) & "," & jg1arr(i, 1) & "$" & jg1 & "$" & i + 1
- Else
- dic1(k).Add s1arr(i, 1), jg1arr(i, 1) & "$" & jg1 & "$" & i + 1
- sk1 = sk1 + 1
- End If
- Next
-
- For k = 1 To 10
- 'Debug.Print dic1(k).count
- If dic1(k).count > 0 Then
- js = dic1(k).keys
- pr = dic1(k).items
- sk = UBound(Split(pr(0), ",")) + 1
- For i = 0 To UBound(js)
- If dic1(k).exists(js(i)) Then
- sk = UBound(Split(pr(i), ",")) + 1
- dic1(k)(js(i)) = "(" & sk & ")" & dic1(k)(js(i))
- End If
- Next
- End If
- Next
- pr = dic1(k - 1).items
- '数据2在数据1中比较,提取结果到jgwz2
- For i = 1 To UBound(s2arr)
- key = s2arr(i, 1)
-
- For j = 1 To 10
- If dic1(j).exists(key) Then
- jgwz2arr(i, 1) = dic1(j)(key)
- Exit For
- End If
- Next
-
- Next
- Range(jgwz2 & "1").Value = s2 & "比较" & s1 & "返回" & jg1
- Range(jgwz2 & "2").Resize(UBound(jgwz2arr), 1) = jgwz2arr
-
- For k = 1 To 10
- Set dic1(k) = Nothing
- Next
- Set jgwz2arr = Nothing
- '数据1在数据2中有几个相同结果
- For i = 1 To 10
- Set dic1(i) = CreateObject("scripting.dictionary")
- Next
- sk1 = 0
- For i = 1 To UBound(s2arr) '循环数据2生成字典
- key = s2arr(i, 1)
- k = sk1 \ 100000 + 1
- Key_Is_Exist = False
-
- For j = 1 To 10
- If dic1(j).exists(key) Then
- Key_Is_Exist = True
- Exit For
- End If
- Next
-
- If Key_Is_Exist Then
- dic1(k)(s2arr(i, 1)) = dic1(k)(s2arr(i, 1)) & "," & jg2arr(i, 1) & "$" & jg2 & "$" & i + 1
- Else
- dic1(k).Add s2arr(i, 1), jg2arr(i, 1) & "$" & jg2 & "$" & i + 1
- sk1 = sk1 + 1
- End If
- Next
-
- For k = 1 To 10
- ' Debug.Print dic1(k).count
- If dic1(k).count > 0 Then
- js = dic1(k).keys
- pr = dic1(k).items
- sk = UBound(Split(pr(0), ",")) + 1
- For i = 0 To UBound(js)
- If dic1(k).exists(js(i)) Then
- sk = UBound(Split(pr(i), ",")) + 1
- dic1(k)(js(i)) = "(" & sk & ")" & dic1(k)(js(i))
- End If
- Next
- End If
- Next
- pr = dic1(k - 1).items
- '数据1在数据2中比较,提取结果到jgwz1
- For i = 1 To UBound(s1arr)
- key = s1arr(i, 1)
-
- For j = 1 To 10
- If dic1(j).exists(key) Then
- jgwz1arr(i, 1) = dic1(j)(key)
- Exit For
- End If
- Next
-
- Next
- Range(jgwz1 & "1").Value = s1 & "比较" & s2 & "返回" & jg2
- Range(jgwz1 & "2").Resize(UBound(jgwz1arr), 1) = jgwz1arr
- MsgBox "核对完成,共用时" & Timer - t & "秒" & "共核对" & row1 - 1 & "/" & row2 - 1 & "条记录"
- Application.ScreenUpdating = True
- For k = 1 To 10
- Set dic1(k) = Nothing
- Next
- Set jgwz1arr = Nothing
- End Sub
复制代码
完一次,追问一个问题,能否把2次互比,合成一次比对,达到相同效果,主要是互相提取指定的列数据,比重是可以一次完成的
|
|