|
本帖最后由 baofa2 于 2024-4-17 19:36 编辑
209901拆分另存模板.zip
(38.96 KB, 下载次数: 10)
- Option Explicit
- Sub test0()
-
- Dim titleRow As Long, splitCol As Long
- Dim Conn As Object, rs As Object, SQL As String
- Dim strPath As String, strFullName As String, strField As String, strValue As String
-
- titleRow = 1
- splitCol = 5
- strField = "[" & Cells(1, splitCol).Value & "]"
-
- strPath = ThisWorkbook.Path & "\分簿"
- If Dir(strPath, vbDirectory) = "" Then MkDir strPath
-
- Set Conn = CreateObject("ADODB.Connection")
- Set rs = CreateObject("ADODB.Recordset")
- Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0 XML;Data Source=" & ThisWorkbook.FullName
-
- SQL = "SELECT DISTINCT " & strField & " FROM [" & ActiveSheet.Name & "$] WHERE LEN(" & strField & ")"
- rs.Open SQL, Conn, 1, 3
-
- While Not rs.EOF
- strValue = rs.Fields(0).Value
- strFullName = strPath & Application.PathSeparator & strValue & ".xlsx"
- If Dir(strFullName) <> "" Then Kill strFullName
- Conn.Execute "SELECT * INTO [" & strFullName & "].[" & strValue & "] FROM [" & ActiveSheet.Name & "$A:G] WHERE " & strField & "='" & strValue & "'"
- rs.MoveNext
- Wend
-
- rs.Close
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Beep
- End Sub
- Sub test1()
- Dim ar, Dict As Object, titleRow As Long, splitCol As Long
- Dim strPath As String, strKey As String, colSize As Long, i As Long
-
- Application.ScreenUpdating = False
-
- titleRow = 1
- splitCol = 5
- 'splitCol = InputBox("请输入 拆分列号", "输入提示:", 3): If Val(splitCol) = 0 Then Exit Sub
- 'titleRow = InputBox("请输入 标题行数", "输入提示:", 6): If Val(titleRow) = 0 Then Exit Sub
-
- strPath = ThisWorkbook.Path & "\分簿"
- If Dir(strPath, vbDirectory) = "" Then MkDir strPath
- strPath = strPath & "\"
-
- Set Dict = CreateObject("Scripting.Dictionary")
- With Worksheets("总表")
- ar = .Range("A1").CurrentRegion
- colSize = UBound(ar, 2)
- For i = titleRow + 1 To UBound(ar)
- strKey = Trim(ar(i, splitCol))
- If Len(strKey) Then
- If Not Dict.Exists(strKey) Then Set Dict(strKey) = .Range("A1").Resize(titleRow, colSize)
- Set Dict(strKey) = Union(Dict(strKey), .Cells(i, 1).Resize(, colSize))
- End If
- Next
- End With
-
- Application.DisplayAlerts = False
- For i = 0 To Dict.Count - 1
- With Workbooks.Add
- With .Worksheets(1)
- .Name = Dict.Keys()(i)
- Dict.Items()(i).Copy .Range("A1")
- .DrawingObjects.Delete
- .Columns.AutoFit
- End With
- .SaveAs strPath & Dict.Keys()(i), 51
- .Close
- End With
- Next
-
- Set Dict = Nothing
-
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- Beep
- End Sub
- Sub test2() '字典定位 数组嵌套
- Dim data, temp() As String, results(), Dict As Object, wks As Worksheet
- Dim i As Long, j As Long, posRow As Long, strPath As String
- Dim titleRow As Long, splitCol As Long
-
- titleRow = 1 '标题所在 行
- splitCol = 5 '拆分依据 列
-
- DoApp False
-
- strPath = ThisWorkbook.Path & Application.PathSeparator & "分簿"
- If Dir(strPath, vbDirectory) = "" Then MkDir strPath
- strPath = strPath & Application.PathSeparator
-
- ' Worksheets("总表").Activate
- Set wks = ActiveSheet
- Set Dict = CreateObject("Scripting.Dictionary")
-
- data = Range("A1").CurrentRegion
- ReDim temp(1 To UBound(data), 1 To UBound(data, 2))
- For j = 1 To UBound(data, 2)
- For i = 1 To titleRow
- temp(i, j) = data(i, j)
- Next
- Next
-
- For i = titleRow + 1 To UBound(data)
- If Not Dict.Exists(data(i, splitCol)) Then Dict(data(i, splitCol)) = Dict.Count + 1
- Next
-
- ReDim results(1 To Dict.Count, 1 To 2)
- For i = 1 To Dict.Count
- results(i, 1) = titleRow
- results(i, 2) = temp
- Next
-
- For i = titleRow + 1 To UBound(data)
- posRow = Dict(data(i, splitCol))
- results(posRow, 1) = results(posRow, 1) + 1
- For j = 1 To UBound(data, 2)
- results(posRow, 2)(results(posRow, 1), j) = data(i, j)
- Next
- Next
-
- For i = 1 To Dict.Count
- wks.Copy
- With ActiveWorkbook
- With .Worksheets(1)
- .Name = results(i, 2)(titleRow + 1, splitCol)
- .Range("A1").Resize(results(i, 1), UBound(data, 2)) = results(i, 2)
- .DrawingObjects.Delete
- .UsedRange.Offset(, UBound(data, 2)).Clear
- .UsedRange.Offset(results(i, 1)).Clear
- End With
- .SaveAs strPath & results(i, 2)(titleRow + 1, splitCol), 51
- .Close
- End With
- Next
-
- Set Dict = Nothing
-
- DoApp
- Beep
- End Sub
- Sub test3()
- Dim splitCol As Long, titleRow As Long
-
- titleRow = 1
- splitCol = 5
- ' splitCol = InputBox("请输入 拆分列号", "输入提示:", 1): If Val(splitCol) = 0 Then Exit Sub
- ' titleRow = InputBox("请输入 标题行数", "输入提示:", 2): If Val(titleRow) = 0 Then Exit Sub
- DoApp False
-
- Dim ar, i As Long, j As Long, rowSize As Long, lastRow As Long
- Dim strPath As String, strName As String, wks As Worksheet
-
- strPath = ThisWorkbook.Path & Application.PathSeparator & "分簿"
- If Dir(strPath, vbDirectory) = "" Then MkDir strPath
- strPath = strPath & Application.PathSeparator
-
- ' Worksheets("总表").Activate
- Set wks = ActiveSheet
- With wks.Range("A1").CurrentRegion
- lastRow = .Rows.Count
- ar = .Resize(lastRow + 1)
- End With
- With CreateObject("Excel.Sheet")
- With .ActiveSheet.Range("A1").Resize(UBound(ar), UBound(ar, 2))
- .Value = ar
- With Intersect(.Offset(0), .Offset(titleRow))
- .Sort .Item(splitCol), xlDescending, , , , , , xlYes
- End With
- ar = .Value
- End With
- .Close
- End With
-
- ' QuickSort ar, titleRow + 1, lastRow, 1, UBound(ar, 2), splitCol
-
- rowSize = titleRow
- For i = titleRow + 1 To lastRow
- rowSize = rowSize + 1
- For j = 1 To UBound(ar, 2)
- ar(rowSize, j) = ar(i, j)
- Next
- If ar(i, splitCol) <> ar(i + 1, splitCol) Then
- strName = ar(i, splitCol)
- wks.Copy
- With ActiveWorkbook
- With .Worksheets(1)
- .Range("A1").Resize(rowSize, 3).NumberFormatLocal = "@"
- .Range("A1").Resize(rowSize, j - 1) = ar
- .UsedRange.Offset(, j - 1).Clear
- .UsedRange.Offset(rowSize).Clear
- .DrawingObjects.Delete
- .Name = strName
- End With
- .SaveAs strPath & strName, 51
- .Close
- End With
- rowSize = titleRow
- End If
- Next
-
- Set wks = 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
- Function QuickSort(ar, u As Long, d As Long, l As Long, r As Long, pos As Long)
- Dim t As Long, b As Long, x As Long, pivot As String, swap
- t = u
- b = d
- pivot = ar((u + d) \ 2, pos)
- While t <= b
- Do While t < d
- If StrComp(ar(t, pos), pivot, vbTextCompare) = -1 Then t = t + 1 Else Exit Do
- Loop
- Do While b > u
- If StrComp(pivot, ar(b, pos), vbTextCompare) = -1 Then b = b - 1 Else Exit Do
- Loop
- If t < b Then
- For x = l To r
- swap = ar(t, x): ar(t, x) = ar(b, x): ar(b, x) = swap
- Next
- t = t + 1: b = b - 1
- Else
- If t = b Then t = t + 1: b = b - 1
- End If
- Wend
- If u < b Then QuickSort ar, u, b, l, r, pos
- If t < d Then QuickSort ar, t, d, l, r, pos
- End Function
复制代码
|
评分
-
2
查看全部评分
-
|