Skip to main content
added example data
Source Link

Here is data that I would be an example. Each column is in a spreadsheet and the first column starts as 'B'

1-Jan-14    8:43:48 worker1 QQ  SAQ20   Z   R   143 3   0   1   2.72E-02    2.71E-02                
1-Jan-14    8:43:48 worker1 QQ  SAQ20   Z   R   143 4   0   1   2.75E-02    2.73E-02                
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 1   0       6.20E-02    6.19E-02    2.77E-02    2.76E-02    1.19E-02    1.35E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 3   0       0.062127182 6.18E-02    2.77E-02    2.78E-02    0.010853701 1.47E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 4   0       6.20E-02    6.20E-02    2.76E-02    2.75E-02    0.011244671 1.45E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 5   0       6.19E-02    6.20E-02    2.78E-02    2.75E-02    1.29E-02    1.29E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 6   0       6.20E-02    6.20E-02    2.79E-02    2.76E-02    1.20E-02    1.36E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 7   0       6.21E-02    6.20E-02    2.75E-02    2.74E-02    1.19E-02    1.38E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 8   0       6.17E-02    6.17E-02    2.75E-02    2.75E-02    1.34E-02    1.20E-02
2-Jan-14    7:08:39 worker1 QQ  SA3054  Z   R   150 9   0       6.16E-02    6.16E-02    2.73E-02    2.77E-02    1.30E-02    1.23E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 10  0       0.061871287 6.19E-02    2.75E-02    2.74E-02    1.19E-02    1.36E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 11  0       6.17E-02    6.19E-02    2.77E-02    2.76E-02    0.012293416 1.33E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 12  0       0.062024465 0.062002266 2.76E-02    2.75E-02    1.16E-02    1.41E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 13  0       6.19E-02    6.17E-02    2.74E-02    2.76E-02    1.29E-02    1.26E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 14  0       6.19E-02    6.16E-02    2.74E-02    2.78E-02    1.30E-02    1.23E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 15  0       6.18E-02    6.19E-02    2.75E-02    2.74E-02    1.25E-02    1.31E-02
3-Jan-14    7:34:05 worker1 QQ  SAQ20   Z   R   181 1   0       6.21E-02    6.19E-02    2.73E-02    2.71E-02    1.34E-02    0.012262073
3-Jan-14    7:34:05 worker1 QQ  SAQ20   Z   R   181 2   0       6.20E-02    6.22E-02    2.71E-02    2.70E-02    1.32E-02    1.28E-02

Here is data that I would be an example. Each column is in a spreadsheet and the first column starts as 'B'

1-Jan-14    8:43:48 worker1 QQ  SAQ20   Z   R   143 3   0   1   2.72E-02    2.71E-02                
1-Jan-14    8:43:48 worker1 QQ  SAQ20   Z   R   143 4   0   1   2.75E-02    2.73E-02                
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 1   0       6.20E-02    6.19E-02    2.77E-02    2.76E-02    1.19E-02    1.35E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 3   0       0.062127182 6.18E-02    2.77E-02    2.78E-02    0.010853701 1.47E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 4   0       6.20E-02    6.20E-02    2.76E-02    2.75E-02    0.011244671 1.45E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 5   0       6.19E-02    6.20E-02    2.78E-02    2.75E-02    1.29E-02    1.29E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 6   0       6.20E-02    6.20E-02    2.79E-02    2.76E-02    1.20E-02    1.36E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 7   0       6.21E-02    6.20E-02    2.75E-02    2.74E-02    1.19E-02    1.38E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 8   0       6.17E-02    6.17E-02    2.75E-02    2.75E-02    1.34E-02    1.20E-02
2-Jan-14    7:08:39 worker1 QQ  SA3054  Z   R   150 9   0       6.16E-02    6.16E-02    2.73E-02    2.77E-02    1.30E-02    1.23E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 10  0       0.061871287 6.19E-02    2.75E-02    2.74E-02    1.19E-02    1.36E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 11  0       6.17E-02    6.19E-02    2.77E-02    2.76E-02    0.012293416 1.33E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 12  0       0.062024465 0.062002266 2.76E-02    2.75E-02    1.16E-02    1.41E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 13  0       6.19E-02    6.17E-02    2.74E-02    2.76E-02    1.29E-02    1.26E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 14  0       6.19E-02    6.16E-02    2.74E-02    2.78E-02    1.30E-02    1.23E-02
2-Jan-14    7:08:39 worker1 QQ  SAQ20   Z   R   150 15  0       6.18E-02    6.19E-02    2.75E-02    2.74E-02    1.25E-02    1.31E-02
3-Jan-14    7:34:05 worker1 QQ  SAQ20   Z   R   181 1   0       6.21E-02    6.19E-02    2.73E-02    2.71E-02    1.34E-02    0.012262073
3-Jan-14    7:34:05 worker1 QQ  SAQ20   Z   R   181 2   0       6.20E-02    6.22E-02    2.71E-02    2.70E-02    1.32E-02    1.28E-02
edited tags; edited tags
Link
RubberDuck
  • 31.2k
  • 6
  • 74
  • 177
Tweeted twitter.com/#!/StackCodeReview/status/557633633885097984
Source Link

Efficiently create and sort a Collection

The goal of my code is to sort data into two categories. It must use a local copy of the initial data from Collar (Top View).csv. My code creates a Collection of items called Collars using the initial data file, then moves each Collar into its respective category based upon its E dimension. I would like feedback on if I could do this more efficiently and readable, but other feedback is welcomed.

Option Explicit
Option Base 1
Dim CollarCol As New Collection
Dim BatchNum As String

' Calls for creation of a collection of collars and then calls that to be sorted.
Sub SortButton_Click()

    ' Clear current values
    Range("D3:L30").Clear

    ' Create local copy. Cannot open live copies of files.
    FileCopy "O:\IQC_Inspection\EngineeringData\Collar (Top View).csv", _
        ActiveWorkbook.Path + "\" + "Collar (Top View).csv"
        
    ' Get user input for desired batch number
    On Error GoTo ErrorHandler
    BatchNum = InputBox(Prompt:="Enter batch number: ")
    If (BatchNum = 0) Then Exit Sub ' exit for cancel button
    
    Set CollarCol = New Collection
    Call PopulateCollarCol
    Call SortCollarCol
    Exit Sub
    
ErrorHandler:
    MsgBox Err & ": " & Error(Err)
    
End Sub


' Populates the Collection named CollarCol
Private Sub PopulateCollarCol()
    
    Workbooks.Open ActiveWorkbook.Path + "\" + "Collar (Top View).csv"
    Dim Index As Integer, EndIndex As Integer
    Dim NewCollar As Collar
    EndIndex = FindEnd(BatchNum)
   
    For Index = FindStart(BatchNum) To EndIndex
        Set NewCollar = New Collar
        ' If first measure, add to collection
        If (Cells(Index, 11) = 0) Then '
            NewCollar.SetBatchNum (Cells(Index, 9))
            NewCollar.SetSerialNum (Cells(Index, 10))
            NewCollar.SetDimE (Cells(Index, 13))
            CollarCol.Add Item:=NewCollar, key:=CStr(NewCollar.GetSerialNum)
        Else ' see if remeasure is done for DimE
            If (Cells(Index, 15) <> " ") Then
                Dim EditCollar As New Collar
                Set EditCollar = CollarCol.Item(CStr(Cells(Index, 10)))
                ' make sure remeasure is done for DimE
                EditCollar.SetDimE (Cells(Index, 13))
            End If
        End If
    Next Index
    
    Workbooks("Collar (Top View).csv").Close
    
End Sub ' PopulateCollarCol


' Returns the first row of the given string
Function FindStart(ToFind As String) As Integer

    ' find bottom of batch
    Dim Rng As Range
    If Trim(ToFind) <> "" Then
        With Sheets("Collar (Top View)").Range("I2:I30000")
            Set Rng = .Find(What:=ToFind, _
                            after:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            searchdirection:=xlPrevious, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                FindStart = Rng.Row ' found bottom
            Else
                MsgBox "Nothing found"
                Exit Function
            End If
        End With
    End If
    
    ' Loop past remeasures
    Do While (Cells(FindStart, 11) = 1)
        FindStart = FindStart - 1
    Loop
    
    ' Loop while batch number is the same
    Do While (Cells(FindStart - 1, 9) = ToFind)
        If Cells(FindStart - 1, 10) < Cells(FindStart, 10) Or _
        Cells(FindStart, 11) = 1 Then
            FindStart = FindStart - 1
        Else
            Exit Do
        End If
    Loop
    
End Function ' FindStart


Function FindEnd(ToFind As String) As Integer

    ' find bottom of batch
    Dim Rng As Range
    If Trim(ToFind) <> "" Then
        With Sheets("Collar (Top View)").Range("I2:I30000")
            Set Rng = .Find(What:=ToFind, _
                            after:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            searchdirection:=xlPrevious, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                FindEnd = Rng.Row ' found bottom
            Else
                MsgBox "Error finding end of batch."
                Exit Function
            End If
        End With
    End If
    
End Function ' FindEnd


' Takes CollarCol and places each collar into its respective list
Private Sub SortCollarCol()
    
    Dim BlueIndex As Integer, YellowIndex As Integer
    Dim Index As Integer
    Dim CurCollar As New Collar
    BlueIndex = 3
    YellowIndex = 3
    
    For Index = 1 To CollarCol.Count
        Set CurCollar = CollarCol.Item(Index)
        If (CurCollar.GetDimE < 0.062055555) Then
            Cells(BlueIndex, 4) = CurCollar.GetBatchNum
            Cells(BlueIndex, 5) = CurCollar.GetSerialNum
            Cells(BlueIndex, 6) = CurCollar.GetDimE
            BlueIndex = BlueIndex + 1
        Else ' Bucket 2
            Cells(YellowIndex, 9) = CurCollar.GetBatchNum
            Cells(YellowIndex, 10) = CurCollar.GetSerialNum
            Cells(YellowIndex, 11) = CurCollar.GetDimE
            YellowIndex = YellowIndex + 1
        End If
    Next Index
    
End Sub ' SortCollarCol


'Returns boolean true if an object is within a collection
Public Function InCollection(col As Collection, key As String) As Boolean
  Dim var As Variant
  Dim errNumber As Long

  InCollection = False
  Set var = Nothing

  Err.Clear
  On Error Resume Next
    var = col.Item(key)
    errNumber = CLng(Err.Number)
  On Error GoTo 0

  '5 is not in, 0 and 438 represent incollection
  If errNumber = 5 Then ' it is 5 if not in collection
    InCollection = False
  Else
    InCollection = True
  End If

End Function