Generar informe por mes y año con macro

Para Dante Amor

Buen día:

Tengo una macro con la que genero un informe a partir de una base de datos contenida en el archivo que contiene las macros, tengo dificultades cuando el numero de filas con datos supera 3600, creo que es por el tipo de variable que utilizo, pero no he logrado dar con el error, este es el código que utilizo para generar el informe.

De antemano muchas gracias por su constante apoyo.

PD: Le envié al correo el archivo.

'Informe entradas por mes
Private Sub ComboBox1_Enter()
Application.ScreenUpdating = False
Dim i As Double
Dim final As Double
Dim tareas As String
'Muestra el numero del mes
For i = 2 To 30
If Hoja1.Cells(i, 27) = "" Then 'Columna AA hoja Formulario
final = i - 1
Exit For
End If
Next
'Muestra el numero del mes
  For i = 2 To final
  tareas = Hoja1.Cells(i, 27)
  ComboBox1.AddItem (tareas)
  Next
End Sub
Private Sub ComboBox2_Enter()
Application.ScreenUpdating = False
Dim i As Double
Dim final As Double
Dim tareas As String
'Muestra el AÑO
For i = 2 To 30
If Hoja1.Cells(i, 30) = "" Then 'Columna AD hoja Formulario
final = i - 1
Exit For
End If
Next
'Muestra el numero del año
  For i = 2 To final
  tareas = Hoja1.Cells(i, 30)
  ComboBox2.AddItem (tareas)
  Next
End Sub
Private Sub ComboBox1_Click()
Dim i As Integer
Dim final As Integer
'Muestra la descripcion del mes
For i = 2 To 30
If Hoja1.Cells(i, 27) = "" Then 'Columna AA hoja Formulario
final = i - 1
Exit For
End If
Next
'Muestra la descripcion del mes
For i = 2 To final
If CStr(ComboBox1) = CStr(Hoja1.Cells(i, 27)) Then 'Columna AA hoja Formulario
'Muestra la descripcion del mes
TextBox1 = Hoja1.Cells(i, 28)
Exit For
End If
Next
End Sub
Private Sub CommandButton1_Click()
Dim nuevo As Object
Dim i As Integer
Dim L As Integer
Dim j As Integer
Dim VALOR As String
Dim CONTAR As Double
Dim CONTAR1 As Double
Dim libro1 As String
'Extraer el mes de la hoja entradas
ActiveWorkbook.Unprotect "1717171"
Hoja2.Visible = xlSheetVisible 'Hoja Entradas
Sheets("ENTRADAS").Select
libro1 = ActiveWorkbook.Name
Dim mes As String
ult = Hoja2.Range("A" & Rows.Count).End(xlUp).Row
'Extraer el mes y año de la hoja entradas
For y = 2 To ult
    mes = Month(Cells(y, 5)) '5 Indica la columna donde esta la fecha
Hoja2.Unprotect Password = "1717171" 'Hoja entradas
Cells(y, 11) = mes '11 indica la columna donde colocara el No. Del mes
Cells(y, 12) = Year(Cells(y, 5)) 'guarda el año en col 12
Next
Set nuevo = Workbooks.Add
nuevo.Activate
ORIGEN = ActiveWorkbook.Name
For i = 1 To 30000
'ENTRADAS
If Hoja2.Cells(i, 9) = "" Then 'Columna I Hoja Entradas ultima con datos
final = i - 1
Exit For
End If
Next
VALOR = Informe_EA_Mes.ComboBox1
' ENTRADAS
CONTAR = 10 '11
' ASIGNAR VALORES PARA EL INFORME
Application.Workbooks(ORIGEN).Worksheets(1).Cells(1, 1) = "INFORME ENTRADAS POR MES"
Application.Workbooks(ORIGEN).Worksheets(1).Cells(3, 2) = VALOR '3 indica No. De Fila y 2 No. Columna
For L = 1 To 30000
If Hoja1.Cells(L, 27) = VALOR Then
Application.Workbooks(ORIGEN).Worksheets(1).Cells(4, 2) = Hoja1.Cells(L, 28) 'Descricpion del mes
Exit For
End If
Next
For j = 1 To final   'la varible guardó última fila de hoja Entradas
If Hoja2.Cells(j, 11) = VALOR And Hoja2.Cells(j, 12) = Val(ComboBox2) Then
CONTAR = CONTAR + 1
'Funciona entradas
Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 2) = Hoja2.Cells(j, 6) 'Numero Fra
Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 3) = Hoja2.Cells(j, 1) 'Codigo Item
Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 4) = Hoja2.Cells(j, 2) 'Descricpion Item
Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 5) = Hoja2.Cells(j, 5) 'Fecha Entrada
Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 6) = Hoja2.Cells(j, 3) 'Cantidad entrada
Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 7) = Hoja2.Cells(j, 8) 'Valor
End If
Next

1 respuesta

Respuesta
2

Te cambio la macro, revisa el resultado

Private Sub CommandButton1_Click()
'Act.Por.Dante Amor
    Set l1 = ThisWorkbook
    Set h1 = l1.Sheets("ENTRADAS")
    h1.Unprotect Password = "1717171"
    '
    u = h1.Range("A" & Rows.Count).End(xlUp).Row
    If h1.AutoFilterMode Then h1.AutoFilterMode = False
    dia = Day(DateSerial(Year(Val(ComboBox2)), Val(ComboBox1) + 1, 1) - 1)
    fecha = Val(ComboBox1) & "/" & dia & "/" & Val(ComboBox2)
    h1.Range("A1:J" & u).AutoFilter Field:=5, Operator:=xlFilterValues, Criteria2:=Array(1, fecha)
    Set l2 = Workbooks.Add
    Set h2 = l2.Sheets(1)
    j = 2
    cols = Array(6, 1, 2, 5, 3, 8)
    For i = LBound(cols) To UBound(cols)
        h1.Range(h1.Cells(2, cols(i)), h1.Cells(u, cols(i))).Copy h2.Cells(11, j)
        j = j + 1
    Next
    If h1.AutoFilterMode Then h1.AutoFilterMode = False
    h2.Cells(1, 1) = "INFORME ENTRADAS POR MES"
    h2.Cells(3, 2) = ComboBox1
    Call Formato(l2, h2)
    Application.ScreenUpdating = False
    h1.Protect Password = "1717171"
    'Cerrar los UF
    Unload Me
    Unload Informes
    Unload Menu_Principal
End Sub
'
Sub Formato(l2, h2)
    l2.Activate
    h2.Select
        Range("A1:G1").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        With Selection.Interior
            .ColorIndex = 11
            .Pattern = xlSolid
        End With
        Selection.Font.ColorIndex = 2
        Selection.Font.Bold = True
        Range("A3").Select
        ActiveCell.FormulaR1C1 = "No DE MES"
        Range("A4").Select
        ActiveCell.FormulaR1C1 = "DESCRIPCION DEL MES"
        Range("B4") = TextBox1
        Range("B4:F4").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Range("B3:F3").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Range("B6:F6").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        'Encabezados del informe
        Range("B8").Select
        ActiveCell.FormulaR1C1 = ""
    'Para entradas
        Range("B10").Select
        ActiveCell.FormulaR1C1 = "No.FACTURA"
        Range("C10").Select
        ActiveCell.FormulaR1C1 = "COD.ITEM"
        Range("D10").Select
        ActiveCell.FormulaR1C1 = "DESCRIPCION"
        Range("E10").Select
        ActiveCell.FormulaR1C1 = "F.ENTRADA"
        Range("F10").Select
        ActiveCell.FormulaR1C1 = "C.ENTRADA"
        Range("G10").Select
        ActiveCell.FormulaR1C1 = "VALOR"
        Range("B8:G8").Select
        With Selection
             .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        ActiveCell.FormulaR1C1 = "ENTRADAS PARA EL MES DE" & " " & Range("B4")
        Range("B8:G8").Select
        With Selection.Interior
            .ColorIndex = 3
            .Pattern = xlSolid
        End With
        Selection.Font.ColorIndex = 2
        Selection.Font.Bold = True
        Range("B10:G10").Select
        Selection.Font.Bold = True
        Range("B10:G10").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        Cells.Select
        Cells.EntireColumn.AutoFit
        Range("A2").Select
        '***********************************
    'Formato de fecha
        Range("E11").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.NumberFormat = "m/d/yyyy"
End Sub

.

'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas