|
- Sub 生成()
- Application.ScreenUpdating = False
- Dim d, wb As Workbook, arr, i As Integer, j As Integer, key, record, totalrow&, fso
- Set d = CreateObject("Scripting.Dictionary")
- arr = Range("a1").CurrentRegion
- For i = 2 To UBound(arr)
- If d.exists(arr(i, 1)) = False Then
- d(arr(i, 1)) = i
- Else
- d(arr(i, 1)) = d(arr(i, 1)) & "!" & i
- End If
- Next
- Set fso = CreateObject("Scripting.FileSystemObject")
- If fso.FolderExists(ThisWorkbook.Path & "\机构代码") = True Then fso.GetFolder(ThisWorkbook.Path & "\机构代码").Delete
- fso.CreateFolder ThisWorkbook.Path & "\机构代码"
- For Each key In d.keys
- record = Split(d(key), "!")
- totalrow = UBound(record)
- ReDim result(1 To totalrow + 2, 1 To UBound(arr, 2))
- For i = 0 To totalrow
- For j = 1 To UBound(arr, 2)
- If i = 0 Then result(1, j) = arr(1, j)
- result(i + 2, j) = arr(record(i), j)
- Next
- Next
- Set wb = Workbooks.Add
- With wb.Worksheets(1)
- .Cells.NumberFormatLocal = "@"
- .Cells(1, 1).Resize(totalrow + 2, UBound(arr, 2)).Value = result
- End With
- wb.SaveAs ThisWorkbook.Path & "\机构代码" & key
- wb.Close False
- Next
- Application.ScreenUpdating = True
- Set wb = Nothing
- Set d = Nothing
- End Sub
复制代码 |
|