Guest User

Untitled

a guest
May 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function ColumnLength(ByVal column As String) As Integer                ' Obtains the length of specified column
  2.    Dim row As Integer                                                  ' Hold row position
  3.    row = 2                                                             ' Default to 2nd row (minus header)
  4.    Do While Not Range(column & row) = ""                               ' While first cell of row isn't blank
  5.        row = row + 1                                                   ' Increase row counter
  6.    Loop
  7.     ColumnLength = row - 1                                              ' Return correct column length
  8. End Function
  9.  
  10. Function Position(ByVal column As String) As Integer                    ' Obtains position of specified column
  11.    Dim col As Integer                                                  ' Hold column position
  12.    col = 1                                                             ' Default to 1st column
  13.    Do While Not Cells(1, col) = column                                 ' While the current position doesn't match arg
  14.        If col = 255 Then                                               ' Max boundary of 255 columns
  15.            Position = -1                                               ' Return error code
  16.        End If
  17.         col = col + 1                                                   ' Increase column
  18.    Loop
  19.     Position = col                                                      ' Return position that matches column arg
  20. End Function
  21.  
  22. Function FurthestHeader() As Integer                                    ' Obtains furthest header in DAV file
  23.    Dim col As Integer                                                  ' Hold column position
  24.    col = 1                                                             ' Default to 1st column
  25.    Do While Not Cells(1, col) = ""                                     ' While current column is not blank
  26.        col = col + 1                                                   ' Increase column
  27.    Loop
  28.     FurthestHeader = col - 1                                            ' Return last column position
  29. End Function
  30.  
  31. Function ColumnLetter(ByVal column As String) As String                 ' Obtain column header letter
  32.    ColumnLetter = Split(Cells(1, Position(column)).Address, "$")(1)    ' Left side from the dollar sign converted from column address
  33. End Function
  34.  
  35. Sub LithoMonster()
  36.     Dim sampleFromText, sampleToText, lithoFromText, lithoToText, lithoCodeText As String               ' Text to search for in column header
  37.    sampleFromText = "From"                                                                             ' Sample from
  38.    sampleToText = "To"                                                                                 ' Sample to
  39.    lithoFromText = "Lf"                                                                                ' Litho from
  40.    lithoToText = "Lt"                                                                                  ' Litho to
  41.    lithoCodeText = "Litho"                                                                             ' Litho code
  42.  
  43.     Dim sampleFromLetter, sampleToLetter, lithoFromLetter, lithoToLetter As String                      ' Obtain column header letters
  44.    sampleFromLetter = ColumnLetter(sampleFromText)                                                     ' Sample from
  45.    sampleToLetter = ColumnLetter(sampleToText)                                                         ' Sample to
  46.    lithoFromLetter = ColumnLetter(lithoFromText)                                                       ' Litho from
  47.    lithoToLetter = ColumnLetter(lithoToText)                                                           ' Litho to
  48.    lithoCodeLetter = ColumnLetter(lithoCodeText)                                                       ' Litho code
  49.  
  50.     Dim sampleFromCell, sampleToCell, lithoFromCell, lithoToCell As Double                              ' Hold sample and litho from to's
  51.    Dim SL As String                                                                                    ' Hold litho
  52.    Dim count, highest As Integer                                                                       ' Multiple litho
  53.    highest = count = 0                                                                                 ' Default to 0
  54.    For dav = 2 To ColumnLength("A")                                                                    ' For each sample
  55.        For litho = 2 To ColumnLength(lithoFromLetter)                                                  ' For each litho
  56.            If Not Range(lithoToLetter & litho) = "EOH" And Not Cells(dav, Position("To")) = "" Then    ' Ignores EOH and DUP/HLF/STD/BLKs
  57.                sampleFromCell = Range(sampleFromLetter & dav)                                          ' Obtains sample from as a double
  58.                sampleToCell = Range(sampleToLetter & dav)                                              ' Obtains sample to as a double
  59.                lithoFromCell = Range(lithoFromLetter & litho)                                          ' Obtains litho from as a double
  60.                lithoToCell = Range(lithoToLetter & litho)                                              ' Obtains litho to as a double
  61.                    
  62.                 ' If litho from <= sample from and sample from < litho to
  63.                ' or Litho from < sample to and sample to <= litho to
  64.                ' or litho from <= sample from and sample to <= litho to
  65.                If (lithoFromCell <= sampleFromCell) And (sampleFromCell < lithoToCell) _
  66.                 Or (lithoFromCell < sampleToCell) And (sampleToCell <= lithoToCell) _
  67.                 Or (lithoFromCell <= sampleFromCell) And (sampleToCell <= lithoToCell) _
  68.                 Then
  69.                     SL = Range(lithoCodeLetter & litho)                                                 ' Save litho
  70.                    Cells(dav, Position("Litho") + 2 + count) = SL                                      ' Current litho slot filled
  71.                    count = count + 1                                                                   ' Increase counter for next possible litho
  72.                    If count > highest Then                                                             ' Check if highest count
  73.                        highest = count                                                                 ' Set the highest count
  74.                    End If
  75.                 End If
  76.             End If
  77.         Next litho
  78.         count = 0                                                                                       ' Reset litho counter for next pass-around
  79.    Next dav
  80.     For i = Position("Litho") + 2 To Position("Litho") + 2 + highest - 1                                ' Count number of lithos in a row
  81.        Cells(1, i) = "SL (" & i - 16 & ")"                                                             ' Place adaquete amount of headers
  82.    Next i
  83. End Sub
Add Comment
Please, Sign In to add comment