|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- '提取1.xls数据,B列为address,C列为Size,F为data
- Function dataDict1(rng As Range)
- Dim d, a, i%
- a = rng
- Set d = CreateObject("Scripting.dictionary")
- For i = 3 To UBound(a) '从第3行开始
- If IsNumeric(a(i, 2)) Then
- For j = 0 To a(i, 2) - 1
- d(Right(Hex(Val("&H" & Right(a(i, 1), 4)) + j), 4)) = "'" & Mid(a(i, 5), j * 2 + 1, 2)
- Next
- End If
- Next
- Set dataDict1 = d
- End Function
- '提取2.xls数据,B列为address,D列为Size,G为data
- Function dataDict2(rng As Range)
- Dim d, a, i%
- a = rng
- Set d = CreateObject("Scripting.dictionary")
- For i = 3 To UBound(a) '从第3行开始
- If IsNumeric(a(i, 3)) Then
- For j = 0 To a(i, 3) - 1
- d(Right(Hex(Val("&H" & Right(a(i, 1), 4)) + j), 4)) = "'" & IIf(a(i, 6) Like "N/A*", a(i, 6), Mid(a(i, 6), j * 2 + 1, 2))
- Next
- End If
- Next
- Set dataDict2 = d
- End Function
- '
- Sub test()
- Dim d1, d2, wb, wb1, wb2, a1, a2, i%
- '如果1.xls,2.xls已经打开则直接提取数据,否则先打开再提取数据
- For Each wb In Workbooks
- If wb.Name = "1.xls" Then
- Set wb1 = wb
- ElseIf wb.Name = "2.xls" Then
- Set wb2 = wb
- End If
- Next
-
- '1.xls和2.xls应该和3.xls在同一路径
- If IsEmpty(wb1) Then Set wb1 = Workbooks.Open(ThisWorkbook.Path & "\1.xls")
- If IsEmpty(wb2) Then Set wb2 = Workbooks.Open(ThisWorkbook.Path & "\2.xls")
- '数据都在1.xls和2.xls的第一个工作表
- Set d1 = dataDict1(Application.Intersect(wb1.Sheets(1).UsedRange, wb1.Sheets(1).Columns("B:F")))
- Set d2 = dataDict2(Application.Intersect(wb2.Sheets(1).UsedRange, wb2.Sheets(1).Columns("B:G")))
-
- With Sheet2
- '填充
- .[a2].Resize(d1.Count, 1) = Application.Transpose(d1.keys)
- .[B2].Resize(d1.Count, 1) = Application.Transpose(d1.items)
- ' .[E2].Resize(d2.Count, 1) = Application.Transpose(d2.keys)
- .[C2].Resize(d2.Count, 1) = Application.Transpose(d2.items)
- '对比
- a1 = .[B2].Resize(d1.Count, 2)
- ReDim a2(UBound(a1))
- For i = 1 To UBound(a1)
- If a1(i, 1) = a1(i, 2) Then
- a2(i - 1) = "OK"
- Else
- a2(i - 1) = "NG"
- End If
- Next
- .[d2].Resize(UBound(a1), 1) = Application.Transpose(a2)
- .Activate
- End With
- End Sub
复制代码 |
|