Advertisement
tic

Untitled

tic
Oct 16th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Presale()
  2.  
  3.     On Error GoTo ErrorHandler
  4.  
  5.     Dim app As New Excel.Application
  6.     Dim wrkbkExtract As Workbook
  7.     Dim wrkSheetMaster As Worksheet, wrkSheetExtract As Worksheet
  8.     Dim arrSource() As Variant, arrDestination() As Variant
  9.     Dim objFSO As Object, objFolder As Object
  10.    
  11.     app.Visible = False
  12.    
  13.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  14.     Set objFolder = objFSO.GetFolder(ThisWorkbook.Path)
  15.    
  16.     For Each objFile In objFolder.Files
  17.         If (Right(objFile.Name, 4) = ".xls" And Left(objFile.Name, 4) = "NYC_" And objFile.DateCreated > newestDateCreated) Then
  18.             newestFilePath = objFile
  19.             newestFilePathName = objFile.Name
  20.             newestDateCreated = objFile.DateCreated
  21.         End If
  22.     Next
  23.    
  24.     If (newestFilePath = Empty) Then
  25.         Err.Raise (0)
  26.     End If
  27.    
  28.     Set wrkbkExtract = app.Workbooks.Open(newestFilePath)
  29.     Set wrkSheetExtract = wrkbkExtract.Sheets(Left(newestFilePathName, Len(newestFilePathName) - 4))
  30.     Set wrkSheetMaster = ThisWorkbook.Sheets("Master")
  31.  
  32.     lastRow = wrkSheetExtract.UsedRange.Rows.Count
  33.    
  34.     ReDim arrSource(1 To lastRow - 2, 1 To 89)
  35.     ReDim arrDestination(1 To lastRow - 2, 1 To 28)
  36.    
  37.     arrSource = wrkSheetExtract.Range("A3:CK" & lastRow)
  38.  
  39.     aSource = Array("AP", "AQ", "I", "B", "Y", "Z", "H", "CI", "CJ", "CH", "AE", "AI", "AL", "AM")
  40.     aDestination = Array("A", "B", "C", "D", "E", "F", "G", "T", "U", "V", "W", "AB", "Z", "AA")
  41.    
  42.     If (UBound(aSource) <> UBound(aDestination)) Then
  43.         Err.Raise (0)
  44.     End If
  45.    
  46.     lotColumn = ColumnToInteger("AQ")
  47.     k = LBound(arrDestination, 1)
  48.    
  49.     Dim letterSource As String, letterDestination As String
  50.     For i = LBound(arrSource, 1) To UBound(arrSource, 1)
  51.    
  52.         lot1 = arrSource(i, lotColumn)
  53.        
  54.         lotExists = False
  55.         For j = i + 1 To UBound(arrSource, 1)
  56.             lot2 = arrSource(j, lotColumn)
  57.             If (lot1 = lot2) Then
  58.                 lotExists = True
  59.                 Exit For
  60.             End If
  61.         Next j
  62.        
  63.         If Not lotExists Then
  64.    
  65.             For j = LBound(aSource) To UBound(aSource)
  66.                 letterSource = aSource(j)
  67.                 letterDestination = aDestination(j)
  68.                 thisSource = ColumnToInteger(letterSource)
  69.                 thisDestination = ColumnToInteger(letterDestination)
  70.  
  71.                 If (letterDestination = "U") Then
  72.                     arrDestination(k, thisDestination) = Trim(arrSource(i, thisSource))
  73.                 Else
  74.                     arrDestination(k, thisDestination) = arrSource(i, thisSource)
  75.                 End If
  76.             Next j
  77.        
  78.             k = k + 1
  79.         End If
  80.     Next i
  81.    
  82.     wrkSheetMaster.Range("A2").Resize(UBound(arrDestination, 1), UBound(arrDestination, 2)).Value = arrDestination
  83.    
  84. ErrorHandler:
  85.    
  86.     If Not (wrkbkExtract Is Nothing) Then
  87.         wrkbkExtract.Close SaveChanges:=False
  88.     End If
  89.     Set wrkSheetMaster = Nothing
  90.     Set wrkSheetExtract = Nothing
  91.     Set wrkbkExtract = Nothing
  92.     Set app = Nothing
  93.     Set objFSO = Nothing
  94.     Set objFolder = Nothing
  95.  
  96. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement