Sunteți pe pagina 1din 68

Public os, calendario As Boolean

Public dir As String


Public fla1, fla2, fla3
Function Connect(Path As String, sql As Variant, Optional ByVal psw)
'm8thisfuN
Dim V() As Variant
Dim cn As ADODB.Connection, rs As ADODB.Recordset, sSQL As String, myField, Rz A
s ADODB.Recordset

'fladb = False
On Local Error Resume Next
Set cn = New ADODB.Connection
With cn
.Provider = os
.Properties("Jet OLEDB:Database Password") = psw
.Open Path
If Not IsArray(sql) Then
Set rs = .Execute(sql)
Else
Set rs = .Execute(Trim(Join(sql, "")))
End If
'Set rs = IIf(IsArray(sql), .Execute(sql), .Execute(Trim(Join(sql, "")))
)
'.Execute(Trim(Join(args(0)(2), "")))
End With
'fladb = True

'Test de no records
'If Rs.EOF Then er = 4: GoTo erro_handler:
ReDim Preserve V(1 To rs.Fields.Count, 0)
Do Until rs.EOF
For Each myField In rs.Fields
o = o + 1
If o = rs.Fields.Count + 1 Then
D = D + 1 '+ IIf(IsInArray("T", obt), 1, 0)
o = 1
ReDim Preserve V(1 To rs.Fields.Count, D) '+ IIf(IsInArray("T", obt)
, 1, 0)
End If
V(o, D) = myField
Next myField
rs.MoveNext
Loop
Connect = V
End Function
Sub activeRef()
'Add Microsoft ActiveX Data Objects xx Library base on ISO
On Error Resume Next
If Is64bit() Then 'Revisa si es de 64 o 32 bits, intala la referencia adecuada
ThisWorkbook.VBProject.References.AddFromGuid "{2A75196C-D9EB-4129-B803-931327F7
2D5C}", 2, 8
os = "Microsoft.ACE.OLEDB.12.0"
ThisWorkbook.VBProject.References.AddFromGuid "{420B2830-E718-11CF-893D-00A0C905
4228}", 1, 0
Else
ThisWorkbook.VBProject.References.AddFromGuid "{00000206-0000-0010-8000-00AA006D
2EA4}", 2, 6
os = IIf(Err.number = 1004, "Microsoft.ACE.OLEDB.12.0", "Microsoft.Jet.OLEDB.4.0
")
ThisWorkbook.VBProject.References.AddFromGuid "{420B2830-E718-11CF-893D-00A0C905
4228}", 1, 0
End If
End Sub
Sub CreaCases()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
mainSheet = ActiveSheet.Name
Call frmCaseGenerator.CreateSheetCases("DataDump")
Sheets(mainSheet).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub DirMod()
Dim fulldir As String
Dim WM As Worksheet: Set WM = Worksheets("Main")
Dim ary As Variant
iFilePath = FilePath("GRTMPS Result database (*.mdb), *.mdb")
If iFilePath = False Then Exit Sub
Call activeRef
'Datos del modelo
fulldir = iFilePath
sql = "SELECT Cases.Name, Cases.Description FROM Cases": io = Connect(fulldir,
sql)
With WM
.Cells(9, 4) = fulldir
.Cells(10, 4) = io(1, 0): .Cells(10, 7) = IIf(IsNull(io(2, 0)), "N/D", io(2, 0))
End With
'Seleccin del periodo
sql = "SELECT Period.Name FROM Period": io = Connect(fulldir, sql)
ReDim ary(UBound(io, 1))
For i = 0 To UBound(io, 1)
ary(i) = "'" & io(1, i)
Next i
With WM.Cells(10, 11).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(ary, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
'Inicializacin de unidades
If Worksheets("Main").Cells(11, 11) = "WT" Then
If Worksheets("Main").Cells(11, 14) = "Mbd" Then
Worksheets("Main").Cells(11, 14) = "Mtd"
Else
Worksheets("Main").Cells(11, 14) = "Td"
End If
Else
If Worksheets("Main").Cells(11, 14) = "Mtd" Then
Worksheets("Main").Cells(11, 14) = "Mbd"
Else
Worksheets("Main").Cells(11, 14) = "Bld"
End If
End If

End Sub
Sub ShowExtExplorer()
Dim i As Long
Dim sMsg As String

For i = 1 To UserForms.Count
Unload UserForms(i - 1)
Next
frmCaseGenerator.Show vbModeless

End Sub
Sub CreaAuxSheets()
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim io
Call activeRef
Set WM = Worksheets("Main"): Set WS = Worksheets("Input")
fulldir = WM.Cells(9, 4)
basis = "_" & WM.Cells(11, 11)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")

'Hoja de Unidades de proceso


frmMessages.Caption = "Generador de Casos: paso 1 de 8"
Set iWS = Worksheets("Units")
With iWS
.UsedRange.Clear
.Cells(1, 1) = "Case": .Cells(1, 2) = "Location": .Cells(1, 3) = "Unit": .Cells(
1, 4) = "Description"
.Cells(1, 5) = "Name": .Cells(1, 6) = "MIN" & basis: .Cells(1, 7) = "MAX" & basi
s: .Cells(1, 8) = "iMIN" & basis: .Cells(1, 9) = "iMAX" & basis: .Cells(1, 10) =
"#Conf": .Cells(1, 11) = "ManI": .Cells(1, 12) = "ManF": .Cells(1, 13) = "Dur/p
er": .Cells(1, 14) = "#Manto"
End With
sql = CreaCaseQuery("Caps", Period, , , factor): io = Connect(fulldir, sql)
cas = TrimElemArray(Split(WS.Range("C3").Validation.Formula1, ","))
h = 1
With iWS
per = IIf(WM.Cells(10, 11) <> "", IIf(WM.Cells(10, 11) <> "XX", WM.Cells(10, 11)
, "**"), "**")
For i = LBound(cas) To UBound(cas)
For j = LBound(io, 2) To UBound(io, 2)
h = h + 1: p = Split(cas(i), "(")
.Cells(h, 1) = Trim(p(0))
.Cells(h, 2) = io(4, j)
.Cells(h, 3) = io(1, j)
.Cells(h, 4) = io(3, j)
.Cells(h, 5) = io(4, j) & per & io(2, j)
Next j
Next i
End With

'Proclim
frmMessages.Caption = "Generador de Casos: paso 2 de 8"
Set iWS = Worksheets("Proclim")
With iWS
.UsedRange.Clear
.Cells(1, 1) = "Case": .Cells(1, 2) = "Location": .Cells(1, 3) = "Unit": .Cells(
1, 4) = "Proclim": .Cells(1, 5) = "Description"
.Cells(1, 6) = "Name": .Cells(1, 7) = "MIN" & basis: .Cells(1, 8) = "MAX" & basi
s
End With
sql = CreaCaseQuery("Proclim", Period): io = Connect(fulldir, sql)
cas = TrimElemArray(Split(WS.Range("C3").Validation.Formula1, ","))
h = 1
With iWS
per = IIf(WM.Cells(10, 11) <> "", IIf(WM.Cells(10, 11) <> "XX", WM.Cells(10, 11)
, "**"), "**")
For i = LBound(cas) To UBound(cas)
For j = LBound(io, 2) To UBound(io, 2)
h = h + 1: p = Split(cas(i), "(")
.Cells(h, 1) = Trim(p(0))
.Cells(h, 2) = io(4, j)
.Cells(h, 3) = io(1, j)
.Cells(h, 4) = io(2, j)
.Cells(h, 5) = io(3, j)
.Cells(h, 6) = io(4, j) & per & io(2, j) & "$"
Next j
Next i
End With

'Purchases
frmMessages.Caption = "Generador de Casos: paso 3 de 8"
Set iWS = Worksheets("Purchases")
With iWS
.UsedRange.Clear
.Cells(1, 1) = "Case": .Cells(1, 2) = "Location": .Cells(1, 3) = "Account": .Cel
ls(1, 4) = "Stream": .Cells(1, 5) = "IsSale": .Cells(1, 6) = "Description"
.Cells(1, 7) = "Name": .Cells(1, 8) = "MIN" & basis: .Cells(1, 9) = "MAX" & basi
s: .Cells(1, 10) = "Prices" & basis
End With
sql = CreaCaseQuery("pur", Period, , , , factor):
io = Connect(fulldir, sql)
cas = TrimElemArray(Split(WS.Range("C3").Validation.Formula1, ","))
h = 1
With iWS
per = IIf(WM.Cells(10, 11) <> "", IIf(WM.Cells(10, 11) <> "XX", WM.Cells(10, 11)
, "**"), "**")
For i = LBound(cas) To UBound(cas)
For j = LBound(io, 2) To UBound(io, 2)
h = h + 1: p = Split(cas(i), "(")
.Cells(h, 1) = Trim(p(0))
.Cells(h, 2) = io(4, j)
.Cells(h, 3) = io(2, j)
.Cells(h, 4) = io(1, j)
.Cells(h, 5) = io(5, j)
.Cells(h, 6) = io(3, j)
.Cells(h, 7) = io(4, j) & per & io(1, j)
Next j
Next i
End With

'Crudos
frmMessages.Caption = "Generador de Casos: paso 4 de 8"
Set iWS = Worksheets("Oil")
With iWS
.UsedRange.Clear
.Cells(1, 1) = "Case": .Cells(1, 2) = "Location": .Cells(1, 3) = "Account": .Cel
ls(1, 4) = "Stream": .Cells(1, 5) = "Description"
.Cells(1, 6) = "Name": .Cells(1, 7) = "MIN" & basis: .Cells(1, 8) = "MAX" & basi
s: .Cells(1, 9) = "Prices" & basis
End With
sql = CreaCaseQuery("oil", Period, "NUEVO TEAPA", , , factor):
io = Connect(fulldir, sql)
cas = TrimElemArray(Split(WS.Range("C3").Validation.Formula1, ","))
h = 1
With iWS
per = IIf(WM.Cells(10, 11) <> "", IIf(WM.Cells(10, 11) <> "XX", WM.Cells(10, 11)
, "**"), "**")
For i = LBound(cas) To UBound(cas)
For j = LBound(io, 2) To UBound(io, 2)
h = h + 1: p = Split(cas(i), "(")
.Cells(h, 1) = Trim(p(0))
.Cells(h, 2) = io(4, j)
.Cells(h, 3) = io(2, j)
.Cells(h, 4) = io(1, j)
.Cells(h, 5) = io(3, j)
.Cells(h, 6) = io(4, j) & per & io(1, j)

Next j
Next i
End With

' Sales Ref


frmMessages.Caption = "Generador de Casos: paso 5 de 8"
Set iWS = Worksheets("SalesRef")
With iWS
.UsedRange.Clear
.Cells(1, 1) = "Case": .Cells(1, 2) = "Location": .Cells(1, 3) = "Account": .Cel
ls(1, 4) = "Stream": .Cells(1, 5) = "IsSale": .Cells(1, 6) = "Description"
.Cells(1, 7) = "Name": .Cells(1, 8) = "MIN" & basis: .Cells(1, 9) = "MAX" & basi
s: .Cells(1, 10) = "Prices" & basis
End With
sql = CreaCaseQuery("salRef", Period, , , , factor):
io = Connect(fulldir, sql)
cas = TrimElemArray(Split(WS.Range("C3").Validation.Formula1, ","))
h = 1
With iWS
per = IIf(WM.Cells(10, 11) <> "", IIf(WM.Cells(10, 11) <> "XX", WM.Cells(10, 11)
, "**"), "**")
For i = LBound(cas) To UBound(cas)
For j = LBound(io, 2) To UBound(io, 2)
h = h + 1: p = Split(cas(i), "(")
.Cells(h, 1) = Trim(p(0))
.Cells(h, 2) = io(4, j)
.Cells(h, 3) = io(1, j)
.Cells(h, 4) = io(2, j)
.Cells(h, 5) = io(5, j)
.Cells(h, 6) = io(3, j)
.Cells(h, 7) = io(4, j) & per & io(2, j)
Next j
Next i
End With
' Sales Tar
frmMessages.Caption = "Generador de Casos: paso 6 de 8"
Set iWS = Worksheets("SalesTar")
With iWS
.UsedRange.Clear
.Cells(1, 1) = "Case": .Cells(1, 2) = "Location": .Cells(1, 3) = "Account": .Cel
ls(1, 4) = "Stream": .Cells(1, 5) = "IsSale": .Cells(1, 6) = "Description"
.Cells(1, 7) = "Name": .Cells(1, 8) = "MIN" & basis: .Cells(1, 9) = "MAX" & basi
s: .Cells(1, 10) = "Prices" & basis
End With
sql = CreaCaseQuery("salTar", Period, , , , factor):
io = Connect(fulldir, sql)
cas = TrimElemArray(Split(WS.Range("C3").Validation.Formula1, ","))
h = 1
With iWS
per = IIf(WM.Cells(10, 11) <> "", IIf(WM.Cells(10, 11) <> "XX", WM.Cells(10, 11)
, "**"), "**")
For i = LBound(cas) To UBound(cas)
For j = LBound(io, 2) To UBound(io, 2)
h = h + 1: p = Split(cas(i), "(")
.Cells(h, 1) = Trim(p(0))
.Cells(h, 2) = io(4, j)
.Cells(h, 3) = io(1, j)
.Cells(h, 4) = io(2, j)
.Cells(h, 5) = io(5, j)
.Cells(h, 6) = io(3, j)
.Cells(h, 7) = io(4, j) & per & io(2, j)
Next j
Next i
End With

' Importacion
frmMessages.Caption = "Generador de Casos: paso 7 de 8"
Set iWS = Worksheets("Import")
With iWS
.UsedRange.Clear
.Cells(1, 1) = "Case": .Cells(1, 2) = "Location": .Cells(1, 3) = "Account": .Cel
ls(1, 4) = "Stream": .Cells(1, 5) = "IsSale": .Cells(1, 6) = "Description"
.Cells(1, 7) = "Name": .Cells(1, 8) = "MIN" & basis: .Cells(1, 9) = "MAX" & basi
s: .Cells(1, 10) = "Prices" & basis
End With
sql = CreaCaseQuery("Import", Period, , , , factor):
io = Connect(fulldir, sql)
cas = TrimElemArray(Split(WS.Range("C3").Validation.Formula1, ","))
h = 1
With iWS
per = IIf(WM.Cells(10, 11) <> "", IIf(WM.Cells(10, 11) <> "XX", WM.Cells(10, 11)
, "**"), "**")
For i = LBound(cas) To UBound(cas)
For j = LBound(io, 2) To UBound(io, 2)
h = h + 1: p = Split(cas(i), "(")
.Cells(h, 1) = Trim(p(0))
.Cells(h, 2) = io(4, j)
.Cells(h, 3) = io(1, j)
.Cells(h, 4) = io(2, j)
.Cells(h, 5) = io(5, j)
.Cells(h, 6) = io(3, j)
.Cells(h, 7) = io(4, j) & per & io(2, j)
Next j
Next i
End With
End Sub
Function CreaCaseQuery(ByVal qryType As String, Optional ByVal periodo, Optional
ByVal location, Optional ByVal Duration, Optional ByVal Ubase, Optional ByVal F
, Optional ByVal A)
Dim D
F = IIf(IsMissing(F), "/1", F)
A = IIf(IsMissing(A), "Todas", A)
Duration = IIf(IsMissing(Duration), False, Duration)
basis = IIf(IsMissing(Ubase), "", Ubase)
location = IIf(IsMissing(location), "Todas", location)
D = IIf(Duration = True, "/[Period.Duration]", "")
Dim sql()
ReDim sql(0)
If qryType = "Manto" Or qryType = "Caps" Then
Ji = "Loading_Limit": JD = "Loading_Factor"
sql(0) = "SELECT DISTINCT [vUnitMaterialBalance.UnitName],[Loading_Factor.Name],
[Process_Unit.Description]+':'+[Loading_Factor.Description],[Location.Name],[Pr
ocess_Unit.Sort], "
'ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "IIF([Loading_Limit.Min
] IS NULL, '',[Loading_Limit.Min]" & D & F & "), IIF([Loading_Limit.Max] IS NULL
, '',[Loading_Limit.Max]" & D & F & "), IIF([Loading_Limit.Fix] IS NULL, '',[Loa
ding_Limit.Fix]" & D & F & "),"
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "IIF([Loading_Limit.Min]
IS NULL, 0,[Loading_Limit.Min]" & D & F & "), IIF([Loading_Limit.Max] IS NULL,
0,[Loading_Limit.Max]" & D & F & "), IIF([Loading_Limit.Fix] IS NULL, 0,[Loading
_Limit.Fix]" & D & F & "),"
'Union de tablas de busqueda
For h = UBound(sql) To LBound(sql) Step -1: If sql(h) <> Empty Then Exit
For
Next h: sql(h) = Left(sql(h), Len(sql(h)) - 1): fi = h + 1
ReDim Preserve sql(LBound(sql) To UBound(sql) + 2): sql(UBound(sql)) =
" INNER JOIN Cases ON Cases.CaseNum=" & Ji & ".CaseNum)"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN Period ON Period.PeriodCode=" & Ji & ".PeriodCode)" 'Agrega tabal
de descriptor
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN Location ON Location.LocationCode=" & Ji & ".LocationCode)" 'Tabl
a de locaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN vUnitMaterialBalance ON vUnitMaterialBalance.UnitCode=" & Ji & ".U
nitCode)" 'Tabla de locaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN Loading_Factor ON Loading_Factor.ControlCode=" & Ji & ".ControlCod
e)" 'Tabla de locaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN Process_Unit ON Process_Unit.UnitCode=" & Ji & ".UnitCode)"
sql(fi) = " FROM ": sql(fi - 1) = IIf(Right(sql(fi - 1), 1) = ",", Left(
sql(fi - 1), Len(sql(fi - 1)) - 1), sql(fi - 1))
'Condicion de busqueda
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " W
HERE vUnitMaterialBalance.UnitName=[Process_Unit.Name]+'-'+[Process_Unit.ShortCo
de]":
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " A
ND vUnitMaterialBalance.LocationCode=Location.LocationCode"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " A
ND Loading_Factor.Description LIKE '%CAP%' "
If location <> "Todas" Then ReDim Preserve sql(LBound(sql) To UBound(sql)
+ 1): sql(UBound(sql)) = " AND Location.Description='" & location & "'"
If A <> "Todas" Then ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): s
ql(UBound(sql)) = " AND Process_Unit.Description='" & A & "'"

ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "


ORDER BY [Location.Name] ASC, [Process_Unit.Sort] ASC " ', vUnitMaterialBalance
WHERE Location.LocationCode=vUnitMaterialBalance.LocationCode ORDER BY [Locatio
n.Description] ASC"
'Reajusta FROM
E = "": For h = 1 To Content2(sql, "INNER"): E = E & "(": Next h: sql(St
rInArrayIndex(sql, "FROM")) = " FROM " & E & Ji & ""
ElseIf qryType = "Proclim" Then
Ji = "Loading_Limit": JD = "Loading_Factor"

sql(0) = "SELECT DISTINCT [vUnitMaterialBalance.UnitName],[Loading_Factor.Na


me], [Process_Unit.Description]+':'+[Loading_Factor.Description],[Location.Name]
,[Process_Unit.Sort],[Cases.Name], "
'ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "IIF([Loading_L
imit.Min] IS NULL, '',[Loading_Limit.Min]" & D & F & "), IIF([Loading_Limit.Max]
IS NULL, '',[Loading_Limit.Max]" & D & F & "), IIF([Loading_Limit.Fix] IS NULL,
'',[Loading_Limit.Fix]" & D & F & "),"
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "IIF([Loading_Li
mit.Min] IS NULL, 0,[Loading_Limit.Min]" & D & F & "), IIF([Loading_Limit.Max] I
S NULL, 0,[Loading_Limit.Max]" & D & F & "), IIF([Loading_Limit.Fix] IS NULL, 0,
[Loading_Limit.Fix]" & D & F & "),"
'Union de tablas de busqueda
For h = UBound(sql) To LBound(sql) Step -1: If sql(h) <> Empty Then Exit
For
Next h: sql(h) = Left(sql(h), Len(sql(h)) - 1): fi = h + 1
ReDim Preserve sql(LBound(sql) To UBound(sql) + 2): sql(UBound(sql)) =
" INNER JOIN Cases ON Cases.CaseNum=" & Ji & ".CaseNum)"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN Period ON Period.PeriodCode=" & Ji & ".PeriodCode)" 'Agrega tabal
de descriptor
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN Location ON Location.LocationCode=" & Ji & ".LocationCode)" 'Tabl
a de locaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN vUnitMaterialBalance ON vUnitMaterialBalance.UnitCode=" & Ji & ".U
nitCode)" 'Tabla de locaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN Loading_Factor ON Loading_Factor.ControlCode=" & Ji & ".ControlCod
e)" 'Tabla de locaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" INNER JOIN Process_Unit ON Process_Unit.UnitCode=" & Ji & ".UnitCode)"
sql(fi) = " FROM ": sql(fi - 1) = IIf(Right(sql(fi - 1), 1) = ",", Left(
sql(fi - 1), Len(sql(fi - 1)) - 1), sql(fi - 1))
'Condicion de busqueda
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " W
HERE vUnitMaterialBalance.UnitName=[Process_Unit.Name]+'-'+[Process_Unit.ShortCo
de]":
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " A
ND vUnitMaterialBalance.LocationCode=Location.LocationCode"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " A
ND " & Ji & ".IsRatio=True "
If location <> "Todas" Then ReDim Preserve sql(LBound(sql) To UBound(sql)
+ 1): sql(UBound(sql)) = " AND Location.Description='" & location & "'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "
ORDER BY [Location.Name] ASC,[Process_Unit.Sort] ASC " ', vUnitMaterialBalance
WHERE Location.LocationCode=vUnitMaterialBalance.LocationCode ORDER BY [Location
.Description] ASC"
'Reajusta FROM
E = "": For h = 1 To Content2(sql, "INNER"): E = E & "(": Next h: sql(St
rInArrayIndex(sql, "FROM")) = " FROM " & E & Ji & ""
ElseIf qryType = "pur" Or qryType = "sal" Or qryType = "Import" Then
Ji = "Purchase_Sale": JD = "Cost_Account": JC = "Stream"
account = Worksheets("Input").Cells(5, 3)
ps = IIf(qryType = "pur" Or qryType = "Import", "False", "True"):
sql(0) = "SELECT [Stream.Name],[Cost_Account.Name],IIF([Cost_Account.Description
] IS NOT NULL,[Cost_Account.Description],'N/D')+':'+IIF([Stream.Description] IS
NOT NULL,[Stream.Description],'N/D'),[Location.Name],[Purchase_Sale.IsSale],"
'ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "IIF
([" & Ji & ".Min" & basis & "] IS NULL, '',[" & Ji & ".Min" & basis & "]" & D &
F & "), IIF([" & Ji & ".Max" & basis & "] IS NULL, '',[" & Ji & ".Max" & basis &
"]" & D & F & "),IIF([" & Ji & ".Fix" & basis & "] IS NULL, '',[" & Ji & ".Fix"
& basis & "]" & D & F & "),IIF([" & Ji & ".Price" & IIf(basis = "_WT", "", "2")
& "] IS NULL, '',[" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "]),"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "IIF(
[" & Ji & ".Min" & basis & "] IS NULL, 0,[" & Ji & ".Min" & basis & "]" & D & F
& "), IIF([" & Ji & ".Max" & basis & "] IS NULL, 0,[" & Ji & ".Max" & basis & "]
" & D & F & "),IIF([" & Ji & ".Fix" & basis & "] IS NULL, 0,[" & Ji & ".Fix" & b
asis & "]" & D & F & "),IIF([" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "
] IS NULL, 0,[" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "]),"
'Union de tablas de busqueda
For h = UBound(sql) To LBound(sql) Step -1: If sql(h) <> Empty Then Exit
For
Next h: sql(h) = Left(sql(h), Len(sql(h)) - 1): fi = h + 1
ReDim Preserve sql(LBound(sql) To UBound(sql) + 2): sql(UBound(sql)) = " INN
ER JOIN Cases ON Cases.CaseNum=" & Ji & ".CaseNum)"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Period ON Period.PeriodCode=" & Ji & ".PeriodCode)" 'Agrega tabal de des
criptor
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Location ON Location.LocationCode=" & Ji & ".LocationCode)" 'Tabla de l
ocaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Cost_Account ON Cost_Account.Account=" & Ji & ".Account)" 'Tabla de loca
ci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Stream ON Stream.StreamCode=" & Ji & ".StreamCode)" 'Tabla de locaci
sql(fi) = " FROM ": sql(fi - 1) = IIf(Right(sql(fi - 1), 1) = ",", Left(sql(
fi - 1), Len(sql(fi - 1)) - 1), sql(fi - 1))
'Condicion de busqueda
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " W
HERE " & Ji & ".IsSale=" & ps:
'If per <> Empty Then ReDim Preserve sql(LBound(sql) To UBound(sql) + 1):
sql(UBound(sql)) = " AND Period.Name='" & per & "'"

'If qryType = "oil" Then location = "NUEVO TEAPA"


If location <> "Todas" Then ReDim Preserve sql(LBound(sql) To UBound(sql)
+ 1): sql(UBound(sql)) = " AND Location.Description='" & location & "'"
'ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) =
" AND Location.Description LIKE '%Imp.%'"
If qryType = "pur" Then
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(s
ql)) = " AND Location.Name NOT LIKE '%NT%'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(s
ql)) = " AND Cost_Account.Name NOT LIKE '%CRX%'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(s
ql)) = " AND Location.Name NOT LIKE 'IE%'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(s
ql)) = " AND Location.Name NOT LIKE 'IO%'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(s
ql)) = " AND " & Ji & ".Price" & IIf(basis = "_WT", "", "2") & " < 9999"
ElseIf qryType = "Import" Then
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(
sql)) = " AND Location.Description LIKE '%Imp%'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(s
ql)) = " AND " & Ji & ".Price" & IIf(basis = "_WT", "", "2") & " < 9999"
End If

If A <> 0 Then If account <> Empty Then If account <> "Todas" Then ReDim P
reserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " AND Cost_Accou
nt.Description='" & account & "'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "
ORDER BY [Location.Name] ASC,[Cost_Account.Name] ASC,[Cost_Account.Description]
+':'+[Stream.Description] ASC " ', vUnitMaterialBalance WHERE Location.LocationC
ode=vUnitMaterialBalance.LocationCode ORDER BY [Location.Description] ASC"
'Reajusta FROM
E = "": For h = 1 To Content2(sql, "INNER"): E = E & "(": Next h: sql(St
rInArrayIndex(sql, "FROM")) = " FROM " & E & Ji & ""
ElseIf qryType = "oil" Then
Ji = "Purchase_Sale": JD = "Cost_Account": JC = "Stream"
account = Worksheets("Input").Cells(5, 3)
ps = IIf(qryType = "oil", "False", "True"):
sql(0) = "SELECT [Stream.Name],[Cost_Account.Name],IIF([Cost_Account.Description
] IS NOT NULL,[Cost_Account.Description],'N/D')+':'+IIF([Stream.Description] IS
NOT NULL,[Stream.Description],'N/D'),[Location.Name],[Purchase_Sale.IsSale],"
'ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "IIF
([" & Ji & ".Min" & basis & "] IS NULL, '',[" & Ji & ".Min" & basis & "]" & D &
F & "), IIF([" & Ji & ".Max" & basis & "] IS NULL, '',[" & Ji & ".Max" & basis &
"]" & D & F & "),IIF([" & Ji & ".Fix" & basis & "] IS NULL, '',[" & Ji & ".Fix"
& basis & "]" & D & F & "),IIF([" & Ji & ".Price" & IIf(basis = "_WT", "", "2")
& "] IS NULL, '',[" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "]),"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "IIF(
[" & Ji & ".Min" & basis & "] IS NULL, 0,[" & Ji & ".Min" & basis & "]" & D & F
& "), IIF([" & Ji & ".Max" & basis & "] IS NULL, 0,[" & Ji & ".Max" & basis & "]
" & D & F & "),IIF([" & Ji & ".Fix" & basis & "] IS NULL, 0,[" & Ji & ".Fix" & b
asis & "]" & D & F & "),IIF([" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "
] IS NULL, 0,[" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "]),"
'Union de tablas de busqueda
For h = UBound(sql) To LBound(sql) Step -1: If sql(h) <> Empty Then Exit
For
Next h: sql(h) = Left(sql(h), Len(sql(h)) - 1): fi = h + 1
ReDim Preserve sql(LBound(sql) To UBound(sql) + 2): sql(UBound(sql)) = " INN
ER JOIN Cases ON Cases.CaseNum=" & Ji & ".CaseNum)"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Period ON Period.PeriodCode=" & Ji & ".PeriodCode)" 'Agrega tabal de des
criptor
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Location ON Location.LocationCode=" & Ji & ".LocationCode)" 'Tabla de l
ocaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Cost_Account ON Cost_Account.Account=" & Ji & ".Account)" 'Tabla de loca
ci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Stream ON Stream.StreamCode=" & Ji & ".StreamCode)" 'Tabla de locaci
sql(fi) = " FROM ": sql(fi - 1) = IIf(Right(sql(fi - 1), 1) = ",", Left(sql(
fi - 1), Len(sql(fi - 1)) - 1), sql(fi - 1))
'Condicion de busqueda
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " W
HERE " & Ji & ".IsSale=" & ps:
'If per <> Empty Then ReDim Preserve sql(LBound(sql) To UBound(sql) + 1):
sql(UBound(sql)) = " AND Period.Name='" & per & "'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "
AND Location.Description='" & location & "'"
'If A <> 0 Then If account <> Empty Then If account <> "Todas" Then ReDim
Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " AND Cost_Acco
unt.Description='" & account & "'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "
ORDER BY [Location.Name] ASC,[Cost_Account.Name] ASC,[Cost_Account.Description]
+':'+[Stream.Description] ASC " ', vUnitMaterialBalance WHERE Location.LocationC
ode=vUnitMaterialBalance.LocationCode ORDER BY [Location.Description] ASC"
'Reajusta FROM
E = "": For h = 1 To Content2(sql, "INNER"): E = E & "(": Next h: sql(St
rInArrayIndex(sql, "FROM")) = " FROM " & E & Ji & ""
ElseIf qryType = "salTar" Then
Ji = "Purchase_Sale": JD = "Cost_Account": JC = "Stream"
account = Worksheets("Input").Cells(5, 3)
ps = IIf(qryType = "pur", "False", "True"):
sql(0) = "SELECT [Stream.Name],[Cost_Account.Name],IIF([Cost_Account.Description
] IS NOT NULL,[Cost_Account.Description],'N/D')+':'+IIF([Stream.Description] IS
NOT NULL,[Stream.Description],'N/D'),[Location.Name],[Purchase_Sale.IsSale],"
' ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "IIF
([" & Ji & ".Min" & basis & "] IS NULL, '',[" & Ji & ".Min" & basis & "]" & D &
F & "), IIF([" & Ji & ".Max" & basis & "] IS NULL, '',[" & Ji & ".Max" & basis &
"]" & D & F & "),IIF([" & Ji & ".Fix" & basis & "] IS NULL, '',[" & Ji & ".Fix"
& basis & "]" & D & F & "),IIF([" & Ji & ".Price" & IIf(basis = "_WT", "", "2")
& "] IS NULL, '',[" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "]),"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "IIF(
[" & Ji & ".Min" & basis & "] IS NULL, 0,[" & Ji & ".Min" & basis & "]" & D & F
& "), IIF([" & Ji & ".Max" & basis & "] IS NULL, 0,[" & Ji & ".Max" & basis & "]
" & D & F & "),IIF([" & Ji & ".Fix" & basis & "] IS NULL, 0,[" & Ji & ".Fix" & b
asis & "]" & D & F & "),IIF([" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "
] IS NULL, 0,[" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "]),"
'Union de tablas de busqueda
For h = UBound(sql) To LBound(sql) Step -1: If sql(h) <> Empty Then Exit
For
Next h: sql(h) = Left(sql(h), Len(sql(h)) - 1): fi = h + 1
ReDim Preserve sql(LBound(sql) To UBound(sql) + 2): sql(UBound(sql)) = " INN
ER JOIN Cases ON Cases.CaseNum=" & Ji & ".CaseNum)"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Period ON Period.PeriodCode=" & Ji & ".PeriodCode)" 'Agrega tabal de des
criptor
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Location ON Location.LocationCode=" & Ji & ".LocationCode)" 'Tabla de l
ocaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Cost_Account ON Cost_Account.Account=" & Ji & ".Account)" 'Tabla de loca
ci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Stream ON Stream.StreamCode=" & Ji & ".StreamCode)" 'Tabla de locaci
sql(fi) = " FROM ": sql(fi - 1) = IIf(Right(sql(fi - 1), 1) = ",", Left(sql(
fi - 1), Len(sql(fi - 1)) - 1), sql(fi - 1))
'Condicion de busqueda
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " W
HERE " & Ji & ".IsSale=" & ps:
'If per <> Empty Then ReDim Preserve sql(LBound(sql) To UBound(sql) + 1):
sql(UBound(sql)) = " AND Period.Name='" & per & "'"
'ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "
AND Location.Description LIKE '%TAR%'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = IIf
(location <> "Todas", " AND Location.Description='" & location & "'", " AND Loca
tion.Description LIKE '%TAR%'")
'If location <> "Todas" Then ReDim Preserve sql(LBound(sql) To UBound(sql)
+ 1): sql(UBound(sql)) = " AND Location.Description='" & location & "'"
'If A <> 0 Then If account <> Empty Then If account <> "Todas" Then ReDim
Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " AND Cost_Acco
unt.Description='" & account & "'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "
ORDER BY [Location.Name] ASC,[Cost_Account.Name] ASC,[Cost_Account.Description]
+':'+[Stream.Description] ASC " ', vUnitMaterialBalance WHERE Location.LocationC
ode=vUnitMaterialBalance.LocationCode ORDER BY [Location.Description] ASC"
'Reajusta FROM
E = "": For h = 1 To Content2(sql, "INNER"): E = E & "(": Next h: sql(St
rInArrayIndex(sql, "FROM")) = " FROM " & E & Ji & ""

ElseIf qryType = "salRef" Then


Ji = "Purchase_Sale": JD = "Cost_Account": JC = "Stream"
ps = IIf(qryType = "pur", "False", "True"):
account = A
sql(0) = "SELECT [Stream.Name],[Cost_Account.Name],IIF([Cost_Account.Description
] IS NOT NULL,[Cost_Account.Description],'N/D')+':'+IIF([Stream.Description] IS
NOT NULL,[Stream.Description],'N/D'),[Location.Name],[Purchase_Sale.IsSale],"
' ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "IIF
([" & Ji & ".Min" & basis & "] IS NULL, '',[" & Ji & ".Min" & basis & "]" & D &
F & "), IIF([" & Ji & ".Max" & basis & "] IS NULL, '',[" & Ji & ".Max" & basis &
"]" & D & F & "),IIF([" & Ji & ".Fix" & basis & "] IS NULL, '',[" & Ji & ".Fix"
& basis & "]" & D & F & "),IIF([" & Ji & ".Price" & IIf(basis = "_WT", "", "2")
& "] IS NULL, '',[" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "]),"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "IIF(
[" & Ji & ".Min" & basis & "] IS NULL, 0,[" & Ji & ".Min" & basis & "]" & D & F
& "), IIF([" & Ji & ".Max" & basis & "] IS NULL, 0,[" & Ji & ".Max" & basis & "]
" & D & F & "),IIF([" & Ji & ".Fix" & basis & "] IS NULL, 0,[" & Ji & ".Fix" & b
asis & "]" & D & F & "),IIF([" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "
] IS NULL, 0,[" & Ji & ".Price" & IIf(basis = "_WT", "", "2") & "]),"
'Union de tablas de busqueda
For h = UBound(sql) To LBound(sql) Step -1: If sql(h) <> Empty Then Exit
For
Next h: sql(h) = Left(sql(h), Len(sql(h)) - 1): fi = h + 1
ReDim Preserve sql(LBound(sql) To UBound(sql) + 2): sql(UBound(sql)) = " INN
ER JOIN Cases ON Cases.CaseNum=" & Ji & ".CaseNum)"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Period ON Period.PeriodCode=" & Ji & ".PeriodCode)" 'Agrega tabal de des
criptor
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Location ON Location.LocationCode=" & Ji & ".LocationCode)" 'Tabla de l
ocaci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Cost_Account ON Cost_Account.Account=" & Ji & ".Account)" 'Tabla de loca
ci
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " INN
ER JOIN Stream ON Stream.StreamCode=" & Ji & ".StreamCode)" 'Tabla de locaci
sql(fi) = " FROM ": sql(fi - 1) = IIf(Right(sql(fi - 1), 1) = ",", Left(sql(
fi - 1), Len(sql(fi - 1)) - 1), sql(fi - 1))
'Condicion de busqueda
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " W
HERE " & Ji & ".IsSale=" & ps:
'If per <> Empty Then ReDim Preserve sql(LBound(sql) To UBound(sql) + 1):
sql(UBound(sql)) = " AND Period.Name='" & per & "'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " A
ND Location.Description LIKE '%Ref.%'"

If location <> "Todas" Then ReDim Preserve sql(LBound(sql) To UBound(sql)


+ 1): sql(UBound(sql)) = " AND Location.Description='" & location & "'"
If A <> 0 Then If account <> Empty Then If account <> "Todas" Then ReDim P
reserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = " AND Cost_Accou
nt.Description='" & account & "'"
ReDim Preserve sql(LBound(sql) To UBound(sql) + 1): sql(UBound(sql)) = "
ORDER BY [Location.Name] ASC,[Cost_Account.Name] ASC,[Cost_Account.Description]
+':'+[Stream.Description] ASC " ', vUnitMaterialBalance WHERE Location.LocationC
ode=vUnitMaterialBalance.LocationCode ORDER BY [Location.Description] ASC"
'Reajusta FROM
E = "": For h = 1 To Content2(sql, "INNER"): E = E & "(": Next h: sql(St
rInArrayIndex(sql, "FROM")) = " FROM " & E & Ji & ""

End If
CreaCaseQuery = sql
End Function
Sub CapUnits()
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim loc, Aux As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WM = Worksheets("Main"): Set WS = Worksheets("Input"): Set iWS = Worksheets
("Units")
basis = "_" & WM.Cells(11, 11)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)

'Drills de seleccion
With WS
With .Cells(5, 2)
.Font.Bold = True
.HorizontalAlignment = xlRight
.Value = "Planta:"
End With
Call setBordes("Input", "C5")
End With
loc = TrimElemArray(Split(WS.Range("C4").Validation.Formula1, ","))
Aux = IIf(WS.Cells(5, 3) <> "", WS.Cells(5, 3), "Todas")
If HasValidation(WS.Range("C5")) Then Aux = TrimElemArray(Split(WS.Range("C5").V
alidation.Formula1, ","))

On Local Error Resume Next


WS.Cells(4, 3) = IIf(IsInArray(WS.Cells(4, 3), loc), WS.Cells(4, 3), "Todas"
):
WM.Cells(4, 3) = "":
Call Cargalocacion(fulldir, "1", "Input", 4, 3):
Call Cargaunits(fulldir, location, "Input", 5, 3)

'WS.Cells(5, 3) = IIf(IsInArray(WS.Cells(4, 3), location), WS.Cells(4, 3), "


Todas"): WM.Cells(4, 3) = "":
'WS.Cells(5, 3) = IIf(IsInArray(WS.Cells(5, 3), loc), WS.Cells(4, 3), "Todas
"): WM.Cells(4, 3) = "": Call Cargalocacion(fulldir, "1", "Input", 4, 3)

With WS
.Select
.Cells(6, 3) = "Capacidad"
.Cells(8, 2) = "Capacidad"
.Cells(8, 1) = "Planta"
.Range(Cells(IniRow - 2, 5), Cells(IniRow - 1, LastColumn)).Select
With Selection
.Clear
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
'Crea las columnasd de los casos
io = TrimElemArray(Split(Range("C3").Validation.Formula1, ",")) 'Extrae los cas
os de la lista
For i = LBound(io) To UBound(io)
Range(Cells(8, 8 + (3 * (i - 1))), Cells(8, 10 + (3 * (i - 1)))).Merge
Cells(8, 8 + (3 * (i - 1))) = io(i)
Cells(9, 8 + (3 * (i - 1))) = "MIN": Cells(9, 9 + (3 * (i - 1))) = "MAX": Cells(
9, 10 + (3 * (i - 1))) = "%Conf"
With Range(Cells(8, 9 + (3 * (i - 1))), Cells(9, 11 + (3 * (i - 1))))
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
Next i
Call activeRef: sql = CreaCaseQuery("Caps", Period, location, , , factor): io =
Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra los d
atos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub Import()
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim loc As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WM = Worksheets("Main"): Set WS = Worksheets("Input"): Set iWS = Worksheets
("Import")
basis = "_" & WM.Cells(11, 11)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)
'Drills de seleccion
Call removeBordes("Input", "C5")
On Local Error Resume Next
WS.Cells(4, 3) = IIf(IsInArray(WS.Cells(4, 3), loc), WS.Cells(4, 3), "Todas"
): WM.Cells(4, 3) = "": Call Cargalocacion(fulldir, "3", "Input", 4, 3)
With WS
.Select
.Cells(6, 3) = "Importaciones"
.Cells(8, 2) = "Cuentas"
.Range(Cells(IniRow - 2, 5), Cells(IniRow - 1, LastColumn)).Select
With Selection
.Clear
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
'Crea las columnasd de los casos
io = TrimElemArray(Split(Range("C3").Validation.Formula1, ",")) 'Extrae los cas
os de la lista
For i = LBound(io) To UBound(io)
Range(Cells(8, 8 + (3 * (i - 1))), Cells(8, 10 + (3 * (i - 1)))).Merge
Cells(8, 8 + (3 * (i - 1))) = io(i)
Cells(9, 8 + (3 * (i - 1))) = "MIN": Cells(9, 9 + (3 * (i - 1))) = "MAX": Cells(
9, 10 + (3 * (i - 1))) = "PRICES"
With Range(Cells(8, 9 + (3 * (i - 1))), Cells(9, 11 + (3 * (i - 1))))
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
Next i
'Drills de seleccion
Call activeRef: sql = CreaCaseQuery("Import", Period, location, , , factor): io
= Connect(fulldir, sql)
With WS
With .Cells(5, 2)
.Font.Bold = False
.HorizontalAlignment = xlRight
.Value = ""
End With
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra los d
atos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub MantoUnits()
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim loc As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WM = Worksheets("Main"): Set WS = Worksheets("Input"): Set iWS = Worksheets
("Units")
basis = "_" & WM.Cells(11, 11)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)
'Drills de seleccion
Call removeBordes("Input", "C5")
On Local Error Resume Next
WS.Cells(4, 3) = IIf(IsInArray(WS.Cells(4, 3), loc), WS.Cells(4, 3), "Todas"): W
M.Cells(4, 3) = "": Call Cargalocacion(fulldir, "1", "Input", 4, 3)
With WS
.Select
.Cells(6, 3) = "Mantenimiento"
.Cells(8, 2) = "Capacidad"
.Range(Cells(IniRow - 2, 5), Cells(IniRow - 1, LastColumn)).Select
With Selection
.Clear
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
'Crea las columnasd de los casos
io = TrimElemArray(Split(Range("C3").Validation.Formula1, ",")) 'Extrae los cas
os de la lista
For i = LBound(io) To UBound(io)
Range(Cells(8, 7 + (2 * (i - 1))), Cells(8, 8 + (2 * (i - 1)))).Merge
Cells(8, 7 + (2 * (i - 1))) = io(i)
Cells(9, 7 + (2 * (i - 1))) = "Ini": Cells(9, 8 + (2 * (i - 1))) = "Fin":

With Range(Cells(8, 7 + (2 * (i - 1))), Cells(9, 8 + (2 * (i - 1))))


Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
Next i
Call activeRef: sql = CreaCaseQuery("Caps", Period, location, , , factor): io =
Connect(fulldir, sql)
With WS
With .Cells(5, 2)
.Font.Bold = False
.HorizontalAlignment = xlRight
.Value = ""
End With
With .Cells(5, 2)
.Font.Bold = False
.HorizontalAlignment = xlRight
.Value = ""
End With
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra los d
atos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub Importar()
frmImportar.Show
If fla1 = 1 Then Exit Sub
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim number As Integer
Set WM = Worksheets("Main"): Set WS = Worksheets("Input")
basis = "_" & WM.Cells(11, 11)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)
factor2 = Right(factor, Len(factor) - 1)
If frmImportar.opbMB.Value = True Then
Call activeRef
If WS.Cells(6, 3) = "Capacidad" Then number = 1
If WS.Cells(6, 3) = "Lim. de Proceso" Then number = 2
If WS.Cells(6, 3) = "Mantenimiento" Then number = 3
If WS.Cells(6, 3) = "Compras" Then number = 4
If WS.Cells(6, 3) = "Compras Crudo" Then number = 5
If WS.Cells(6, 3) = "Ventas en Ref." Then number = 6
If WS.Cells(6, 3) = "Ventas en TARs" Then number = 7
If WS.Cells(6, 3) = "Importaciones" Then number = 8

Select Case number


Case 1
sql = CreaCaseQuery("Caps", Period, location, , , factor): io = Connect(full
dir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra l
os datos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
.Cells(i + IniRow, 5) = io(6, i): .Cells(i + IniRow, 5).NumberFormat
= "0.00"
.Cells(i + IniRow, 6) = io(7, i): .Cells(i + IniRow, 6).NumberFormat
= "0.00"
.Cells(i + IniRow, 7) = 1: .Cells(i + IniRow, 7).NumberFormat = "0.0
%"
'.Cells(i + IniRow, 5) = IIf(io(8, i) <> "", io(8, i), io(6, i)): .Cells
(i + IniRow, 6) = IIf(.Cells(i + IniRow, 6) <> "", .Cells(i + IniRow, 6) / facto
r, .Cells(i + IniRow, 6))
'.Cells(i + IniRow, 6) = IIf(io(8, i) <> "", io(8, i), io(7, i)): .Cells
(i + IniRow, 7) = IIf(.Cells(i + IniRow, 7) <> "", .Cells(i + IniRow, 7) / facto
r, .Cells(i + IniRow, 7))
Next i
End With
Exit Sub
Case 2
sql = CreaCaseQuery("Proclim", Period, location): io = Connect(fulldir, sq
l)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra l
os datos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
.Cells(i + IniRow, 5) = io(7, i): .Cells(i + IniRow, 5).NumberFormat
= "0.00"
.Cells(i + IniRow, 6) = io(8, i): .Cells(i + IniRow, 6).NumberFormat
= "0.00"
'.Cells(i + IniRow, 5) = IIf(io(8, i) <> "", io(8, i), io(6, i)): .Cells
(i + IniRow, 6) = IIf(.Cells(i + IniRow, 6) <> "", .Cells(i + IniRow, 6) / facto
r, .Cells(i + IniRow, 6))
'.Cells(i + IniRow, 6) = IIf(io(8, i) <> "", io(8, i), io(7, i)): .Cells
(i + IniRow, 7) = IIf(.Cells(i + IniRow, 7) <> "", .Cells(i + IniRow, 7) / facto
r, .Cells(i + IniRow, 7))
Next i
End With
Exit Sub
Case 3
sql = CreaCaseQuery("Proclim", Period, location): io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra l
os datos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
'.Cells(i + IniRow, 5) = IIf(io(8, i) <> "", io(8, i), io(6, i)): .Cells
(i + IniRow, 6) = IIf(.Cells(i + IniRow, 6) <> "", .Cells(i + IniRow, 6) / facto
r, .Cells(i + IniRow, 6))
'.Cells(i + IniRow, 6) = IIf(io(8, i) <> "", io(8, i), io(7, i)): .Cells
(i + IniRow, 7) = IIf(.Cells(i + IniRow, 7) <> "", .Cells(i + IniRow, 7) / facto
r, .Cells(i + IniRow, 7))
Next i
End With
Exit Sub

Case 4
sql = CreaCaseQuery("pur", Period, location, , basis, factor): io = Connect(f
ulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra l
os datos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
factor2 = 1
.Cells(i + IniRow, 5) = IIf(io(8, i) <> "", io(8, i), io(6, i)): .Cel
ls(i + IniRow, 5) = IIf(.Cells(i + IniRow, 5) <> "", .Cells(i + IniRow, 5) / fac
tor2, .Cells(i + IniRow, 5)): .Cells(i + IniRow, 5).NumberFormat = "0.00"
'si ahy un valor fijo coloc el valor fijo en el maximo
.Cells(i + IniRow, 6) = IIf(io(8, i) <> "", io(8, i), io(7, i)): .Cel
ls(i + IniRow, 6) = IIf(.Cells(i + IniRow, 6) <> "", .Cells(i + IniRow, 6) / fac
tor2, .Cells(i + IniRow, 6)): .Cells(i + IniRow, 6).NumberFormat = "0.00"
.Cells(i + IniRow, 7) = io(9, i): .Cells(i + IniRow, 7).NumberFormat
= "$#,##0.0"
Next i
End With
Exit Sub

Case 5
sql = CreaCaseQuery("oil", Period, "NUEVO TEAPA", , basis, factor): io = Con
nect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra l
os datos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
factor2 = 1
.Cells(i + IniRow, 5) = IIf(io(8, i) <> "", io(8, i), io(6, i)): .Cell
s(i + IniRow, 5) = IIf(.Cells(i + IniRow, 5) <> "", .Cells(i + IniRow, 5) / fact
or2, .Cells(i + IniRow, 5)): .Cells(i + IniRow, 5).NumberFormat = "0.00"
'si ahy un valor fijo coloc el valor fijo en el maximo
.Cells(i + IniRow, 6) = IIf(io(8, i) <> "", io(8, i), io(7, i)): .Cel
ls(i + IniRow, 6) = IIf(.Cells(i + IniRow, 6) <> "", .Cells(i + IniRow, 6) / fac
tor2, .Cells(i + IniRow, 6)): .Cells(i + IniRow, 6).NumberFormat = "0.00"
.Cells(i + IniRow, 7) = io(9, i): .Cells(i + IniRow, 7).NumberFormat
= "$#,##0.0"
Next i
End With
Exit Sub
Case 6
sql = CreaCaseQuery("salRef", Period, location, , , factor): io = Connect(fu
lldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra l
os datos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
factor2 = 1
.Cells(i + IniRow, 5) = IIf(io(7, i) <> "", io(7, i), io(5, i)): .Cell
s(i + IniRow, 5) = IIf(.Cells(i + IniRow, 5) <> "", .Cells(i + IniRow, 5) / fact
or2, .Cells(i + IniRow, 5)): .Cells(i + IniRow, 5).NumberFormat = "0.00"
'si ahy un valor fijo coloc el valor fijo en el maximo
.Cells(i + IniRow, 6) = IIf(io(7, i) <> "", io(7, i), io(6, i)): .Cel
ls(i + IniRow, 6) = IIf(.Cells(i + IniRow, 6) <> "", .Cells(i + IniRow, 6) / fac
tor2, .Cells(i + IniRow, 6)): .Cells(i + IniRow, 6).NumberFormat = "0.00"
.Cells(i + IniRow, 7) = io(9, i): .Cells(i + IniRow, 7).NumberFormat
= "$#,##0.0"
Next i
End With
Exit Sub
Case 7
sql = CreaCaseQuery("salTar", Period, location, , , factor): io = Connect(fu
lldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra l
os datos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
factor2 = 1
.Cells(i + IniRow, 5) = IIf(io(8, i) <> "", io(8, i), io(6, i)): .Cell
s(i + IniRow, 5) = IIf(.Cells(i + IniRow, 5) <> "", .Cells(i + IniRow, 5) / fact
or2, .Cells(i + IniRow, 5)): .Cells(i + IniRow, 5).NumberFormat = "0.00"
'si ahy un valor fijo coloc el valor fijo en el maximo
.Cells(i + IniRow, 6) = IIf(io(8, i) <> "", io(8, i), io(7, i)): .Cel
ls(i + IniRow, 6) = IIf(.Cells(i + IniRow, 6) <> "", .Cells(i + IniRow, 6) / fac
tor2, .Cells(i + IniRow, 6)): .Cells(i + IniRow, 6).NumberFormat = "0.00"
.Cells(i + IniRow, 7) = io(9, i): .Cells(i + IniRow, 7).NumberFormat = "
$#,##0.0"
Next i
End With
Exit Sub
Case 8
sql = CreaCaseQuery("Import", Period, location, , basis, factor): io = Connec
t(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra l
os datos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
factor2 = 1
.Cells(i + IniRow, 5) = IIf(io(8, i) <> "", io(8, i), io(6, i)): .Cel
ls(i + IniRow, 5) = IIf(.Cells(i + IniRow, 5) <> "", .Cells(i + IniRow, 5) / fac
tor2, .Cells(i + IniRow, 5)): .Cells(i + IniRow, 5).NumberFormat = "0.00"
'si ahy un valor fijo coloc el valor fijo en el maximo
.Cells(i + IniRow, 6) = IIf(io(8, i) <> "", io(8, i), io(7, i)): .Cel
ls(i + IniRow, 6) = IIf(.Cells(i + IniRow, 6) <> "", .Cells(i + IniRow, 6) / fac
tor2, .Cells(i + IniRow, 6)): .Cells(i + IniRow, 6).NumberFormat = "0.00"
.Cells(i + IniRow, 7) = io(9, i): .Cells(i + IniRow, 7).NumberFormat
= "$#,##0.0"
Next i
End With
Exit Sub

End Select

End If
End Sub
Sub loadAPlano()
If frmImportar.cmbSheets.text = Empty Then Exit Sub
Dim io, i, x
Dim caseName
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim sht As Worksheet: Set sht = ActiveSheet
Dim WS As Worksheet: Set WS = Worksheets("Input")
Output = MsgBox("La informacin para la locacin: '" & WS.Cells(4, 3) & "' sera almace
nada y podra remplazar la informacin anterior, desea continuar?", vbYesNo, "Creador
de casos"): If Output = 7 Then Exit Sub
caseName = Worksheets("Main").Cells(10, 4)
IniRow = 10: IniCol = 5
With sht
LastColumn = sht.Cells(IniRow - 1, sht.Columns.Count).End(xlToLeft).Column
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow >
IniRow, LastRow, IniRow)
.Range(.Cells(IniRow, IniCol), .Cells(LastRow, IniCol + 2)).ClearContents
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), caseName) Then Exit For
Next h
io = Pull_Data_from_Excel_with_ADODB(CStr(dir), frmImportar.cmbSheets.text,
1, 1, 2000, 2000)
For i = LBound(io, 2) To UBound(io, 2)
For x = IniRow To LastRow
If .Cells(6, 3) = "Capacidad" Then
If io(1, i) = .Cells(x, 4) And io(4, i) = .Cells(x, 2) Then
.Cells(x, h) = io(6, i): .Cells(x, h + 1) = io(7, i): .Cells(x, h +
2) = io(8, i)
End If
ElseIf .Cells(6, 3) = "Mantenimiento" Then
cases = UniqueArray(RangeToArray(Range(sht.Cells(IniRow - 2, IniCol)
, sht.Cells(IniRow - 2, LastColumn))))
If io(1, i) = .Cells(x, 4) And io(2, i) = .Cells(x, 1) Then
If Not IsNull(io(6, i)) Then mesi = StrConv(MonthName(Month(DateV
alue(io(6, i))), True), 3)
If Not IsNull(io(7, i)) Then mesf = StrConv(MonthName(Month(DateV
alue(io(7, i))), True), 3)
For h = LBound(cases) To UBound(cases) 'Revisa en que caso apli
caria el mantenimiento
If Not IsNull(io(6, i)) Then If InStr(cases(h), mesi) Then .
Cells(x, IniCol + (h * 2)) = io(6, i): .Cells(x, IniCol + (h * 2)).NumberFormat
= "dd-mm-yy;@"
If Not IsNull(io(7, i)) Then If InStr(cases(h), mesf) Then .
Cells(x, IniCol + (h * 2) + 1) = io(7, i): .Cells(x, IniCol + (h * 2) + 1).Numbe
rFormat = "dd-mm-yy;@"
Next h
End If
ElseIf .Cells(6, 3) = "Lim. de Proceso" Then
If io(1, i) = .Cells(x, 4) And io(3, i) = .Cells(x, 2) Then
.Cells(x, h) = io(5, i): .Cells(x, h + 1) = io(6, i)
End If

End If
Next x
Next i
End With
frmImportar.Hide
End Sub
Sub Cargar()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim sql
Dim fulldir As String
Dim location, cases
Dim rngOrigSelection As Range: Set rngOrigSelection = Selection
Dim rngOrigCell As Range: Set rngOrigCell = ActiveCell
Dim sOrigWS As String: sOrigWS = ActiveSheet.Name
Dim sOrigWB As String: sOrigWB = ActiveWorkbook.Name
Application.CutCopyMode = False
Workbooks(sOrigWB).Activate
Sheets(sOrigWS).Select
rngOrigSelection.Select
rngOrigCell.Activate
Application.ScreenUpdating = False
Set WM = Worksheets("Main"): Set WS = Worksheets("Input")
Output = MsgBox("La informacin para la locacin: '" & WS.Cells(4, 3) & "' sera almace
nada y podra remplazar la informacin anterior, desea continuar?", vbYesNo, "Creador
de casos"): If Output = 7 Then Exit Sub
IniRow = 10: IniCol = 5
With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
End With

If WS.Cells(6, 3) = "Capacidad" Then 'Si son capacidades


Set iWS = Worksheets("Units")
iIniRow = 2: iIniCol = 1
With iWS
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = I
If(iLastRow > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow - 1, iWS.Columns.Count).End(xlToLeft
).Column: iLastColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
cases = UniqueArray(RangeToArray(Range(iWS.Cells(iIniRow, 1), iWS.Ce
lls(iLastRow, 1))))
End With
If UBound(location) = 0 Then 'Si se carga por locacin
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Units", "B" & dom & ":B" & iL
astRow)
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row + LastR
ow, 10)).ClearContents 'Borra los datos anterioresd esde la file IniRow
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, I
niCol + (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row, 8)
).PasteSpecial Paste:=xlPasteValues
WS.Activate
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si se carga en su totalidad
iWS.Range(iWS.Cells(iIniRow, 8), iWS.Cells(iLastRow, 10)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
WS.Range(Cells(IniRow, IniCol + (i * 3)), Cells(LastRow, IniCol
+ (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(Cells(iIniRow + (i * (LastRow - IniRow)) + i, 8), Cell
s(iIniRow + (i * (LastRow - IniRow)) + i, 8)).PasteSpecial Paste:=xlPasteValues
WS.Activate
Next i
End If
ElseIf WS.Cells(6, 3) = "Lim. de Proceso" Then 'Si son capacidades
Set iWS = Worksheets("Proclim")
iIniRow = 2: iIniCol = 1
With iWS
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = I
If(iLastRow > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow - 1, iWS.Columns.Count).End(xlToLeft
).Column: iLastColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
cases = UniqueArray(RangeToArray(Range(iWS.Cells(iIniRow, 1), iWS.Ce
lls(iLastRow, 1))))
End With
If UBound(location) = 0 Then 'Si se carga por locacin
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Proclim", "B" & dom & ":B
" & iLastRow)
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row + L
astRow, 10)).ClearContents 'Borra los datos anterioresd esde la file IniRow
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRo
w, IniCol + (i * 2) + 2)).Copy
iWS.Activate
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row
, 8)).PasteSpecial Paste:=xlPasteValues
WS.Activate
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si se carga en su totalidad
iWS.Range(iWS.Cells(iIniRow, 7), iWS.Cells(iLastRow, 8)).ClearConten
ts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
WS.Range(Cells(IniRow, IniCol + (i * 2)), Cells(LastRow, IniCol
+ (i * 2) + 2)).Copy
iWS.Activate
iWS.Range(Cells(iIniRow + (i * (LastRow - IniRow)) + i, 7), Cell
s(iIniRow + (i * (LastRow - IniRow)) + i, 7)).PasteSpecial Paste:=xlPasteValues
WS.Activate
Next i
End If
ElseIf WS.Cells(6, 3) = "Mantenimiento" Then 'Si son capacidades
Set iWS = Worksheets("Units")
iIniRow = 2: iIniCol = 1
With iWS
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = I
If(iLastRow > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow - 1, iWS.Columns.Count).End(xlToLeft
).Column: iLastColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
cases = UniqueArray(RangeToArray(Range(iWS.Cells(iIniRow, 1), iWS.Ce
lls(iLastRow, 1))))
End With
If UBound(location) = 0 Then 'Si se carga por locacin
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Units", "B" & dom & ":B"
& iLastRow)
iWS.Range(iWS.Cells(Range(p).Row, 11), iWS.Cells(Range(p).Row +
LastRow, 12)).ClearContents 'Borra los datos anterioresd esde la file IniRow
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRo
w, IniCol + (i * 2) + 2)).Copy
iWS.Activate
iWS.Range(iWS.Cells(Range(p).Row, 11), iWS.Cells(Range(p).Ro
w, 11)).PasteSpecial Paste:=xlPasteValues
WS.Activate
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si se carga en su totalidad
iWS.Range(iWS.Cells(iIniRow, 11), iWS.Cells(iLastRow, 12)).ClearCont
ents 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
WS.Range(Cells(IniRow, IniCol + (i * 2)), Cells(LastRow, IniCol
+ (i * 2) + 1)).Copy
iWS.Activate
iWS.Range(Cells(iIniRow + (i * (LastRow - IniRow)) + i, 11), Cel
ls(iIniRow + (i * (LastRow - IniRow)) + i, 11)).PasteSpecial Paste:=xlPasteValue
s
WS.Activate
Next i
End If
ElseIf WS.Cells(6, 3) = "Compras" Then 'Si son capacidades
Set iWS = Worksheets("Purchases")
iIniRow = 2: iIniCol = 1
With iWS
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = I
If(iLastRow > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow - 1, iWS.Columns.Count).End(xlToLeft
).Column: iLastColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
cases = UniqueArray(RangeToArray(Range(iWS.Cells(iIniRow, 1), iWS.Ce
lls(iLastRow, 1))))
End With
If UBound(location) = 0 Then 'Si se carga por locacin
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Purchases", "B" & dom & "
:B" & iLastRow)
iWS.Range(iWS.Cells(Range(p).Row, 11), iWS.Cells(Range(p).Row +
LastRow, 12)).ClearContents 'Borra los datos anterioresd esde la file IniRow
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRo
w, IniCol + (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row
, 10)).PasteSpecial Paste:=xlPasteValues
WS.Activate
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si se carga en su totalidad
iWS.Range(iWS.Cells(iIniRow, 8), iWS.Cells(iLastRow, 11)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
WS.Range(Cells(IniRow, IniCol + (i * 3)), Cells(LastRow, IniCol
+ (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(Cells(iIniRow + (i * (LastRow - IniRow)) + i, 8), Cell
s(iIniRow + (i * (LastRow - IniRow)) + i, 8)).PasteSpecial Paste:=xlPasteValues
WS.Activate
Next i
End If
ElseIf WS.Cells(6, 3) = "Compras Crudo" Then 'Si son capacidades
Set iWS = Worksheets("Oil")
iIniRow = 2: iIniCol = 1
With iWS
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = I
If(iLastRow > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow - 1, iWS.Columns.Count).End(xlToLeft
).Column: iLastColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
cases = UniqueArray(RangeToArray(Range(iWS.Cells(iIniRow, 1), iWS.Ce
lls(iLastRow, 1))))
End With
'Solo esta Nuevo Teapa
iWS.Range(iWS.Cells(iIniRow, 7), iWS.Cells(iLastRow, 10)).ClearContents
'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
WS.Range(Cells(IniRow, IniCol + (i * 3)), Cells(LastRow, IniCol
+ (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(Cells(iIniRow + (i * (LastRow - IniRow)) + i, 7), Cell
s(iIniRow + (i * (LastRow - IniRow)) + i, 7)).PasteSpecial Paste:=xlPasteValues
WS.Activate
Next i

ElseIf WS.Cells(6, 3) = "Ventas en Ref." Then 'Si son capacidades


Set iWS = Worksheets("SalesRef")
iIniRow = 2: iIniCol = 1
With iWS
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = I
If(iLastRow > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow - 1, iWS.Columns.Count).End(xlToLeft
).Column: iLastColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
cases = UniqueArray(RangeToArray(Range(iWS.Cells(iIniRow, 1), iWS.Ce
lls(iLastRow, 1))))
End With
If UBound(location) = 0 Then 'Si se carga por locacin
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "SalesRef", "B" & dom & ":
B" & iLastRow)
iWS.Range(iWS.Cells(Range(p).Row, 11), iWS.Cells(Range(p).Row +
LastRow, 12)).ClearContents 'Borra los datos anterioresd esde la file IniRow
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRo
w, IniCol + (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row
, 10)).PasteSpecial Paste:=xlPasteValues
WS.Activate
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si se carga en su totalidad
iWS.Range(iWS.Cells(iIniRow, 8), iWS.Cells(iLastRow, 11)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
WS.Range(Cells(IniRow, IniCol + (i * 3)), Cells(LastRow, IniCol
+ (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(Cells(iIniRow + (i * (LastRow - IniRow)) + i, 8), Cell
s(iIniRow + (i * (LastRow - IniRow)) + i, 8)).PasteSpecial Paste:=xlPasteValues
WS.Activate
Next i
End If

ElseIf WS.Cells(6, 3) = "Ventas en TARs" Then 'Si son capacidades


Set iWS = Worksheets("SalesTar")
iIniRow = 2: iIniCol = 1
With iWS
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = I
If(iLastRow > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow - 1, iWS.Columns.Count).End(xlToLeft
).Column: iLastColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
cases = UniqueArray(RangeToArray(Range(iWS.Cells(iIniRow, 1), iWS.Ce
lls(iLastRow, 1))))
End With
If UBound(location) = 0 Then 'Si se carga por locacin
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "SalesTar", "B" & dom & ":
B" & iLastRow)
iWS.Range(iWS.Cells(Range(p).Row, 11), iWS.Cells(Range(p).Row +
LastRow, 12)).ClearContents 'Borra los datos anterioresd esde la file IniRow
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRo
w, IniCol + (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row
, 10)).PasteSpecial Paste:=xlPasteValues
WS.Activate
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si se carga en su totalidad
iWS.Range(iWS.Cells(iIniRow, 8), iWS.Cells(iLastRow, 11)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
WS.Range(Cells(IniRow, IniCol + (i * 3)), Cells(LastRow, IniCol
+ (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(Cells(iIniRow + (i * (LastRow - IniRow)) + i, 8), Cell
s(iIniRow + (i * (LastRow - IniRow)) + i, 8)).PasteSpecial Paste:=xlPasteValues
WS.Activate
Next i
End If
ElseIf WS.Cells(6, 3) = "Importaciones" Then 'Si son capacidades
Set iWS = Worksheets("Import")
iIniRow = 2: iIniCol = 1
With iWS
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = I
If(iLastRow > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow - 1, iWS.Columns.Count).End(xlToLeft
).Column: iLastColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
cases = UniqueArray(RangeToArray(Range(iWS.Cells(iIniRow, 1), iWS.Ce
lls(iLastRow, 1))))
End With
If UBound(location) = 0 Then 'Si se carga por locacin
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Import", "B" & dom & ":B"
& iLastRow)
iWS.Range(iWS.Cells(Range(p).Row, 11), iWS.Cells(Range(p).Row +
LastRow, 12)).ClearContents 'Borra los datos anterioresd esde la file IniRow
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRo
w, IniCol + (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row
, 10)).PasteSpecial Paste:=xlPasteValues
WS.Activate
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si se carga en su totalidad
iWS.Range(iWS.Cells(iIniRow, 8), iWS.Cells(iLastRow, 11)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
WS.Range(Cells(IniRow, IniCol + (i * 3)), Cells(LastRow, IniCol
+ (i * 3) + 2)).Copy
iWS.Activate
iWS.Range(Cells(iIniRow + (i * (LastRow - IniRow)) + i, 8), Cell
s(iIniRow + (i * (LastRow - IniRow)) + i, 8)).PasteSpecial Paste:=xlPasteValues
WS.Activate
Next i
End If
End If
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub CapUnitsTrans()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim fulldir As String
Dim location
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WS = Worksheets("Input"): Set iWS = Worksheets("Units"): Set WM = Worksheets
("Main")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > 5, LastColumn, 5)
fulldir = WM.Cells(9, 4)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
unit = IIf(WS.Cells(5, 3) <> "", WS.Cells(5, 3), "Todas")
Aux = IIf(WS.Cells(5, 3) <> "", WS.Cells(5, 3), "Todas")
If HasValidation(WS.Range("C5")) Then Aux = TrimElemArray(Split(WS.Range("C5").V
alidation.Formula1, ","))
WS.Cells(5, 3) = IIf(IsInArray(WS.Cells(5, 3), Aux), WS.Cells(5, 3), "Todas"
):

sql = CreaCaseQuery("Caps", Period, location, , , factor, unit):


Call activeRef:
io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Bor
ra los datos anterioresd esde la file IniRow
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), io(6, 0)) Then Exit For
Next h
h = IIf(h > LastColumn, IniCol, h)
For i = LBound(io, 2) To UBound(io, 2)
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With

'Transfiere la informacin de la hoja "Units"


With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
cases = UniqueArray(RangeToArray(Range(WS.Cells(IniRow - 2, IniCol), WS.Cell
s(IniRow - 2, LastColumn))))
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = IIf(iLast
Row > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow, iWS.Columns.Count).End(xlToLeft).Column: iL
astColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
End With
If UBound(location) = 0 Then 'Si es una sola locacin
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Units", "B" & dom & ":B" & iLastRo
w)
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row + (LastRow -
IniRow), 9)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(IniRow, IniCol + (i
* 2) + 2)).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRow, IniCol + (i
* 2) + 1)).NumberFormat = "0.00"
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si son todas las locaciones
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
iWS.Range(iWS.Cells(iIniRow + ((LastRow - IniRow) * i) + i, 8), iWS.Cells(iI
niRow + ((LastRow - IniRow) * i) + (LastRow - IniRow) + i, 9)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (2 * i)), WS.Cells(IniRow, IniCol + (2 *
i))).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRow, IniCol + (i *
2) + 1)).NumberFormat = "0.00"
On Local Error Resume Next
iWS.Activate
Next i
End If
WS.Select
WS.Cells(4, 3).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub ProclimTrans()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim fulldir As String
Dim location
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WS = Worksheets("Input"): Set iWS = Worksheets("Proclim"): Set WM = Workshee
ts("Main")
location = WS.Cells(3, 8)
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > 5, LastColumn, 5)
fulldir = WM.Cells(9, 4)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
sql = CreaCaseQuery("Proclim", Period, location):
Call activeRef:
io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Bor
ra los datos anterioresd esde la file IniRow
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), io(6, 0)) Then Exit For
Next h
h = IIf(h > LastColumn, IniCol, h)
For i = LBound(io, 2) To UBound(io, 2)
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With

'Transfiere la informacin de la hoja "Units"


With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
cases = UniqueArray(RangeToArray(Range(WS.Cells(IniRow - 2, IniCol), WS.Cell
s(IniRow - 2, LastColumn))))
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = IIf(iLast
Row > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow, iWS.Columns.Count).End(xlToLeft).Column: iL
astColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
End With
If UBound(location) = 0 Then 'Si es una sola locacin
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Proclim", "B" & dom & ":B" & iLast
Row)
iWS.Range(iWS.Cells(Range(p).Row, 7), iWS.Cells(Range(p).Row + (LastRow -
IniRow), 8)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(IniRow, IniCol + (i
* 2) + 2)).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRow, IniCol + (i
* 2) + 1)).NumberFormat = "0.00"
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si son todas las locaciones
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
iWS.Range(iWS.Cells(iIniRow + ((LastRow - IniRow) * i) + i, 7), iWS.Cells(iI
niRow + ((LastRow - IniRow) * i) + (LastRow - IniRow) + i, 8)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (2 * i)), WS.Cells(IniRow, IniCol + (2 *
i))).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRow, IniCol + (i *
2) + 1)).NumberFormat = "0.00"
On Local Error Resume Next
iWS.Activate
Next i
End If

WS.Select
WS.Cells(4, 3).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub SalesRefTrans()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim fulldir As String
Dim location
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WS = Worksheets("Input"): Set iWS = Worksheets("SalesRef"): Set WM = Workshe
ets("Main")
location = WS.Cells(3, 8)
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > 5, LastColumn, 5)
fulldir = WM.Cells(9, 4)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
account = IIf(WS.Cells(5, 3) <> "", WS.Cells(5, 3), "Todas")

sql = CreaCaseQuery("salRef", Period, location, , , factor, account): Call activ


eRef: io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Bor
ra los datos anterioresd esde la file IniRow
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), io(6, 0)) Then Exit For
Next h
h = IIf(h > LastColumn, IniCol, h)
For i = LBound(io, 2) To UBound(io, 2)
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With

'Transfiere la informacin de la hoja "Units"


With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
cases = UniqueArray(RangeToArray(Range(WS.Cells(IniRow - 2, IniCol), WS.Cell
s(IniRow - 2, LastColumn))))
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = IIf(iLast
Row > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow, iWS.Columns.Count).End(xlToLeft).Column: iL
astColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
End With
If UBound(location) = 0 Then 'Si es una sola locacin
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "SalesRef", "B" & dom & ":B" & iLas
tRow)
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row + (LastRow -
IniRow), 10)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(IniRow, IniCol + (i
* 3) + 2)).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, IniCol + (i
* 3) + 1)).NumberFormat = "0.00"
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si son todas las locaciones
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
iWS.Range(iWS.Cells(iIniRow + ((LastRow - IniRow) * i) + i, 8), iWS.Cells(iI
niRow + ((LastRow - IniRow) * i) + (LastRow - IniRow) + i, 10)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (3 * i)), WS.Cells(IniRow, IniCol + (3 *
i))).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, IniCol + (i *
3) + 1)).NumberFormat = "0.00"
On Local Error Resume Next
iWS.Activate
Next i
End If
WS.Select
WS.Cells(4, 3).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub PurchasesTrans()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim fulldir As String
Dim location
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WS = Worksheets("Input"): Set iWS = Worksheets("Purchases"): Set WM = Worksh
eets("Main")
location = WS.Cells(3, 8)
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > 5, LastColumn, 5)
fulldir = WM.Cells(9, 4)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
sql = CreaCaseQuery("pur", Period, location, , , factor):
Call activeRef:
io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Bor
ra los datos anterioresd esde la file IniRow
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), io(6, 0)) Then Exit For
Next h
h = IIf(h > LastColumn, IniCol, h)
For i = LBound(io, 2) To UBound(io, 2)
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
'Transfiere la informacin de la hoja "Units"
With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
cases = UniqueArray(RangeToArray(Range(WS.Cells(IniRow - 2, IniCol), WS.Cell
s(IniRow - 2, LastColumn))))
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = IIf(iLast
Row > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow, iWS.Columns.Count).End(xlToLeft).Column: iL
astColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
End With
If UBound(location) = 0 Then 'Si es una sola locacin
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Purchases", "B" & dom & ":B" & iLa
stRow)
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row + (LastRow -
IniRow), 10)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(IniRow, IniCol + (i
* 3) + 2)).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, IniCol + (i
* 3) + 1)).NumberFormat = "0.00"
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si son todas las locaciones
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
iWS.Range(iWS.Cells(iIniRow + ((LastRow - IniRow) * i) + i, 8), iWS.Cells(iI
niRow + ((LastRow - IniRow) * i) + (LastRow - IniRow) + i, 10)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (3 * i)), WS.Cells(IniRow, IniCol + (3 *
i))).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, IniCol + (i *
3) + 1)).NumberFormat = "0.00"
iWS.Activate
Next i
End If

WS.Select
WS.Cells(4, 3).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub ImportTrans()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim fulldir As String
Dim location
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WS = Worksheets("Input"): Set iWS = Worksheets("Import"): Set WM = Worksheet
s("Main")
location = WS.Cells(3, 8)
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > 5, LastColumn, 5)
fulldir = WM.Cells(9, 4)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
basis = "_" & WM.Cells(11, 11)
sql = CreaCaseQuery("Import", Period, location, , basis, factor):
Call activeRef:
io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Bor
ra los datos anterioresd esde la file IniRow
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), io(6, 0)) Then Exit For
Next h
h = IIf(h > LastColumn, IniCol, h)
For i = LBound(io, 2) To UBound(io, 2)
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
'Transfiere la informacin de la hoja "Units"
With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
cases = UniqueArray(RangeToArray(Range(WS.Cells(IniRow - 2, IniCol), WS.Cell
s(IniRow - 2, LastColumn))))
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = IIf(iLast
Row > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow, iWS.Columns.Count).End(xlToLeft).Column: iL
astColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
End With
If UBound(location) = 0 Then 'Si es una sola locacin
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Import", "B" & dom & ":B" & iLastR
ow)
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row + (LastRow -
IniRow), 10)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(IniRow, IniCol + (i
* 3) + 2)).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, IniCol + (i
* 3) + 1)).NumberFormat = "0.00"
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si son todas las locaciones
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
iWS.Range(iWS.Cells(iIniRow + ((LastRow - IniRow) * i) + i, 8), iWS.Cells(iI
niRow + ((LastRow - IniRow) * i) + (LastRow - IniRow) + i, 10)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (3 * i)), WS.Cells(IniRow, IniCol + (3 *
i))).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, IniCol + (i *
3) + 1)).NumberFormat = "0.00"
iWS.Activate
Next i
End If

WS.Select
WS.Cells(4, 3).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub SalesTarTrans()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim fulldir As String
Dim location
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WS = Worksheets("Input"): Set iWS = Worksheets("SalesTar"): Set WM = Workshe
ets("Main")
location = WS.Cells(3, 8)
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > 5, LastColumn, 5)
fulldir = WM.Cells(9, 4)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
sql = CreaCaseQuery("salTar", Period, location, , , factor):
Call activeRef:
io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Bor
ra los datos anterioresd esde la file IniRow
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), io(6, 0)) Then Exit For
Next h
h = IIf(h > LastColumn, IniCol, h)
For i = LBound(io, 2) To UBound(io, 2)
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With

'Transfiere la informacin de la hoja "Units"


With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
cases = UniqueArray(RangeToArray(Range(WS.Cells(IniRow - 2, IniCol), WS.Cell
s(IniRow - 2, LastColumn))))
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = IIf(iLast
Row > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow, iWS.Columns.Count).End(xlToLeft).Column: iL
astColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
End With
If UBound(location) = 0 Then 'Si es una sola locacin
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "SalesTar", "B" & dom & ":B" & iLas
tRow)
iWS.Range(iWS.Cells(Range(p).Row, 8), iWS.Cells(Range(p).Row + (LastRow -
IniRow), 10)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(IniRow, IniCol + (i
* 3) + 2)).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, IniCol + (i
* 3) + 1)).NumberFormat = "0.00"
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si son todas las locaciones
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
iWS.Range(iWS.Cells(iIniRow + ((LastRow - IniRow) * i) + i, 8), iWS.Cells(iI
niRow + ((LastRow - IniRow) * i) + (LastRow - IniRow) + i, 10)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (3 * i)), WS.Cells(IniRow, IniCol + (3 *
i))).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 3)), WS.Cells(LastRow, IniCol + (i *
3) + 1)).NumberFormat = "0.00"
On Local Error Resume Next
iWS.Activate
Next i
End If

WS.Select
WS.Cells(4, 3).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub ShowMenu()
frmMenu.Show
End Sub
Sub Proclim()
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim loc As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WM = Worksheets("Main"): Set WS = Worksheets("Input"): Set iWS = Worksheets
("Proclim")
basis = "_" & WM.Cells(11, 11)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)
'Drills de seleccion
Call removeBordes("Input", "C5")
On Local Error Resume Next
WS.Cells(4, 3) = IIf(IsInArray(WS.Cells(4, 3), loc), WS.Cells(4, 3), "Todas"):
Call Cargalocacion(fulldir, "1", "Input", 4, 3)
With WS
.Select
.Cells(6, 3) = "Lim. de Proceso"
.Cells(8, 2) = "Limite"
.Range(Cells(IniRow - 2, 5), Cells(IniRow - 1, LastColumn)).Select
With Selection
.Clear
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
'Crea las columnasd de los casos
io = TrimElemArray(Split(Range("C3").Validation.Formula1, ",")) 'Extrae los cas
os de la lista
For i = LBound(io) To UBound(io)
Range(Cells(8, 7 + (2 * (i - 1))), Cells(8, 8 + (2 * (i - 1)))).Merge
Cells(8, 7 + (2 * (i - 1))) = io(i)
Cells(9, 7 + (2 * (i - 1))) = "MIN": Cells(9, 8 + (2 * (i - 1))) = "MAX":

With Range(Cells(8, 7 + (2 * (i - 1))), Cells(9, 8 + (2 * (i - 1))))


Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
Next i
Call activeRef: sql = CreaCaseQuery("Proclim", Period, location, , , factor): i
o = Connect(fulldir, sql)
With WS
With .Cells(5, 2)
.Font.Bold = True
.HorizontalAlignment = xlRight
.Value = "Planta:"
End With
Call setBordes("Input", "C5")
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra los d
atos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub MantoUnitsTrans()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim fulldir As String
Dim location
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WS = Worksheets("Input"): Set iWS = Worksheets("Units"): Set WM = Worksheets
("Main")
location = WS.Cells(3, 8)
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > 5, LastColumn, 5)
fulldir = WM.Cells(9, 4)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
sql = CreaCaseQuery("Caps", Period, location, , , factor):
Call activeRef:
io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Bor
ra los datos anterioresd esde la file IniRow
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), io(6, 0)) Then Exit For
Next h
h = IIf(h > LastColumn, IniCol, h)
For i = LBound(io, 2) To UBound(io, 2)
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With

'Transfiere la informacin de la hoja "Units"


With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
cases = UniqueArray(RangeToArray(Range(WS.Cells(IniRow - 2, IniCol), WS.Cell
s(IniRow - 2, LastColumn))))
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = IIf(iLast
Row > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow, iWS.Columns.Count).End(xlToLeft).Column: iL
astColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
End With
If UBound(location) = 0 Then 'Si es una sola locacin
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
dom = iIniRow
For i = LBound(cases) To UBound(cases)
p = FindX(location(UBound(location)), "Units", "B" & dom & ":B" & iLastRo
w)
iWS.Range(iWS.Cells(Range(p).Row, 11), iWS.Cells(Range(p).Row + (LastRow
- IniRow), 12)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(IniRow, IniCol + (i
* 2) + 2)).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRow, IniCol + (i
* 2) + 1)).NumberFormat = "dd-mm-yy;@"
dom = Range(p).Row + (LastRow - IniRow + 1)
Next i
Else 'Si son todas las locaciones
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
iWS.Range(iWS.Cells(iIniRow + ((LastRow - IniRow) * i) + i, 11), iWS.Cells(i
IniRow + ((LastRow - IniRow) * i) + (LastRow - IniRow) + i, 12)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (2 * i)), WS.Cells(IniRow, IniCol + (2 *
i))).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRow, IniCol + (i *
2) + 1)).NumberFormat = "dd-mm-yy;@"
On Local Error Resume Next
iWS.Activate
Next i
End If
WS.Select
WS.Cells(4, 3).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub Purchases_Sales(Optional ByVal ps, Optional ByVal A)
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim loc As Variant
Dim AC, AD As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
A = IIf(IsMissing(A), 0, 1)

Set WM = Worksheets("Main"): Set WS = Worksheets("Input")


If ps = "P" Then Set iWS = Worksheets("Purchases")
If ps = "S" Then Set iWS = Worksheets("Sales")

basis = "_" & WM.Cells(11, 11)


factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)
'Drills de seleccion
Call setBordes("Input", "C5")
On Local Error Resume Next
WS.Cells(4, 3) = IIf(IsInArray(WS.Cells(4, 3), loc), WS.Cells(4, 3), "Todas"): C
all Cargalocacion(fulldir, "1", "Input", 4, 3)
With WS
.Select
If ps = "P" Then .Cells(6, 3) = "Compras"
If ps = "S" Then .Cells(6, 3) = "Ventas"
.Cells(8, 2) = "Cuentas"
.Range(Cells(IniRow - 2, 5), Cells(IniRow - 1, LastColumn)).Select
With Selection
.Clear
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
'Crea las columnasd de los casos
io = TrimElemArray(Split(Range("C3").Validation.Formula1, ",")) 'Extrae los cas
os de la lista
For i = LBound(io) To UBound(io)
Range(Cells(8, 8 + (3 * (i - 1))), Cells(8, 10 + (3 * (i - 1)))).Merge
Cells(8, 8 + (3 * (i - 1))) = io(i)
Cells(9, 8 + (3 * (i - 1))) = "MIN": Cells(9, 9 + (3 * (i - 1))) = "MAX": Cells(
9, 10 + (3 * (i - 1))) = "Precio"
With Range(Cells(8, 9 + (3 * (i - 1))), Cells(9, 11 + (3 * (i - 1))))
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
Next i
If ps = "P" Then Call activeRef: sql = CreaCaseQuery("pur", Period, location, ,
basis, factor, A): io = Connect(fulldir, sql)
'If ps = "S" Then Call activeRef: sql = CreaCaseQuery("Caps", Period, location,
, , factor): io = Connect(fulldir, sql)

With WS
With .Cells(5, 2)
.Font.Bold = True
.HorizontalAlignment = xlRight
.Value = "Cuentas:"
End With
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra los d
atos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub Oil()
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim loc As Variant
Dim AC, AD As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
A = IIf(IsMissing(A), 0, 1)
Set WM = Worksheets("Main"): Set WS = Worksheets("Input"): Set iWS = Worksheets
("Oil")

basis = "_" & WM.Cells(11, 11)


factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)
'Drills de seleccion
Call removeBordes("Input", "C5")
On Local Error Resume Next
WS.Cells(4, 3) = IIf(IsInArray(WS.Cells(4, 3), loc), WS.Cells(4, 3), "Todas"): C
all Cargalocacion(fulldir, "0", "Input", 4, 3)

With WS
.Select
.Cells(6, 3) = "Compras Crudo"
.Cells(8, 2) = "Cuentas"
.Range(Cells(IniRow - 2, 5), Cells(IniRow - 1, LastColumn)).Select
With Selection
.Clear
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
'Crea las columnasd de los casos
io = TrimElemArray(Split(Range("C3").Validation.Formula1, ",")) 'Extrae los cas
os de la lista
For i = LBound(io) To UBound(io)
Range(Cells(8, 8 + (3 * (i - 1))), Cells(8, 10 + (3 * (i - 1)))).Merge
Cells(8, 8 + (3 * (i - 1))) = io(i)
Cells(9, 8 + (3 * (i - 1))) = "MIN": Cells(9, 9 + (3 * (i - 1))) = "MAX": Cells(
9, 10 + (3 * (i - 1))) = "Precio"
With Range(Cells(8, 9 + (3 * (i - 1))), Cells(9, 11 + (3 * (i - 1))))
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
Next i
Call activeRef: sql = CreaCaseQuery("oil", Period, "NUEVO TEAPA", , , factor,
A): io = Connect(fulldir, sql)

With WS
With .Cells(5, 2)
.Font.Bold = False
.HorizontalAlignment = xlRight
.Value = ""
End With
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra los d
atos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
'With WS.Cells(4, 3).Validation
' .Delete
' .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
' xlBetween, Formula1:="NUEVO TEAPA"
' .IgnoreBlank = True
' .InCellDropdown = True
' .InputTitle = ""
' .ErrorTitle = ""
' .InputMessage = ""
' .ErrorMessage = ""
' .ShowInput = True
' .ShowError = True
'End With
' WS.Cells(4, 3) = "NUEVO TEAPA"

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub OilTrans()
Dim WM, WS, iWS As Worksheet
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim fulldir As String
Dim location
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WS = Worksheets("Input"): Set iWS = Worksheets("Oil"): Set WM = Worksheets("
Main")
location = WS.Cells(3, 8)
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > 5, LastColumn, 5)
fulldir = WM.Cells(9, 4)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
'location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
location = "NUEVO TEAPA"
sql = CreaCaseQuery("oil", Period, location):
Call activeRef:
io = Connect(fulldir, sql)
With WS
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Bor
ra los datos anterioresd esde la file IniRow
For h = IniCol To LastColumn
If InStr(.Cells(IniRow - 2, h), io(6, 0)) Then Exit For
Next h
h = IIf(h > LastColumn, IniCol, h)
For i = LBound(io, 2) To UBound(io, 2)
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With

'Transfiere la informacin de la hoja "Units"


With WS
LastRow = WS.Cells(WS.Rows.Count, "D").End(xlUp).Row: LastRow = IIf(LastRow
> IniRow, LastRow, IniRow)
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column: La
stColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
location = UniqueArray(RangeToArray(Range(WS.Cells(IniRow, 4), WS.Cells(Last
Row, 4))))
cases = UniqueArray(RangeToArray(Range(WS.Cells(IniRow - 2, IniCol), WS.Cell
s(IniRow - 2, LastColumn))))
iLastRow = iWS.Cells(WS.Rows.Count, "D").End(xlUp).Row: iLastRow = IIf(iLast
Row > iIniRow, iLastRow, iIniRow)
iLastColumn = iWS.Cells(iIniRow, iWS.Columns.Count).End(xlToLeft).Column: iL
astColumn = IIf(iLastColumn > iIniCol, iLastColumn, iIniCol)
End With
'Solo es Nuevo Teapa
WS.Range(WS.Cells(IniRow, IniCol), WS.Cells(LastRow, LastColumn)).ClearConte
nts 'Borra los datos anterioresd esde la file IniRow
For i = LBound(cases) To UBound(cases)
iWS.Range(iWS.Cells(iIniRow + ((LastRow - IniRow) * i) + i, 7), iWS.Cells(iI
niRow + ((LastRow - IniRow) * i) + (LastRow - IniRow) + i, 9)).Copy
WS.Activate
WS.Range(WS.Cells(IniRow, IniCol + (2 * i) + i), WS.Cells(IniRow, IniCol + (
2 * i) + i)).PasteSpecial Paste:=xlPasteValues
'Formatea las celdas
'WS.Range(WS.Cells(IniRow, IniCol + (i * 2)), WS.Cells(LastRow, IniCol + (i
* 2) + 1)).NumberFormat = "0.00"
On Local Error Resume Next
iWS.Activate
Next i

WS.Select
WS.Cells(4, 3).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Sub SalesRef()
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim AC, AD As Variant
Dim loc As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
A = IIf(IsMissing(A), 0, 1)

Set WM = Worksheets("Main"): Set WS = Worksheets("Input"): Set iWS = Worksheets


("SalesRef")

basis = "_" & WM.Cells(11, 11)


factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)
'Drills de seleccion
setBordes ("C5")
On Local Error Resume Next
WS.Cells(4, 3) = IIf(IsInArray(WS.Cells(4, 3), loc), WS.Cells(4, 3), "Todas"): C
all Cargalocacion(fulldir, "1", "Input", 4, 3)
WS.Cells(5, 3) = IIf(IsInArray(WS.Cells(5, 3) Or WS.Cells(5, 3) = "", location),
WS.Cells(5, 3), "Todas"): Call Cargacuentas(fulldir, location, "Input", 5, 3)
With WS
.Select
.Cells(6, 3) = "Ventas en Ref."
.Cells(8, 2) = "Cuentas"
.Range(Cells(IniRow - 2, 5), Cells(IniRow - 1, LastColumn)).Select
With Selection
.Clear
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
'Crea las columnasd de los casos
io = TrimElemArray(Split(Range("C3").Validation.Formula1, ",")) 'Extrae los cas
os de la lista
For i = LBound(io) To UBound(io)
Range(Cells(8, 8 + (3 * (i - 1))), Cells(8, 10 + (3 * (i - 1)))).Merge
Cells(8, 8 + (3 * (i - 1))) = io(i)
Cells(9, 8 + (3 * (i - 1))) = "MIN": Cells(9, 9 + (3 * (i - 1))) = "MAX": Cells(
9, 10 + (3 * (i - 1))) = "Precio"
With Range(Cells(8, 9 + (3 * (i - 1))), Cells(9, 11 + (3 * (i - 1))))
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
Next i
Call activeRef: sql = CreaCaseQuery("salRef", Period, , , , factor, A): io = Con
nect(fulldir, sql)

With WS
With .Cells(5, 2)
.Font.Bold = True
.HorizontalAlignment = xlRight
.Value = "Cuentas:"
End With
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra los d
atos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Sub SalesTar()
Dim WM, WS, iWS As Worksheet
Dim fulldir As String
Dim LastColumn, IniCol As Long
Dim LastRow, IniRow As Long
Dim iLastColumn, iIniCol As Long
Dim iLastRow, iIniRow As Long
Dim AC, AD As Variant
Dim loc As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
A = IIf(IsMissing(A), 0, 1)
Set WM = Worksheets("Main"): Set WS = Worksheets("Input"): Set iWS = Worksheets
("SalesTar")
basis = "_" & WM.Cells(11, 11)
factor = "/" & IIf(WM.Cells(11, 14) = "Mbd" Or WM.Cells(11, 14) = "Mtd", 1000, 1
)
Period = IIf(WM.Cells(10, 11) <> "", WM.Cells(10, 11), "")
location = IIf(WS.Cells(4, 3) <> "", WS.Cells(4, 3), "Todas")
IniRow = 10: IniCol = 5
iIniRow = 2: iIniCol = 1
LastColumn = WS.Cells(IniRow - 1, WS.Columns.Count).End(xlToLeft).Column
LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row: LastRow = IIf(LastRow > In
iRow, LastRow, IniRow)
LastColumn = IIf(LastColumn > IniCol, LastColumn, IniCol)
fulldir = WM.Cells(9, 4)
'Drills de seleccion
setBordes ("C5")
On Local Error Resume Next
WS.Cells(4, 3) = IIf(IsInArray(WS.Cells(4, 3), loc), WS.Cells(4, 3), "Todas"): C
all Cargalocacion(fulldir, "2", "Input", 4, 3)

With WS
.Select
.Cells(6, 3) = "Ventas en TARs"
.Cells(8, 2) = "Cuentas"
.Range(Cells(IniRow - 2, 5), Cells(IniRow - 1, LastColumn)).Select
With Selection
.Clear
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
'Crea las columnasd de los casos
io = TrimElemArray(Split(Range("C3").Validation.Formula1, ",")) 'Extrae los cas
os de la lista
For i = LBound(io) To UBound(io)
Range(Cells(8, 8 + (3 * (i - 1))), Cells(8, 10 + (3 * (i - 1)))).Merge
Cells(8, 8 + (3 * (i - 1))) = io(i)
Cells(9, 8 + (3 * (i - 1))) = "MIN": Cells(9, 9 + (3 * (i - 1))) = "MAX": Cells(
9, 10 + (3 * (i - 1))) = "Precio"
With Range(Cells(8, 9 + (3 * (i - 1))), Cells(9, 11 + (3 * (i - 1))))
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
Next i
Call activeRef: sql = CreaCaseQuery("salTar", Period, , , , factor, A): io = Con
nect(fulldir, sql)

With WS
With .Cells(5, 2)
.Font.Bold = True
.HorizontalAlignment = xlRight
.Value = "Cuentas:"
End With
.Range(Cells(IniRow, 1), Cells(LastRow, LastColumn)).ClearContents 'Borra los d
atos anterioresd esde la file IniRow
For i = LBound(io, 2) To UBound(io, 2):
.Cells(i + IniRow, 1) = io(1, i):
.Cells(i + IniRow, 2) = io(2, i):
.Cells(i + IniRow, 3) = io(3, i):
.Cells(i + IniRow, 4) = io(4, i):
Next i
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Function Mlocations(ByVal fulldir As String, iloc)
'Coloca las locaciones desplegables
Dim sql(), io
Dim loc As Variant
iloc = TrimElemArray(Split(iloc, ","))
ReDim sql(0)
sql(0) = "SELECT DISTINCT [Location.Name], [Location.Description] "
'FROM
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "FROM Location"
If IsInArray(1, iloc) Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql))
= ",vUnitMaterialBalance "
'WHERE
If IsInArray(0, iloc) Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql))
= IIf(IsInArray("WHERE", sql), " AND", " WHERE") & " Location.Description LIKE
'%TEAPA%' " 'Compra de crudos
If IsInArray(1, iloc) Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql))
= IIf(IsInArray("WHERE", sql), " AND", " WHERE") & " Location.LocationCode=vUni
tMaterialBalance.LocationCode " 'Centros de trabajo
If IsInArray(2, iloc) Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql))
= IIf(IsInArray("WHERE", sql), " OR", " WHERE") & " Location.Description LIKE '
%TAR%' " 'Tars
If IsInArray(3, iloc) Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql))
= IIf(IsInArray("WHERE", sql), " OR", " WHERE") & " Location.Description LIKE '
IMP%' " 'Importaciones
If IsInArray(4, iloc) Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql))
= IIf(IsInArray("WHERE", sql), " OR", " WHERE") & " Location.Description LIKE '
EXP%' " 'Exportacion
'ORDER BY
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = " ORDER BY [Location.Nam
e] ASC, [Location.Description] ASC"
Call activeRef: io = Connect(fulldir, sql): ReDim loc(UBound(io, 2) + 1): Fo
r i = LBound(io, 2) To UBound(io, 2): loc(i) = io(2, i): Next i: loc(UBound(loc)
) = "Todas"
Mlocations = loc
End Function
Function MCuentas(ByVal fulldir As String, Optional ByVal loc)
Dim sql(), io
Dim act As Variant
loc = IIf(IsMissing(loc), "Todas", loc)
ReDim sql(0)
sql(0) = "SELECT DISTINCT [Cost_Account.Name], [Cost_Account.Description] "
'FROM
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "FROM Cost_Account, Loca
tion, Purchase_Sale "
'WHERE
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = " WHERE Location.Locatio
nCode=Location.LocationCode"
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = " AND Purchase_Sale.Acco
unt=Cost_Account.Account "
If loc <> "Todas" Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "
AND Location.Description LIKE '" & loc & "'"
'ORDER BY
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = " ORDER BY [Cost_Account
.Description] ASC"
Call activeRef: io = Connect(fulldir, sql): ReDim act(UBound(io, 2) + 1): Fo
r i = LBound(io, 2) To UBound(io, 2): act(i) = io(2, i): Next i: act(UBound(act)
) = "Todas"
MCuentas = act
End Function
Function MUnidades(ByVal fulldir As String, Optional ByVal loc, Optional ByVal i
uni)
Dim sql(), io
Dim uni As Variant
loc = IIf(IsMissing(loc), "Todas", loc)
iuni = IIf(IsMissing(iuni), "Todas", iuni)
ReDim sql(0)
sql(0) = "SELECT DISTINCT [Process_Unit.Name]+'-'+[Process_Unit.ShortCode], [Pro
cess_Unit.Description] "
'FROM
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "FROM Process_Unit, vUni
tMaterialBalance,Location "
'WHERE
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = " WHERE vUnitMaterialBal
ance.UnitCode=Process_Unit.UnitCode"
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = " AND vUnitMaterialBalan
ce.LocationCode=vUnitMaterialBalance.LocationCode "
If loc <> "Todas" Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = "
AND Location.Description LIKE '" & loc & "'"
If iuni <> "Todas" Then ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) =
" AND Process_Unit.Description LIKE '" & iuni & "'"
'ORDER BY
ReDim Preserve sql(UBound(sql) + 1): sql(UBound(sql)) = " ORDER BY [Process_Unit
.Description] ASC"
Call activeRef: io = Connect(fulldir, sql): ReDim uni(UBound(io, 2) + 1): Fo
r i = LBound(io, 2) To UBound(io, 2): uni(i) = io(2, i): Next i: uni(UBound(uni)
) = "Todas"
MUnidades = uni
End Function
Sub test()
p = MUnidades("D:\Datos_Perfil\558806\Desktop\Formatos\Base_CaseAbrBase.mdb", "R
ef. Madero")
End Sub
Sub Cargalocacion(fulldir As Variant, ins, sht, r, c)
'Coloca las locaciones desplegables
Dim loc As Variant
loc = AlphaSort(Mlocations(fulldir, ins), 0)

'Coloca el nombre de las locaciones desplegado


With Worksheets(sht).Cells(r, c).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(loc, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
Sub Cargacuentas(fulldir As Variant, Optional ByVal act, Optional ByVal sht, Opt
ional ByVal r, Optional ByVal c)
'fulldir As Variant, Optional ByVal act, sht, r, c
'Coloca las locaciones desplegables
Dim loc As Variant
act = UniqueArray(AlphaSort(MCuentas(fulldir, act), 0))

'Coloca el nombre de las locaciones desplegado


With Worksheets(sht).Cells(r, c).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(act, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
Sub Cargaunidades(fulldir As Variant, ins, sht, r, c)
'Coloca las locaciones desplegables
Dim loc As Variant
loc = UniqueArray(AlphaSort(MUnidades(fulldir, ins), 0))
'Coloca el nombre de las locaciones desplegado
With Worksheets(sht).Cells(r, c).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(loc, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
Sub Cargaunits(fulldir As Variant, Optional ByVal act, Optional ByVal sht, Optio
nal ByVal r, Optional ByVal c)
'fulldir As Variant, Optional ByVal act, sht, r, c
'Coloca las locaciones desplegables
Dim loc As Variant
act = UniqueArray(AlphaSort(MUnidades(fulldir, act), 0))

'Coloca el nombre de las locaciones desplegado


With Worksheets(sht).Cells(r, c).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(act, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub

Sub setBordes(sht, cel)


With Worksheets(sht).Range(cel)
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
End Sub
Sub removeBordes(sht, cel)
With Worksheets(sht).Range(cel)
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
'.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
'.Borders(xlInsideHorizontal).LineStyle = xlNone
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
'.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
End Sub
Sub Otras_P()
Call Purchases_Sales("P")
End Sub

S-ar putea să vă placă și