Sub test()
Dim myList, a, e, i As Long, temp As String, txt As String, dic As Object
Dim wf As WorksheetFunction, ap As Application, flg As Boolean
Set wf = WorksheetFunction: Set ap = Application
myList = Array("Car*", "Date*", "Rent*", "Return*", "Mileage*", "Color*")
With Sheets("sheet1").Cells(1).CurrentRegion
a = Filter(wf.IfError(ap.Match(myList, .Rows(1), 0), False), False, 0)
If UBound(a) <> UBound(myList) Then
MsgBox "Something wrong in Header", vbCritical
Exit Sub
End If
a = ap.Index(.Value, Evaluate("row(1:" & .Rows.Count & ")"), a)
Set dic = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(a, 1)
temp = Join(Array(Join(Array(a(i, 2), "Date + Rent & " & _
"Return Days : " & a(i, 3) & a(i, 4)))), vbCrLf)
txt = Join(Array(a(i, 2), "Color", a(i, 6)))
If Not dic.exists(a(i, 1)) Then
dic(a(i, 1)) = Array(temp, a(i, 2), a(i, 5), txt)
Else
flg = a(i, 5) < dic(a(i, 1))(2)
dic(a(i, 1)) = Array(Join(Array(dic(a(i, 1))(0), temp), vbCrLf), _
IIf(flg, a(i, 2), dic(a(i, 1))(1)), IIf(flg, a(i, 5), dic(a(i, 1))(2)), _
Join(Array(dic(a(i, 1))(3), txt), vbCrLf))
End If
Next
For Each e In dic
dic(e) = Join(Array("Car Make & Model """ & e & """", dic(e)(0), _
"Lowest Mileage is: " & dic(e)(2) & " on " & dic(e)(1), dic(e)(3)), vbCrLf)
Next
Open ThisWorkbook.Path & "/Answer.txt" For Output As #1
Print #1, Join(Array("AUTO", Join(dic.items, vbCrLf)), vbCrLf);
Close #1
End With
End Sub
Bookmarks