|
Option Explicit
Sub test()
Dim ar, i&, j&, dic As Object, Rng As Range, wks As Worksheet
DoApp False
Set dic = CreateObject("Scripting.Dictionary")
With Worksheets("BOM全表").[A1].CurrentRegion
ar = .Resize(.Rows.Count + 1)
Set Rng = .Rows(1)
For i = 2 To UBound(ar) - 1
If Len(ar(i, 1)) Then
If Not dic.exists(ar(i, 1)) Then
Set dic(ar(i, 1)) = Rng
End If
For j = i To UBound(ar) - 1
If j = UBound(ar) - 1 Or ar(j + 1, 1) <> 0 Then
Set dic(ar(i, 1)) = Union(dic(ar(i, 1)), .Rows(i & ":" & j))
Exit For
End If
Next j
End If
Next
End With
ar = Worksheets("需求").[A1].CurrentRegion.Value
With Workbooks.Add
For i = 2 To UBound(ar)
If dic.exists(ar(i, 1)) Then
With .Worksheets.Add(after:=.Worksheets(.Worksheets.Count))
.Name = ar(i, 1)
dic(ar(i, 1)).Copy
.[A1].PasteSpecial xlPasteColumnWidths
dic(ar(i, 1)).Copy .[A1]
End With
End If
Next
For Each wks In .Worksheets
If wks.Name Like "*Sheet*" Then wks.Delete
Next
End With
Call CreateList
Set dic = Nothing: Set Rng = Nothing
DoApp
Beep
End Sub
Function DoApp(Optional b As Boolean = True)
With Application
.ScreenUpdating = b
.DisplayAlerts = b
.Calculation = -b * 30 - 4135
End With
End Function
Sub CreateList()
Dim i&
Worksheets.Add(before:=Worksheets(1)).Name = "工作表目录"
[A1].Resize(, 2) = [{"序号", "需求BOM"}]
For i = 2 To Worksheets.Count
Cells(i, 1).Value = i - 1
ActiveSheet.Hyperlinks.Add Cells(i, 2), "", Worksheets(i).Name & "!A2", _
"单击打开:" & Worksheets(i).Name, Worksheets(i).Name
Worksheets(i).Hyperlinks.Add Worksheets(i).Cells(2, 1), "", _
Worksheets(1).Name & "!B" & i, "返回目录"
Next i
Columns("A:D").AutoFit
End Sub
|
评分
-
1
查看全部评分
-
|