I'm using excel 2003.
I have a worksheet full of maintenance data. Each row on this sheet has a
machine number, date, repair information, etc.
I'm trying to make a marco to search thur the list for a specific machine
number copy all the data from that row and paste it on a different sheet.
All of the entries for that machine number would then be copied to that new
sheet.
Does anyone know how to do this?
There are quite a few different ways of accomplishing this. Some use
AdvanceFilters, etc, etc. I use a Collection object in conjunction
with an AutoFilter.
The below sub is called like this. Simply pass the column Letter in
that you want to base the split on. In this case, column A.
Sub tester()
ReportSplit "A"
End Sub
This is the workhorse sub that performs the split.
Sub ReportSplit(colLetter As String)
Dim shSource As Worksheet, shTarget As Worksheet
Dim LastCol As Integer
Dim rgSource As Range, rgUniques As Range, cl As Range
Dim BotRow As Long
Dim Uniques As Collection, Unique
Set Uniques = New Collection
Set shSource = ActiveWorkbook.ActiveSheet
LastCol = Cells.Find(what:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
BotRow = Cells.Find(what:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
With shSource
Set rgSource = .Range(Cells(1, 1), _
Cells(BotRow, LastCol))
Set rgUniques = .Range(Cells(2, colLetter), _
Cells(BotRow, colLetter))
End With
On Error Resume Next
For Each cl In rgUniques
Uniques.Add cl.Value, CStr(cl.Value)
Next cl
On Error GoTo 0
Application.ScreenUpdating = False
For Each Unique In Uniques
Set shTarget = Worksheets.Add(, ActiveSheet)
shTarget.Name = Unique
With rgSource
.Cells(1, colLetter).AutoFilter 1, Unique
.Copy shTarget.Range("A1")
End With
shSource.AutoFilterMode = False
Next Unique
Application.Goto shSource.Range("A1")
Application.ScreenUpdating = True
End Sub