|
- Sub test0()
-
- Dim strFile As String
-
- 'With Application.FileDialog(msoFileDialogOpen)
- '.InitialFileName = ThisWorkbook.Path
- 'With .Filters
- '.Clear
- '.Add "Excel Files", "*.xls*"
- 'End With
- '.AllowMultiSelect = False
- 'If .Show Then strFile = .SelectedItems(1) Else Exit Sub
- 'End With
-
- strFile = "E:\总单.xls"
- If Dir(strFile) = "" Then MsgBox strFile & " 文件不存在!", 64: Exit Sub
-
- DoApp False
-
- Dim ar, br, cr() As Long, Dict As Object
- Dim wkb As Workbook, wks As Worksheet
- Dim i As Long, j As Long
- Dim idx As Long, pos As Long, cnt As Long
-
- pos = 254
- Set Dict = CreateObject("Scripting.Dictionary")
-
- Set wkb = Workbooks.Open(strFile, 0, False, , "123", "123")
- Set wks = wkb.Worksheets("Sheet2")
- With wks
- .Unprotect "456" '原的没密码保护工作表
- With .Range("A1").CurrentRegion
- br = .Columns(2).Value
- ar = .Offset(, 2).Resize(, pos)
- ReDim cr(1 To UBound(br))
- End With
- For i = 1 To UBound(br) Step 3
- If Not Dict.Exists(br(i, 1)) Then Dict.Add br(i, 1), i
- ar(i, pos) = "|"
- For j = 1 To UBound(ar, 2) - 1
- If Len(ar(i, j)) Then
- ar(i, pos) = ar(i, pos) & ar(i, j) & "|" & ar(i + 1, j) & "|" & ar(i + 2, j) & "|"
- cr(i) = cr(i) + 1
- Else
- Exit For
- End If
- Next
- Next
- End With
-
- With ThisWorkbook.Worksheets("Sheet1")
- .Unprotect "456"
- br = .Range("AA1", .Cells(.Rows.Count, "X").End(xlUp))
- .Unprotect "456"
- End With
- For i = 2 To UBound(br)
- If Dict.Exists(br(i, 1)) Then
- idx = Dict(br(i, 1))
- If InStr(ar(idx, pos), "|" & br(i, 2) & "|" & br(i, 3) & "|" & br(i, 4) & "|") = 0 Then
- cr(idx) = cr(idx) + 1
- For j = 2 To UBound(br, 2)
- ar(idx + j - 2, cr(idx)) = br(i, j)
- ar(idx, pos) = ar(idx, pos) & br(i, j) & "|"
- Next
- cnt = cnt + 1
- End If
- End If
- Next
-
- If cnt Then wks.Range("C1").Resize(UBound(ar), WorksheetFunction.Max(cr)) = ar
- wks.Protect "456" '设密码保护工作表
- wkb.Close True
-
- Set wks = Nothing
- Set wkb = Nothing
- Set Dict = Nothing
-
- DoApp True
- MsgBox cnt & " 条数据更新", 64
- End Sub
- Function DoApp(Optional b As Boolean = True)
- With Application
- .ScreenUpdating = b
- .DisplayAlerts = b
- .Calculation = -b * 30 - 4135
- End With
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|