|
- Sub 提取()
- Dim wb As Workbook
- Dim ws As Worksheet
- Dim cellValue As Variant
- Dim filePath As String
- Dim MaxRow As Long
- Dim NameList As String
- Dim NameArr() As String
- Dim I As Long, J As Long, X As Long
- Dim dict As Object
- Dim dict2 As Object
- Dim dict3 As Object
- Dim TitleList As String
-
- On Error Resume Next
- Application.ScreenUpdating = False ' 关闭屏幕更新
- ' 创建新的字典
- Set dict = CreateObject("Scripting.Dictionary")
- Set dict2 = CreateObject("Scripting.Dictionary")
- Set dict3 = CreateObject("Scripting.Dictionary")
- NameList = "erp订单1.xlsx,erp订单2.xlsx" ' 工作簿名称列表
- TitleList = "A,E"
- NameArr = Split(NameList, ",")
- For I = 0 To UBound(NameArr)
- filePath = ThisWorkbook.Path & "" & NameArr(I)
- Set wb = Workbooks.Open(filePath) ' 打开工作簿
- Set ws = wb.Sheets(1) ' 第一个工作表
- ' 读取内容
- With ws
- MaxRow = .Cells(.Rows.Count, 1).End(xlUp).Row '第1列最后一行
- DateArr = .Range(.Cells(2, 1), .Cells(MaxRow, 1)).Value '默认第1行是标题行
- End With
- wb.Close SaveChanges:=False ' 关闭工作簿,不保存更改
- For J = 1 To UBound(DateArr)
- ff = UBound(DateArr)
- If Err.Number = 0 Then
- dict.Add DateArr(J, 1), DateArr(J, 1)
- Else
- dict.Add DateArr, DateArr
- End If
- Err.Clear
- Next J
- DoEvents
- Next I
- ' 读取SRM
- filePath = ThisWorkbook.Path & "\srm订单.xls"
- Set wb = Workbooks.Open(filePath) ' 打开工作簿
- NameArr = Split(TitleList, ",")
- Set wb = Workbooks.Open(filePath) ' 打开工作簿
- For I = 1 To wb.Sheets.Count
- Set ws = wb.Sheets(I) ' 第I个工作表
- For J = 0 To UBound(NameArr)
- ' 读取内容
- With ws
- MaxRow = .Cells(.Rows.Count, NameArr(J)).End(xlUp).Row '第J列最后一行
- DateArr = .Range(.Cells(1, NameArr(J)), .Cells(MaxRow, NameArr(J))).Value
- End With
- For X = 1 To UBound(DateArr)
- ff = UBound(DateArr)
- If Err.Number = 0 Then
- dict2.Add DateArr(X, 1), DateArr(X, 1)
- Else
- dict2.Add DateArr, DateArr
- End If
- Err.Clear
- Next X
- Next J
- DoEvents
- Next I
- wb.Close SaveChanges:=False ' 关闭工作簿,不保存更改
- ' 清除对象变量
- Set ws = Nothing
- Set wb = Nothing
- ' 开始进行判断
- ' srm有但erp找不到,E列数据
- For Each key1 In dict2.keys
- If Not dict.Exists(key1) Then
- dict3.Add key1, key1
- End If
- DoEvents
- Next
- With Sheet1
- .Cells(15, "E").Resize(dict3.Count, 1) = WorksheetFunction.Transpose(dict3.keys)
- End With
- dict3.RemoveAll
- ' ERP有但SR找不到,E列数据
- For Each key1 In dict.keys
- If Not dict2.Exists(key1) Then
- dict3.Add key1, key1
- End If
- DoEvents
- Next
- With Sheet1
- .Cells(15, "B").Resize(dict3.Count, 1) = WorksheetFunction.Transpose(dict3.keys)
- End With
- Set dict = Nothing
- Set dict2 = Nothing
- Set dict3 = Nothing
- Application.ScreenUpdating = True ' 开启屏幕更新
- End Sub
复制代码
使用了3个字典,不知道大数据量下会占用多少时间。 |
|