Lowest and Highest Date based on value in another column

Steve1977

New Member
Joined
May 16, 2019
Messages
33
I'm revisiting my file and wondered if it's possible to tweak and to apply a minimum and maximum date. Basically, this is my data


PARTNO1HyundaiAccent01/01/9431/12/99
PARTNO1Hyundaii3001/01/9831/12/16
PARTNO1Hyundaii4001/01/9931/12/17
PARTNO2HyundaiAccent01/01/9431/12/99
PARTNO2SubaruImpreza01/01/9331/12/98
PARTNO2ToyotaCelica01/01/9531/12/01
PARTNO3ToyotaCelica01/08/9431/05/02
PARTNO3ToyotaMR201/01/91

<tbody>
</tbody>


My original code which concentrates on Columns A, B and C ensures only one unique part number per line.

Code:
'Removes second column
    Columns("B:B").Select
    Selection.Delete Shift:=xlToLeft
 
' Removes Duplicates
    Columns("A:C").Select
    ActiveSheet.Range("$A$1:$C$1000000").RemoveDuplicates Columns:=Array(1, 3), _
        Header:=xlNo
 
' Removes Brackets
    Cells.Replace What:=" (*)", Replacement:="", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
 
 
 
Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant, K As Variant
  
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Dic.Exists(Cl.Value) Then Dic.Add Cl.Value, CreateObject("scripting.dictionary")
         If Not Dic(Cl.Value).Exists(Cl.Offset(, 1).Value) Then
            Dic(Cl.Value).Add (Cl.Offset(, 1).Value), Cl.Offset(, 2).Value
         Else
            Dic(Cl.Value)(Cl.Offset(, 1).Value) = Dic(Cl.Value)(Cl.Offset(, 1).Value) & ", " & Cl.Offset(, 2).Value
         End If
      Next Cl
   End With
   With Sheets("Sheet2")
            For Each Ky In Dic.Keys
         With .Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Value = Ky
            For Each K In Dic(Ky)
               .Offset(, 1).Value = .Offset(, 1).Value & ". " & K & " " & Dic(Ky)(K)
            Next K
            .Offset(, 1).Value = Replace(.Offset(, 1).Value, ". ", "", 1, 1)
         End With
      Next Ky
   End With
End Sub


But how could I get it to understand the value in Column B to ensure it shows the lowest and highest date? So the output would be as follows:

PARTNO1Hyundai Accent, i30, i40 01/94>12/17
PARTNO2Hyundai Accent 01/94>12/99. Subaru Impreza 01/93>12/98
PARTNO2Toyota Celica 08/94>05/02, MR2 01/91>

<tbody>
</tbody>


Any help would be very much appreciated.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about
Code:
Sub Steve1977()
   Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant, K As Variant, Tmp As Variant
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("list")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Dic.Exists(Cl.Value) Then Dic.Add Cl.Value, CreateObject("scripting.dictionary")
         If Not Dic(Cl.Value).Exists(Cl.Offset(, 1).Value) Then
            Dic(Cl.Value).Add (Cl.Offset(, 1).Value), Array(Cl.Offset(, 2).Value, Cl.Offset(, 3).Value, Cl.Offset(, 4).Value)
         Else
            Tmp = Dic(Cl.Value)(Cl.Offset(, 1).Value)
            Tmp(0) = Tmp(0) & ", " & Cl.Offset(, 2).Value
            If Tmp(1) > Cl.Offset(, 3) Then Tmp(1) = Cl.Offset(, 3).Value
            If Tmp(2) < Cl.Offset(, 4) Then Tmp(2) = Cl.Offset(, 4).Value
            Dic(Cl.Value)(Cl.Offset(, 1).Value) = Tmp
         End If
      Next Cl
   End With
   With Sheets("Sheet3")
      For Each Ky In Dic.Keys
         With .Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Value = Ky
            For Each K In Dic(Ky)
               .Offset(, 1).Value = .Offset(, 1).Value & ". " & K & " " & Dic(Ky)(K)(0) & " " & Format(Dic(Ky)(K)(1), "mm/yy") & ">" & Format(Dic(Ky)(K)(2), "mm/yy")
            Next K
            .Offset(, 1).Value = Replace(.Offset(, 1).Value, ". ", "", 1, 1)
         End With
      Next Ky
   End With
End Sub
 
Upvote 0
Absolute legend! This works superbly, thank you. Will try and dechiper the code to get a better understanding of it :)
 
Upvote 0
Actually, just one little question if possible...how would I put the actual dates in a seperate (third) column?
 
Upvote 0
How would you like it to look?
 
Upvote 0
Thank you for the quick reply :)

Basically like this:

(Column A)(Column B)(Column C)
PARTNO1Hyundai Accent, i30, i4001/94>12/17

<tbody>
</tbody>

Actually, thinking out loud...this wouldn't be possible when having two or more different Manufacturers in Column B as I want the date specific to the Manufacturer.

I suppose if the date is in a different column, then a different Manufacturer would have to be on the next line down to accomodate the date.
If you could advise how the date could go into Column C and I'll see if I can adapt the previous code you kindly posted on the other thread which put different manufacturers on each line.
 
Upvote 0
How about
Code:
Sub Steve1977()
   Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant, K As Variant, Tmp As Variant
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("list")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Dic.Exists(Cl.Value) Then Dic.Add Cl.Value, CreateObject("scripting.dictionary")
         If Not Dic(Cl.Value).Exists(Cl.Offset(, 1).Value) Then
            Dic(Cl.Value).Add (Cl.Offset(, 1).Value), Array(Cl.Offset(, 2).Value, Cl.Offset(, 3).Value, Cl.Offset(, 4).Value)
         Else
            Tmp = Dic(Cl.Value)(Cl.Offset(, 1).Value)
            Tmp(0) = Tmp(0) & ", " & Cl.Offset(, 2).Value
            If Tmp(1) > Cl.Offset(, 3) Then Tmp(1) = Cl.Offset(, 3).Value
            If Tmp(2) < Cl.Offset(, 4) Then Tmp(2) = Cl.Offset(, 4).Value
            Dic(Cl.Value)(Cl.Offset(, 1).Value) = Tmp
         End If
      Next Cl
   End With
   With Sheets("Sheet3")
      For Each Ky In Dic.Keys
         With .Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Value = Ky
            For Each K In Dic(Ky)
               .Offset(, 1).Value = .Offset(, 1).Value & ". " & K & " " & Dic(Ky)(K)(0)
               .Offset(, 2).Value = .Offset(, 2).Value & " " & Format(Dic(Ky)(K)(1), "mm/yy") & ">" & Format(Dic(Ky)(K)(2), "mm/yy")
            Next K
            .Offset(, 1).Value = Replace(.Offset(, 1).Value, ". ", "", 1, 1)
            .Offset(, 2).Value = Replace(.Offset(, 2).Value, " ", "", 1, 1)
         End With
      Next Ky
   End With
End Sub
 
Upvote 0
Thank you Fluff :)

It's superb in that it now puts it into a seperate column but the output when there's more than one manufacturer is as follows:

PARTNO2Hyundai Accent. Subaru Impreza. Toyota Celica01/94>12/99 01/93>12/98 01/95>12/01

<tbody>
</tbody>


So what I'm going to do is adapt your code where it put manufacturers on different lines and then include the bit which will put the date in a seperate column.
Doing it this way will keep me learning - hopefully! lol
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top