Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Sub MRC()

MsgBox "Ingrese los archivos de Entregas y Retiros,CMg y Rentas COES del Mes"
Dim book As Object
Application.ScreenUpdating = False
'Definir la variable como tipo Variante
Dim X As Variant
'Abrir cuadro de dialogo
X = Application.GetOpenFilename _
("Excel Files (*.xlsx*,*.xlsm*), *.xlsx*,*.xlsm*", 2, "Abrir
archivos", , True)
'Validar si se seleccionaron archivos
If IsArray(X) Then ' Si se seleccionan
For i = LBound(X) To UBound(X)
Workbooks.Open X(i)

If Left(ActiveWorkbook.Name, 3) = "EyR" Then


ActiveWorkbook.Name = "EyR"

'Cuenta el numero de columnas


Dim celda1 As Variant
Dim pos1, colEyR, ncolEyR As Integer

pos1 = 1
colEyR = 0
Do
Sheets(1).Activate
celda1 = Sheets(1).Cells(1, pos1)
If (celda1 <> "") Then
colEyR = colEyR + 1
End If
pos1 = pos1 + 1
Loop Until (celda1 = "")
'Renombramos variable
ncolEyR = colEyR

'Cuenta el numero de filas


Dim celda2 As Variant
Dim pos2, filEyR, nfilEyR As Integer

pos2 = 1
filEyR = 0
Do
Sheets(1).Activate
celda2 = Sheets(1).Cells(pos2, 1)
If (celda2 <> "") Then
filEyR = filEyR + 1
End If
pos2 = pos2 + 1
Loop Until (celda2 = "")
'Renombramos variable
nfilEyR = filEyR

Dim DatosEyR(nfilEyR, ncolEyR) As Variant


Dim i, j As Integer
'Copiamos los valores EyR a una matriz
For i = 1 To nfilEyR
For j = 1 To ncolEyR
DatosEyR(i, j) = Sheets(1).Cells(i, j).Value
Next
Next

ActiveWorkbook.Close

ElseIf Left(ActiveWorkbook.Name, 5) = "CMgCP" Then


ActiveWorkbook.Name = "CMgCP"

'Cuenta el numero de columnas


Dim celda3 As Variant
Dim pos3, colCMgC, ncolCMgC As Integer

pos3 = 1
colCMgC = 0
Do
Sheets("Cmg_Cong").Activate
celda3 = Sheets("Cmg_Cong").Cells(1, pos3)
If (celda3 <> "") Then
colCMgC = colCMgC + 1
End If
pos3 = pos3 + 1
Loop Until (celda3 = "")
'Renombramos variable
ncolCMgC = colCMgC

'Cuenta el numero de filas


Dim celda4 As Variant
Dim pos4, filCMgC, nfilCMgC As Integer

pos4 = 1
filCMgC = 0
Do
Sheets("Cmg_Cong").Activate
celda4 = Sheets("Cmg_Cong").Cells(pos4, 1)
If (celda4 <> "") Then
filCMgC = filCMgC + 1
End If
pos4 = pos4 + 1
Loop Until (celda4 = "")
'Renombramos variable
nfilCMgC = filCMgC

Dim DatosCMgC(nfilCMgC, ncolCMgC) As Variant


Dim i, j As Integer
'Copiamos los valores CMgC a una matriz
For i = 1 To nfilCMgC
For j = 1 To ncolCMgC
DatosCMgC(i, j) = Sheets("Cmg_Cong").Cells(i, j).Value
Next
Next

ActiveWorkbook.Close

ElseIf Left(ActiveWorkbook.Name, 7) = "Reporte" Then


ActiveWorkbook.Name = "RMRC"

Dim celda5 As Variant


Dim pos5, filRenta, nfilRenta As Integer

pos5 = 11
filRenta = 0
'Contamos las filas
Do
Sheets("Renta").Activate
celda5 = Sheets("Renta").Cells(pos5, 1)
If (celda5 <> "") Then
filRenta = filRenta + 1
End If
pos5 = pos5 + 1
Loop Until (celda5 = "")
'Renombramos variable
nfilRenta = filRenta

Dim DatosRenta(nfilRenta) As Variant


Dim k As Integer

For k = 1 To nfilRenta
DatosRenta(k) = Sheets("Renta").Cells(10 + k, 2).Value +
Sheets("Renta").Cells(10 + k, 3).Value
Next

ActiveWorkbook.Close
End If

Next
End If

ThisWorkbook.Activate
Dim nsheets As Integer

For nsheets = 1 To 5

If nsheets = 1 Then
Sheets("R").Activate
Call limpiador

ElseIf nsheets = 2 Then


Sheets("E").Activate
Call limpiador

ElseIf nsheets = 3 Then


Sheets("U").Activate
Call limpiador

ElseIf nsheets = 4 Then


Sheets("CMGC").Activate
Call limpiador

ElseIf nsheets = 5 Then


Sheets("MRC").Activate
Call limpiador
Dim f, c As Integer
For f = 4 To (nfilCMgC - 1 + 3)
Cells(f, 1).Value = DatosEyR(f + 1, 1)
Cells(f, nbus + 3).Value = DatosRenta(f - 3)
Cells(3, nbus + 3).Value =
Application.WorksheetFunction.Sum(Range(Cells(4, nbus + 3), Cells(nfilCMgC - 1 + 3,
nbus + 3)))
For c = 2 To (nbus - 1 + 2)
Cells(f, c).Value = (Sheets("R").Cells(f, c) +
Sheets("U").Cells(f, c) - Sheets("E").Cells(f, c)) * Sheets("CMGC").Cells(f, c)
Cells(3, c).Value =
Application.WorksheetFunction.Sum(Range(Cells(4, c), Cells(3 + nfilCMgC - 1, c)))
Cells(f, nbus + 2).Value =
Application.WorksheetFunction.Max(Application.WorksheetFunction.Sum(Range(Cells(f,
2), Cells(f, 2 + nbus - 1))), 0)
Cells(3, nbus + 2).Value =
Application.WorksheetFunction.Sum(Range(Cells(4, nbus + 2), Cells(3 + nfilCMgC - 1,
nbus + 2)))
Next
Next

ElseIf nsheets = 6 Then


Sheets("RJ").Activate
Call limpiador
Dim f, c As Integer
For f = 4 To (nfilCMgC - 1 + 3)
Cells(f, 1).Value = DatosEyR(f + 1, 1)
For c = 2 To (nbus - 1 + 2)
Cells(f, c).Value = (Sheets("R").Cells(f, c) +
Sheets("U").Cells(f, c)) *
Application.WorksheetFunction.Max(Sheets("CMGC").Cells(f, c), 0)
Cells(3, c).Value =
Application.WorksheetFunction.Sum(Range(Cells(4, c), Cells(3 + nfilCMgC - 1, c)))
Cells(f, nbus + 2).Value =
Application.WorksheetFunction.Sum(Range(Cells(f, 2), Cells(f, 2 + nbus - 1)))
Cells(3, nbus + 2).Value =
Application.WorksheetFunction.Sum(Range(Cells(4, nbus + 2), Cells(3 + nfilCMgC - 1,
nbus + 2)))
Next
Next
End If
Next

Application.ScreenUpdating = False
End Sub

Sub limpiador()

Dim celda As Variant


Dim pos, bus, nbus As Integer

pos = 1
bus = 0

Do
celda = Cells(2, pos)
If (celda <> "") Then
bus = bus + 1
End If
pos = pos + 1
Loop Until (celda = "")
nbus = bus

Range(Cells(4, 1), Cells(2979, nbus + 5)).ClearContents


Range(Cells(3, 2), Cells(3, nbus + 5)).ClearContents

End Sub

You might also like