以下需要參考在VBE的工具,References被添加到Microsoft Scripting Runtime。
Sub split_Locations_to_Worksheets()
Dim a As Long, b As Long, c As Long, aLOCs As Variant, aTMP As Variant
Dim dLOCs As New Scripting.Dictionary
appTGGL bTGGL:=False
With Worksheets("Main")
With .Cells(1, 1).CurrentRegion
aLOCs = .Cells.Value2
For a = LBound(aLOCs, 1) + 1 To UBound(aLOCs, 1)
If dLOCs.Exists(aLOCs(a, 2)) Then
ReDim aTMP(1 To UBound(dLOCs.Item(aLOCs(a, 2)), 1) + 1, 1 To UBound(aLOCs, 2))
For b = LBound(dLOCs.Item(aLOCs(a, 2)), 1) To UBound(dLOCs.Item(aLOCs(a, 2)), 1)
For c = LBound(dLOCs.Item(aLOCs(a, 2)), 2) To UBound(dLOCs.Item(aLOCs(a, 2)), 2)
aTMP(b, c) = dLOCs.Item(aLOCs(a, 2))(b, c)
Next c
Next b
For c = LBound(aLOCs, 2) To UBound(aLOCs, 2)
aTMP(b, c) = aLOCs(a, c)
Next c
dLOCs.Item(aLOCs(a, 2)) = aTMP
Else
ReDim aTMP(1 To 2, 1 To UBound(aLOCs, 2))
aTMP(1, 1) = aLOCs(1, 1): aTMP(1, 2) = aLOCs(1, 2)
aTMP(2, 1) = aLOCs(a, 1): aTMP(2, 2) = aLOCs(a, 2)
dLOCs.Add Key:=aLOCs(a, 2), Item:=aTMP
End If
Next a
For Each aLOCs In dLOCs.keys
On Error GoTo bm_Need_WS
With Worksheets("Sheet " & aLOCs)
.Cells.ClearContents
.Cells(1, 1).Resize(UBound(dLOCs.Item(aLOCs), 1), UBound(dLOCs.Item(aLOCs), 2)) = dLOCs.Item(aLOCs)
End With
Next aLOCs
End With
End With
GoTo bm_Safe_Exit
bm_Need_WS:
On Error GoTo 0
With Worksheets.Add(after:=Sheets(Sheets.Count))
.Name = "Sheet " & aLOCs
.Visible = True
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
.FreezePanes = True
.Zoom = 80
End With
End With
Resume
bm_Safe_Exit:
dLOCs.RemoveAll: Set dLOCs = Nothing
appTGGL
End Sub
Public Sub appTGGL(Optional bTGGL As Boolean = True)
Application.ScreenUpdating = bTGGL
Application.EnableEvents = bTGGL
Application.DisplayAlerts = bTGGL
Application.Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual)
End Sub
通過批量加載所有可能值分到一個變量數組和將它們加工成另一個內存對象,這應該相當迅速地處理。雖然這主要是爲了適應你的雙列樣本而設計的,但我留下了循環中的空間來處理大量的列;你只需要調整一些硬編碼值。
這個問題有很多答案。一個[這裏](http://stackoverflow.com/questions/34621490/if-cell-value-is-specific-size-copy-3-cells-in-that-row-to-new-sheet/34621624#34621624 ) –
才發現重複: – Adam
子sbFindDuplicatesInColumn() 昏暗LASTROW只要 昏暗matchFoundIndex只要 昏暗iCntr只要 LASTROW =範圍( 「B65000」)結束(xlUp).Row 對於iCntr = 1至LASTROW If Cells(iCntr,2)<>「」Then matchFoundIndex = WorksheetFunction.Match(Cells(iCntr,2),Range(「B1:B」&lastRow),0) 如果iCntr <> matchFoundIndex Then Cells iCntr,4)=「重複」 End If End If Next 結束小組 – Adam