|
- ' 以下4法,均是平时练习,未设格式
-
- Sub test1() '数组 快排
- Dim data, wks As Worksheet
- Dim i As Long, j As Long, rowSize As Long
- Dim titleRow As Long, splitCol As Long
- titleRow = 3 '标题所在 行
- splitCol = 8 '拆分依据 列
- DoApp False
- Worksheets("总表").Activate
- For Each wks In Worksheets
- If wks.Name <> ActiveSheet.Name Then wks.Delete
- Next
- With Range("A1").CurrentRegion
- data = .Resize(.Rows.Count + 1).Value
- End With
- QuickSort data, titleRow + 1, UBound(data) - 1, 1, UBound(data, 2), splitCol
- rowSize = titleRow
- For i = titleRow + 1 To UBound(data) - 1
- rowSize = rowSize + 1
- For j = 1 To UBound(data, 2)
- data(rowSize, j) = data(i, j)
- Next
- If data(i, splitCol) <> data(i + 1, splitCol) Then
- Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = data(i, splitCol)
- With ActiveSheet
- .Range("A1").Resize(rowSize, j - 1) = data
- .Columns.AutoFit
- End With
- rowSize = titleRow
- End If
- Next
- Worksheets(1).Activate
- DoApp
- Beep
- End Sub
- Sub test2() 'ADO + SQL
- Dim Conn As Object, rs As Object
- Dim strConn As String, SQL As String, str_ As String
- Dim wks As Worksheet, data, titleRow As Long, splitCol As Long
- titleRow = 3 '标题所在 行
- splitCol = 8 '拆分依据 列
- DoApp False
- Worksheets("总表").Activate
- For Each wks In Worksheets
- If wks.Name <> ActiveSheet.Name Then wks.Delete
- Next
- Set Conn = CreateObject("ADODB.Connection")
- Set rs = CreateObject("ADODB.Recordset")
- If Application.Version < 12 Then
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
- With Range("A1").CurrentRegion
- data = .Resize(titleRow).Value
- str_ = .Parent.Name & "$" & Intersect(.Offset(0), .Offset(titleRow - 1)).Address(0, 0)
- End With
- SQL = "SELECT DISTINCT [" & data(titleRow, splitCol) & "] FROM [" & str_ & "]"
- rs.Open SQL, Conn, 1, 3
- SQL = "SELECT * FROM [" & str_ & "] WHERE TRIM([" & data(titleRow, splitCol) & "])='[str_]'"
- While Not rs.EOF
- str_ = rs.Fields(0).Value
- Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = str_
- With ActiveSheet
- .Range("A1").Resize(titleRow, UBound(data, 2)) = data
- .Range("A" & titleRow + 1).CopyFromRecordset Conn.Execute(Replace(SQL, "[str_]", str_))
- .Columns.AutoFit
- End With
- rs.MoveNext
- Wend
- rs.Close
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Worksheets(1).Activate
- DoApp
- Beep
- End Sub
- Sub test3() ' 字典
- Dim dict As Object, wks As Worksheet
- Dim data, i As Long, j As Long, strKey As String
- Dim titleRow As Long, splitCol As Long
- titleRow = 3 '标题所在 行
- splitCol = 8 '拆分依据 列
- DoApp False
- Worksheets("总表").Activate
- For Each wks In Worksheets
- If wks.Name <> ActiveSheet.Name Then wks.Delete
- Next
- Set dict = CreateObject("Scripting.DictionAry")
- With Range("A1").CurrentRegion
- j = .Columns.Count
- data = .Columns(splitCol)
- End With
- For i = titleRow + 1 To UBound(data)
- strKey = data(i, 1)
- If Len(strKey) Then
- If Not dict.Exists(strKey) Then Set dict(strKey) = Range("A1").Resize(titleRow, j)
- Set dict(strKey) = Union(dict(strKey), Range("A" & i).Resize(1, j))
- End If
- Next
- For i = 0 To dict.Count - 1
- With Worksheets.Add(After:=Worksheets(Worksheets.Count))
- .Name = dict.Keys()(i)
- dict.Items()(i).Copy .Range("A1")
- .Columns.AutoFit
- End With
- Next
- Worksheets(1).Activate
- Set dict = Nothing
- DoApp
- Beep
- End Sub
- Sub test4() '字典定位 数组嵌套 测试过30W+数据,速度最快
- Dim data, temp() As String, results(), dict As Object, wks As Worksheet
- Dim i As Long, j As Long, posRow As Long
- Dim titleRow As Long, splitCol As Long
- titleRow = 3 '标题所在 行
- splitCol = 8 '拆分依据 列
- DoApp False
- Worksheets("总表").Activate
- For Each wks In Worksheets
- If wks.Name <> ActiveSheet.Name Then wks.Delete
- Next
- 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
- With Worksheets.Add(After:=Worksheets(Worksheets.Count))
- .Name = results(i, 2)(titleRow + 1, splitCol)
- .Range("A1").Resize(results(i, 1), UBound(data, 2)) = results(i, 2)
- .Columns.AutoFit
- End With
- Next
- Worksheets(1).Activate
- Set dict = Nothing
- DoApp
- Beep
- End Sub
- 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, y As Long, pivot As String, swap
- t = u
- b = d
- pivot = ar((u + d) \ 2, pos)
- While t <= b
- Do
- If StrComp(ar(t, pos), pivot, vbTextCompare) = -1 Then t = t + 1 Else Exit Do
- Loop While t < d
- Do
- If StrComp(pivot, ar(b, pos), vbTextCompare) = -1 Then b = b - 1 Else Exit Do
- Loop While b > u
- 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
- Function DoApp(Optional b As Boolean = True)
- With Application
- .ScreenUpdating = b
- .DisplayAlerts = b
- .Calculation = -b * 30 - 4135
- End With
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|