You are on page 1of 6

Public CalcState As Long

Public EventState As Boolean


Public PageBreakState As Boolean
Public sThisFile As String, sThisSheet As String, sSiteHandler As String, CellValue
As String, sPathFile As String, sChave As String
Public iThisRow As Long
Public wbThisWorkbook As Workbook, wbEngenharia As Workbook, wbMaster As Workbook,
wbReportSh As Workbook, wbRolloutTim As Workbook
Public cReportSh As Collection, cEngenharia As Collection, cRolloutTim As
Collection, cMaster As Collection

Sub Unificar_Tabelas()
sPathFile = Application.ActiveWorkbook.Path + "\Input\"
Call OptimizeCode_Begin

sThisSheet = "Rollout"
Set wbThisWorkbook = ThisWorkbook

Call FillCollections

iThisRow = 8
sSiteHandler = wbThisWorkbook.Sheets(sThisSheet).Range("E" & iThisRow).Value

Do Until sSiteHandler <> ""


iThisRow = iThisRow + 1
sSiteHandler = wbThisWorkbook.Sheets(sThisSheet).Range("E" &
iThisRow).Value
Loop

Do Until sSiteHandler = ""

Call FillWithReportSH
Call FillWithEngenharia
Call FillWithRolloutTim
Call FillWithMasterPontoA
Call FillWithMasterPontoB

iThisRow = iThisRow + 1
sSiteHandler = wbThisWorkbook.Sheets(sThisSheet).Range("E" &
iThisRow).Value
Loop

wbReportSh.Close
wbEngenharia.Close
wbRolloutTim.Close
wbMaster.Close

Call OptimizeCode_End
End Sub

Sub OptimizeCode_Begin()
Application.ScreenUpdating = False
EventState = Application.EnableEvents
CalcState = Application.Calculation
PageBreakState = ActiveSheet.DisplayPageBreaks
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
ActiveSheet.DisplayPageBreaks = False
End Sub
Sub OptimizeCode_End()
ActiveSheet.DisplayPageBreaks = PageBreakState
Application.Calculation = CalcState
Application.EnableEvents = EventState
Application.ScreenUpdating = True
End Sub

Sub FillCollections()

Dim FilePath As String

Set wbReportSh = Workbooks.Open(sPathFile & "Report SH.xlsx")


Set cReportSh = GetCollection(wbReportSh, "DRT", "A", 7)

Set wbEngenharia = Workbooks.Open(sPathFile & "Engenharia.xlsm")


FilePath = sPathFile & "Engenharia_new.xlsx"
Call SaveCompatibilityMode(wbEngenharia, FilePath)
Set cEngenharia = GetCollection(wbEngenharia, "List", "C", 31)

Set wbRolloutTim = Workbooks.Open(sPathFile & "Rollout TIM.xlsx")


Set cRolloutTim = GetCollection(wbRolloutTim, "TEMPLATE 2018", "B", 3)

Set wbMaster = Workbooks.Open(sPathFile & "MASTER.xlsx")


Set cMaster = GetCollection(wbMaster, "ENTREGUE", "B", 2)

End Sub

Function GetCollection(oWorkBook As Workbook, sSheetName As String, sColumn As


String, iRow As Long) As Collection

Dim cCollection As Collection


Set cCollection = New Collection
Set GetCollection = cCollection

CellValue = oWorkBook.Sheets(sSheetName).Range(sColumn & iRow).Value

Do Until CellValue = ""

If (Not IsError(CellValue)) And (Not Contains(cCollection, CellValue)) Then


cCollection.Add iRow, Trim(CellValue)
End If

iRow = iRow + 1
oCellValue = oWorkBook.Sheets(sSheetName).Range(sColumn & iRow).Value

If (Not IsError(oCellValue)) Then


CellValue = oCellValue
End If
Loop

iQuantity = cCollection.Count

End Function

Public Function Contains(col As Collection, key As Variant) As Boolean


On Error Resume Next
col (key) ' Just try it. If it fails, Err.Number will be nonzero.
Contains = (Err.Number = 0)
Err.Clear
End Function

Sub SaveCompatibilityMode(oWorkBook As Workbook, FilePath As String)

Application.DisplayAlerts = False

With oWorkBook
.CheckCompatibility = False
.SaveAs FilePath, FileFormat:=51
.Close
End With

Application.DisplayAlerts = True

Set oWorkBook = Workbooks.Open(FilePath)

End Sub

Sub FillWithReportSH()

Dim iRow As Long

If (Not Contains(cReportSh, sSiteHandler)) Then


Exit Sub
End If

iRow = cReportSh.Item(sSiteHandler)
Call FillRowWithReportSH(iRow, "DRT")

End Sub

Sub FillRowWithReportSH(iRow As Long, sSheetName As String)

oCell = wbReportSh.Sheets(sSheetName).Range("J" & iRow).Value


If (ValidateCell(oCell)) Then wbThisWorkbook.Sheets(sThisSheet).Range("BR" &
iThisRow).Value = oCell

oCell = wbReportSh.Sheets(sSheetName).Range("L" & iRow).Value


If (ValidateCell(oCell)) Then wbThisWorkbook.Sheets(sThisSheet).Range("BY" &
iThisRow).Value = oCell

oCell = wbReportSh.Sheets(sSheetName).Range("AB" & iRow).Value


If (ValidateCell(oCell)) Then
wbThisWorkbook.Sheets(sThisSheet).Range("HV" & iThisRow).Value = oCell
wbThisWorkbook.Sheets(sThisSheet).Range("HY" & iThisRow).Value = oCell
wbThisWorkbook.Sheets(sThisSheet).Range("IB" & iThisRow).Value = oCell
End If

oCell = wbReportSh.Sheets(sSheetName).Range("AD" & iRow).Value


If (ValidateCell(oCell)) Then
wbThisWorkbook.Sheets(sThisSheet).Range("IE" & iThisRow).Value = oCell
wbThisWorkbook.Sheets(sThisSheet).Range("IH" & iThisRow).Value = oCell
End If

oCell = wbReportSh.Sheets(sSheetName).Range("AE" & iRow).Value


If (ValidateCell(oCell)) Then wbThisWorkbook.Sheets(sThisSheet).Range("IK" &
iThisRow).Value = oCell
oCell = wbReportSh.Sheets(sSheetName).Range("AJ" & iRow).Value
If (ValidateCell(oCell)) Then wbThisWorkbook.Sheets(sThisSheet).Range("IN" &
iThisRow).Value = oCell

End Sub

Public Function ValidateCell(oCell) As Boolean

ValidateCell = (oCell <> -1) And (Not IsError(oCell))

End Function

Sub FillWithEngenharia()

Dim iRow As Long

If (Not Contains(cEngenharia, sSiteHandler)) Then


Exit Sub
End If

iRow = cEngenharia.Item(sSiteHandler)
Call FillRowWithEngenharia(iRow, "List")

End Sub

Sub FillRowWithEngenharia(iRow As Long, sSheetName As String)

wbThisWorkbook.Sheets(sThisSheet).Range("AX" & iThisRow).Value =


wbEngenharia.Sheets(sSheetName).Range("AE" & iRow).Value '61.06.Requirement
Executed (CAMPO DATA)
wbThisWorkbook.Sheets(sThisSheet).Range("BA" & iThisRow).Value =
wbEngenharia.Sheets(sSheetName).Range("AF" & iRow).Value '61.07.Requirement
Approved
wbThisWorkbook.Sheets(sThisSheet).Range("BN" & iThisRow).Value =
wbEngenharia.Sheets(sSheetName).Range("W" & iRow).Value '0.1.STEP (VIR O CAMPO
TEXTO)
wbThisWorkbook.Sheets(sThisSheet).Range("BS" & iThisRow).Value =
wbEngenharia.Sheets(sSheetName).Range("Q" & iRow).Value '61.14.LOS Planned
wbThisWorkbook.Sheets(sThisSheet).Range("BZ" & iThisRow).Value =
wbEngenharia.Sheets(sSheetName).Range("AJ" & iRow).Value '60.01.Link Design
Executed (CAMPO DATA)
wbThisWorkbook.Sheets(sThisSheet).Range("CG" & iThisRow).Value =
wbEngenharia.Sheets(sSheetName).Range("R" & iRow).Value '08.01.TSSR Planned (CAMPO
DATA)
wbThisWorkbook.Sheets(sThisSheet).Range("CL" & iThisRow).Value =
wbEngenharia.Sheets(sSheetName).Range("R" & iRow).Value '08.01.TSSR Planned (CAMPO
DATA)

End Sub

Sub FillWithRolloutTim()

Dim iRow As Long

sChave = wbThisWorkbook.Sheets(sThisSheet).Range("F" & iThisRow).Value

If (Not Contains(cRolloutTim, sChave)) Then


Exit Sub
End If
iRow = cRolloutTim.Item(sChave)
Call FillRowWithRolloutTim(iRow, "TEMPLATE 2018")

End Sub

Sub FillRowWithRolloutTim(iRow As Long, sSheetName As String)

wbThisWorkbook.Sheets(sThisSheet).Range("V" & iThisRow).Value =


wbRolloutTim.Sheets(sSheetName).Range("AB" & iRow).Value 'DETENTOR A
wbThisWorkbook.Sheets(sThisSheet).Range("X" & iThisRow).Value =
wbRolloutTim.Sheets(sSheetName).Range("AC" & iRow).Value 'DETENTOR B
wbThisWorkbook.Sheets(sThisSheet).Range("AA" & iThisRow).Value =
wbRolloutTim.Sheets(sSheetName).Range("M" & iRow).Value 'CLASSIFICAÇÃO PONTA A
wbThisWorkbook.Sheets(sThisSheet).Range("AC" & iThisRow).Value =
wbRolloutTim.Sheets(sSheetName).Range("R" & iRow).Value 'CLASSIFICAÇÃO PONTA B
wbThisWorkbook.Sheets(sThisSheet).Range("AO" & iThisRow).Value =
wbRolloutTim.Sheets(sSheetName).Range("I" & iRow).Value 'Detalhamento: Projeto
Associado

End Sub

Sub FillWithMasterPontoA()

Dim iRow As Long

sPontoA = Trim(wbThisWorkbook.Sheets(sThisSheet).Range("GX" & iThisRow).Value)

If (Not Contains(cMaster, sPontoA)) Then


Exit Sub
End If

iRow = cMaster.Item(sPontoA)
Call FillRowWithMasterPontoA(iRow, "ENTREGUE")

End Sub

Sub FillRowWithMasterPontoA(iRow As Long, sSheetName As String)

wbThisWorkbook.Sheets(sThisSheet).Range("GY" & iThisRow).Value =


wbMaster.Sheets(sSheetName).Range("W" & iRow).Value 'DT FAT PLANEJADO (CAMPO DATA)
wbThisWorkbook.Sheets(sThisSheet).Range("GZ" & iThisRow).Value =
wbMaster.Sheets(sSheetName).Range("Z" & iRow).Value 'DT DOCA PLANEJADO (CAMPO DATA)

End Sub

Sub FillWithMasterPontoB()

Dim iRow As Long

sPontoB = Trim(wbThisWorkbook.Sheets(sThisSheet).Range("HD" & iThisRow).Value)

If (Not Contains(cMaster, sPontoB)) Then


Exit Sub
End If

iRow = cMaster.Item(sPontoB)
Call FillRowWithMasterPontoB(iRow, "ENTREGUE")
End Sub

Sub FillRowWithMasterPontoB(iRow As Long, sSheetName As String)

wbThisWorkbook.Sheets(sThisSheet).Range("HE" & iThisRow).Value =


wbMaster.Sheets(sSheetName).Range("W" & iRow).Value 'DT FAT PLANEJADO (CAMPO DATA)
wbThisWorkbook.Sheets(sThisSheet).Range("HF" & iThisRow).Value =
wbMaster.Sheets(sSheetName).Range("Z" & iRow).Value 'DT DOCA PLANEJADO (CAMPO DATA)

End Sub

You might also like