chuckles1066
Banned
- Joined
- Dec 20, 2004
- Messages
- 372
I have cobbled together a couple of macros very kindly supplied by the geniuses on this forum and have also added in some code generated by the macro recorder.
Consequently, what I have works but I suspect its bloaty.
If anyone's got a spare five minutes, please feel free to critique my macro:
Consequently, what I have works but I suspect its bloaty.
If anyone's got a spare five minutes, please feel free to critique my macro:
Code:
Sub Separate_Controllers()
Dim LastRow As Long, LastCol As Integer, i As Long, iStart As Long, iEnd As Long
Dim ws As Worksheet, r As Range, iCol As Integer, t As Date, Prefix As String
Dim sh As Worksheet, Master As String
Sheets("All Jobs").Select
LR = Range("A" & Rows.Count).End(xlUp).Row
For z = 12 To LR
If Cells(z, 17).Value = "Overdue" Then
Range("A" & z & ":S" & z).Interior.ColorIndex = 3
End If
If Cells(z, 17).Value = "Raised & Completed Today" Then
Range("A" & z & ":S" & z).Interior.ColorIndex = 4
End If
If Cells(z, 17).Value = "In Progress" Then
Range("A" & z & ":S" & z).Interior.ColorIndex = 5
Range("A" & z & ":S" & z).Font.ColorIndex = 2
End If
If Cells(z, 17).Value = "Raised Today" Then
Range("A" & z & ":S" & z).Interior.ColorIndex = 6
End If
If Cells(z, 17).Value = "Outstanding" Then
Range("A" & z & ":S" & z).Interior.ColorIndex = 7
End If
If Cells(z, 17).Value = "Completed Today" Then
Range("A" & z & ":S" & z).Interior.ColorIndex = 8
End If
Next z
Rows("1:10").Delete
Cells.Select
Selection.AutoFilter
Cells.Select
With Selection.Font
.Name = "Trebuchet MS"
.Size = 9
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
End With
On Error Resume Next
Set r = Application.InputBox("Highlight the column you wish to work with", Type:=8)
On Error GoTo 0
If r Is Nothing Then Exit Sub
iCol = r.Column
t = Now
Application.ScreenUpdating = False
With ActiveSheet
Master = .Name
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
.Range(.Cells(2, 1), Cells(LastRow, LastCol)).Sort Key1:=Cells(2, iCol), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
iStart = 2
For i = 2 To LastRow
If .Cells(i, iCol).Value <> .Cells(i + 1, iCol).Value Then
iEnd = i
Sheets.Add after:=Sheets(Sheets.Count)
Set ws = ActiveSheet
On Error Resume Next
ws.Name = .Cells(iStart, iCol).Value
On Error GoTo 0
ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
.Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy Destination:=ws.Range("A2")
iStart = iEnd + 1
End If
Next i
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
Sheets("ref").Select
Rows("1:10").Select
Selection.Copy
Sheets("All Jobs").Select
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Sheets(Array("All Jobs", "Homer", "Marge", "Bart", _
"Lisa", "Maggie", "Smithers")).Select
Sheets("All Jobs").Activate
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Sheets("All Jobs").Select
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:B").Select
Selection.ColumnWidth = 12
Range("A11:S11").Select
Application.CutCopyMode = False
Selection.AutoFilter
Rows("11:11").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Sheets(Array("Homer", "Marge", "Bart", _
"Lisa", "Maggie", "Smithers")).Select
Rows("11:11").Select
ActiveSheet.Paste
Sheets("Homer").Select
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:B").Select
Selection.ColumnWidth = 12
Range("A11:S11").Select
Application.CutCopyMode = False
Selection.AutoFilter
Sheets("Marge").Select
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:B").Select
Selection.ColumnWidth = 12
Range("A11:S11").Select
Application.CutCopyMode = False
Selection.AutoFilter
Sheets("Bart").Select
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:B").Select
Selection.ColumnWidth = 12
Range("A11:S11").Select
Application.CutCopyMode = False
Selection.AutoFilter
Sheets("Lisa").Select
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:B").Select
Selection.ColumnWidth = 12
Range("A11:S11").Select
Application.CutCopyMode = False
Selection.AutoFilter
Sheets("Maggie").Select
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:B").Select
Selection.ColumnWidth = 12
Range("A11:S11").Select
Application.CutCopyMode = False
Selection.AutoFilter
Sheets("Smithers").Select
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:B").Select
Selection.ColumnWidth = 12
Range("A11:S11").Select
Application.CutCopyMode = False
Selection.AutoFilter
Sheets("All Jobs").Select
MsgBox "Completed in " & Format(Now - t, "hh:mm:ss.00"), vbInformation
If MsgBox("Do you wish to save the individual sheets as separate workbooks?", vbYesNo + vbQuestion) = vbYes Then
Prefix = InputBox("Enter a prefix (or leave blank)")
Application.ScreenUpdating = False
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> "ref" And sh.Name <> Master Then
sh.Copy
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Prefix & sh.Name & ".xls"
ActiveWorkbook.Close
End If
Next sh
Application.ScreenUpdating = True
End If
End Sub