- Sub test0()
- Dim strFile As String
- ' With Application.FileDialog(msoFileDialogOpen)
- ' .InitialFileName = ThisWorkbook.Path
- ' With .Filters
- ' .Clear
- ' .Add "Excel文件(xls*)", "*.xls*"
- ' End With
- ' .AllowMultiSelect = False
- ' If .Show Then strFile = .SelectedItems(1) Else Exit Sub
- ' End With
- strFile = "F:\总单.xlsm"
- If Dir(strFile) = "" Then MsgBox strFile & " 文件不存在!", 64: Exit Sub
-
- Application.ScreenUpdating = False
-
- Dim ar, br, cr() As Long, Dict As Object
- Dim wkb As Workbook, wks As Worksheet
- Dim i As Long, j As Long, idx As Long, pos As Long
-
- pos = 666
-
- Set Dict = CreateObject("Scripting.Dictionary")
- Set wkb = Workbooks.Open(strFile, False, , , 123, 123)
- Set wks = wkb.Worksheets("资料")
- With wks
- .Unprotect 456
- With .Range("A1").CurrentRegion
- br = Application.Rept(.Rows(2), 1)
- ar = .Offset(2).Resize(pos)
- ReDim cr(1 To UBound(br))
- End With
- ar(pos, 2) = 0
- For j = 1 To UBound(br) Step 3
- Dict.Add br(j), j
- cr(j) = .Cells(1, j).End(xlDown).Row - 2
- ar(pos, j) = "|"
- For i = 1 To cr(j)
- ar(pos, j) = ar(pos, j) & ar(i, j) & "|" & ar(i, j + 1) & "|" & ar(i, j + 2) & "|"
- Next
- Next
- End With
- br = ThisWorkbook.Worksheets("单价").Range("A1").CurrentRegion.Offset(, 1).Resize(, 4)
-
- For i = 2 To UBound(br)
- If Dict.exists(br(i, 1)) Then
- idx = Dict(br(i, 1))
- If InStr(ar(pos, idx), "|" & br(i, 2) & "|" & br(i, 3) & "|" & br(i, 4) & "|") = 0 Then
- ar(pos, 2) = ar(pos, 2) + 1
- cr(idx) = cr(idx) + 1
- For j = 2 To UBound(br, 2)
- ar(cr(idx), idx + j - 2) = br(i, j)
- ar(pos, idx) = ar(pos, idx) & br(i, j) & "|"
- Next
- End If
- End If
- Next
-
- wks.Range("A3").Resize(WorksheetFunction.Max(cr), UBound(ar, 2)) = ar
- wks.Protect 456
- Set wks = Nothing
- wkb.Close True
- Set wkb = Nothing
- Set Dict = Nothing
-
- Application.ScreenUpdating = True
- MsgBox ar(pos, 2) & " 行更新", 64
- End Sub
复制代码 |