1st, ensure the Sheet exists with the correct name (customise the GetIDs SUB in the Set-up part)
2nd, I've made it more parameterised so you can 'Set-up' how you need...
As before...
* Open VB (Alt+F11)
* In the Project Explorer, right click the project & add a Class
* Name the new Class MyDataRow
* Add the following code to the class
Public ID As String
Public Stage As String
Public Ver As Long
Public EntryDate As String
* In the Project Explorer, right click the project & add a Module
* Add the following 2 (UPDATED) functions to that module
Private Function BuildDataRow(ID As String, _
Stage As String, _
Ver As Single, _
EntryDate As String) As MyDataRow
Dim mdr As New MyDataRow
mdr.ID = ID
mdr.Stage = Stage
mdr.Ver = Ver
mdr.EntryDate = EntryDate
Set BuildDataRow = mdr
End Function
Public Sub GetIDs()
Dim rResults As Range
Dim rID As Range
Dim rStage As Range
Dim rVer As Range
Dim rDate As Range
Dim wkSheet As Worksheet
Dim OutputSheet As Worksheet
Dim dic, key, sKey As String
Set dic = CreateObject("Scripting.Dictionary")
'******* Set-up ### Customise here ### ********
'Set Results sheet name
Set OutputSheet = Workbooks("Output.xls").Sheets("Results")
'Set source data sheet name
Set wkSheet = ThisWorkbook.Sheets("Sheet1")
'Set start points
Set rID = wkSheet.Range("A2")
Set rStage = wkSheet.Range("B2")
Set rVer = wkSheet.Range("C2")
Set rDate = wkSheet.Range("D2")
'Set rResults to start output at A1
Set rResults = OutputSheet.Range("A1")
'*******Read table in wkSheet********
'loop through items
While rID.Text <> ""
'Build 2 part 'ID.Stage' key for dic object
sKey = rID.Text & "|" & rStage.Text
'Test see if ID.Stage key has already been seen
If dic.Exists(sKey) Then
'YES - See if this ID.Stage is highest Ver.
If dic(sKey).Ver < rVer.Value Then
Set dic(sKey) = BuildDataRow(rID.Text, _
rStage.Text, _
rVer.Value, _
rDate.Text)
End If
Else
'NO - Store this DataRow as highest ID.Stage
Set dic(sKey) = BuildDataRow(rID.Text, _
rStage.Text, _
rVer.Value, _
rDate.Text)
End If
Set rID = rID.Offset(1)
Set rStage = rStage.Offset(1)
Set rVer = rVer.Offset(1)
Set rDate = rDate.Offset(1)
Wend
'*******List Results********
'Clear output sheet
OutputSheet.Cells.Clear
'Set up titles
rResults.Value = "ID"
rResults.Offset(0, 1).Value = "Stage"
rResults.Offset(0, 2).Value = "Version"
rResults.Offset(0, 3).Value = "Date"
'Set rResults to start output next row
Set rResults = rResults.Offset(1, 0)
For Each key In dic.Keys
'Put ID in cell
rResults.Value = dic(key).ID
'Put Stage in cell.offset 1 col
rResults.Offset(0, 1).Value = dic(key).Stage
'Put Ver in cell.offset 2 cols
rResults.Offset(0, 2).Value = dic(key).Ver
'Put Date in cell.offset 3 cols
rResults.Offset(0, 3).Value = dic(key).EntryDate
'refernece next cell (1 row down)
Set rResults = rResults.Offset(1, 0)
Next
'Show results
OutputSheet.Activate
End Sub
* Now its done - Run macro GetIDs
Let me know how you get on.
Regards - Steve