我有追嗰個post
冇人答你點解out咗
Sub saveExcels()
Dim wb As Workbook
Dim ws As Worksheet
Dim header As Range
Dim table As Range
Dim maxRow As Integer
Dim maxCol As Integer
Dim code As Range
Dim uniqueCode() As Variant
Dim codeLen As Integer
Dim i As Integer
Dim j As Integer
Dim arr() As Range
Dim count() As Integer
Dim newWB As Workbook
Set wb = ThisWorkbook
Set ws = wb.ActiveSheet
Set header = ws.Range("_header")
Set table = ws.Range("_table")
maxRow = table(999999, 1).End(xlUp).Row - table(1)(1).Row + 1
maxCol = table(1, 9999).End(xlToLeft).Column - table(1)(1).Column + 1
Set code = table.Columns(1).Range(Cells(1, 1), Cells(maxRow, 1))
uniqueCode = getUnique(code)
codeLen = UBound(uniqueCode)
ReDim arr(codeLen, maxRow)
ReDim count(codeLen)
For i = 0 To codeLen
count(i) = 0
Next i
For i = 1 To maxRow
j = Application.Match(code(i), uniqueCode, 0) - 1
Set arr(j, count(j)) = table.Rows(i)
count(j) = count(j) + 1
Next i
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For i = 0 To codeLen
Set newWB = Workbooks.Add
With newWB.Sheets(1)
.Rows(1).Value = header.Value
For j = 0 To count(i) - 1
.Rows(j + 2).Value = arr(i, j).Value
Next j
End With
newWB.SaveAs wb.Path + "\" + CStr(arr(i, 1).Cells(1, 1).Value) + ".xlsx"
newWB.Close
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Function getUnique(inputRange As Range) As Variant()
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
For Each cell In inputRange
dict(cell.Value) = 1
Next cell
getUnique = dict.Keys()
End Function