Results 1 to 8 of 8

VBA: Create unique list based on latest entry in list

Threaded View

  1. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA: Create unique list based on latest entry in list

    welchs101,

    Here's a macro solution. I have attached a modified version of your sample file so you can see how it works.
    Sub GetDataMacro_for_welchs101()
        
        Const LotIDCol As String = "A"      'The column containing LotIDs in the source data
        Const LotYieldCol As String = "B"   'The column containing Yield numbers in the source data
        Const LotDateCol As String = "C"    'The column containing Dates in the source data
        Const StartRow As String = "3"      'The row that the source data starts on (excluding headers)
        
        Const DestCol As String = "F"       'The column that the extracted LotID's will be sent to
        Const DestStartRow As String = "3"  'The starting row for the extracted data
        
        Dim rngLotIDs As Range: Set rngLotIDs = Range(LotIDCol & StartRow, Cells(Rows.Count, LotIDCol).End(xlUp))
        Dim rngDest As Range:   Set rngDest = Range(DestCol & DestStartRow)
        
        Dim LotIDCell As Range
        Dim LotIDFound As Boolean
        Dim arrMax As Long, arrIndex As Long
        Dim LotID() As String, LotYield() As Double, LotDate() As Date
        
        Application.ScreenUpdating = False
        
        If rngDest.Value <> vbNullString Then
            Range(rngDest, Cells(Rows.Count, rngDest.Offset(0, 2).Column).End(xlUp)).ClearContents
        End If
        
        For Each LotIDCell In rngLotIDs
            LotIDFound = False
            For arrIndex = 1 To arrMax
                If LotID(arrIndex) = LotIDCell.Value Then
                    If LotDate(arrIndex) < Range(LotDateCol & LotIDCell.Row).Value Then
                        LotDate(arrIndex) = Range(LotDateCol & LotIDCell.Row).Value
                        LotYield(arrIndex) = Range(LotYieldCol & LotIDCell.Row).Value
                    End If
                    LotIDFound = True
                    Exit For
                End If
            Next
            If LotIDFound = False Then
                arrMax = arrMax + 1
                ReDim Preserve LotID(1 To arrMax)
                ReDim Preserve LotYield(1 To arrMax)
                ReDim Preserve LotDate(1 To arrMax)
                LotID(arrMax) = Range(LotIDCol & LotIDCell.Row).Value
                LotYield(arrMax) = Range(LotYieldCol & LotIDCell.Row).Value
                LotDate(arrMax) = Range(LotDateCol & LotIDCell.Row).Value
            End If
        Next LotIDCell
        
        rngDest.Offset(0, 0).Resize(arrMax, 1).Value = WorksheetFunction.Transpose(LotID)
        rngDest.Offset(0, 1).Resize(arrMax, 1).Value = WorksheetFunction.Transpose(LotYield)
        rngDest.Offset(0, 2).Resize(arrMax, 1).Value = WorksheetFunction.Transpose(LotDate)
        
        Application.ScreenUpdating = True
        
    End Sub


    Hope this helps,
    ~tigeravatar

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1