BARRA DE progreso en un for

Tengo un código de impresión múltiple en el cual estoy anexando una barra de progreso el problema es que como es un rango dinámico el el 100 % que debería estar en la barra se descontrola y nunca llega o se pasa espero me puedas ayudar a solucionarlo mira:

   preg = MsgBox("Existen " & Sheets("HOJA DE SUELDOS").Range("Bd5").Value & " personas del Depto." & ComboBox1.Value & " desde el # " & TextBox1.Value & " Hasta el # " & TextBox2.Value & " ¿IMPRIMIR?", vbQuestion + vbYesNo, "IMPORTANTE")
     If preg = vbYes Then
'DoEvents
Dim oProgress As New frm_lcf_ProgressBar
oProgress.Initialize Range("BE5").Value, 2, "EN PROCESO"
IMPRESION.Hide
oProgress.Show 0
'oProgress.Increase
   Application.ScreenUpdating = False
   Sheets("IMPRESION").Activate
        For i = inicio To Fin
Application.ScreenUpdating = False
oProgress.Increase
            Sheets("IMPRESION").Range("C2").FormulaR1C1 = i
           If IMPRESION.ComboBox1.Value = Sheets("IMPRESION").Range("C23").Value Then
            ''MsgBox Range("C2").Value
            Sheets("IMPRESION").Range("A1:I29").Select
   Sheets("IMPRESION").PageSetup.PrintArea = "$A$1:$I$29"
 oProgress.Increase
   ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
            Application.ScreenUpdating = False
            oProgress.Increase
            Sheets("IMPRESION").Range("C2") = ""
            Else
           ' MsgBox "NO ES" & Range("C2").Value
            End If
         Next
  oProgress.Increase
          Unload oProgress
        MsgBox "FINALIZADO!", vbInformation, ""
        IMPRESION.Show
End If
Sheets("HOJA DE SUELDOS").Activate
End Sub

ese el codigo que estoy usando

y de aqui agarre el formulario

http://www.xperimentos.com/2007/06/27/barra-de-progreso-para-macros-de-excel/ 

1 Respuesta

Respuesta
4

La macro no está completa.

Puedes enviarme tu archivo y me explicas qué vas a hacer y en dónde quieres la barra.

Ya te envíe el correo con asunto: BARRA DE progreso en un for muchas muchas gracias dan

Te anexo el código del form Impresion

Private Sub CommandButton1_Click()
'Act.Por.Dante Amor
    Sheets("HOJA DE SUELDOS").Range("BC5").Value = ""
    Sheets("HOJA DE SUELDOS").Range("BC5").Value = ComboBox1.Value
    Application.ScreenUpdating = False
    inicio = IMPRESION.TextBox1.Value
    Fin = IMPRESION.TextBox2.Value
    'If TextBox1.Value > TextBox2.Value Then
    'MsgBox "No podemos continuar ya que el No inicial es mayor que el No final ", vbCritical, "ERROR"
    'Exit Sub
    'End If
    '
    If TextBox1.Value = "" Then
        MsgBox "Falta No de INICIO", vbInformation, "AVISO"
        TextBox1.SetFocus
        Exit Sub
    End If
    '
    If TextBox2.Value = "" Then
        MsgBox "Falta No de FINAL", vbInformation, "AVISO"
        TextBox2.SetFocus
        Exit Sub
    End If
    '
    If ComboBox1.Value = "" Then
        MsgBox "Falta Departamento", vbInformation, "AVISO"
        ComboBox1.SetFocus
        Exit Sub
    End If
    '
    If Sheets("HOJA DE SUELDOS").Range("BD5").Value <= 0 Then
        MsgBox "No hay personal de " & ComboBox1 & " para imprimir", vbInformation, "AVISO"
        DoEvents
        Exit Sub
    End If
    '
    preg = MsgBox("Existen " & Sheets("HOJA DE SUELDOS").Range("Bd5").Value & " personas del Depto." & ComboBox1.Value & " desde el # " & TextBox1.Value & " Hasta el # " & TextBox2.Value & " ¿IMPRIMIR?", vbQuestion + vbYesNo, "IMPORTANTE")
    If preg = vbNo Then Exit Sub
    '
    UserForm1.Show
    Sheets("HOJA DE SUELDOS").Activate
End Sub

Y el código del form Userform1 para el control de la barra

Private Sub UserForm_Activate()
'Referencia: http://support.microsoft.com/kb/211736/es
'Mod.Por.Dante Amor
    LProgress.Width = 0
    principal
End Sub
Sub principal()
'Por.Dante Amor
    Application.ScreenUpdating = False
    con = 1
    rep = 10
    Label1 = "Procesando ..."
    '
    ini = Val(IMPRESION.TextBox1)
    Fin = Val(IMPRESION.TextBox2)
    '
    Set h1 = Sheets("IMPRESION")
    For i = ini To Fin
        h1.Range("C2").Value = i
        If IMPRESION.ComboBox1.Value = h1.Range("C23").Value Then
            h1.PageSetup.PrintArea = "$A$1:$I$29"
            h1.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
        End If
        '
        If (con * 100) / Fin >= rep Then
            UpdateProgressBar rep
            rep = rep + 10
        End If
        con = con + 1
    Next
    h1.Range("C2") = ""
    Application.ScreenUpdating = True
    Label1 = "Proceso Terminado"
    MsgBox "FINALIZADO!", vbInformation, ""
    Unload Me
End Sub
Sub UpdateProgressBar(ava)
'Por.Dante Amor
    UserForm1.Frame1.Caption = Int(ava) & " %"
    LProgress.Width = LProgress.Width + 30
    DoEvents
    'Application. Wait Now + TimeValue("00:00:01")
End Sub

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

hola dan si esta genial.... mejor de lo que esperaba.... y funciona bien, pero cuando en el textbox1 pongo el valor 1 y en textbox2 porngo valor 10 funciona hasta el 100% que es el objetivo

pero cuando puse en textbox1 valor 6 y y en textbox2 puse 10 este se paro en 50% a que se debera??

Como el avance es de 10 en 10, la macro espera encontrar mínimo 10 repeticiones, de 6 a 10 solamente hay 5 repeticiones, por eso llegó al 50%.

Lo reviso y trato de ajustarla.

Muchas gracias dan.. en el textbox1 es cambiante así como puede ser 1 puede ser 5 o 10 dependiendo desde donde se quiera realizar la búsqueda

Te anexo la actualización para cuando son menos de 10 elementos.

Sub principal()
'Por.Dante Amor
    Application.ScreenUpdating = False
    con = 1
    rep = 10
    Label1 = "Procesando ..."
    '
    ini = Val(IMPRESION.TextBox1)
    Fin = Val(IMPRESION.TextBox2)
    wdif = Fin - ini + 1
    wflag = False
    If wdif < 10 Then
        wflag = True
        rep = Round(100 / wdif)
        winc = rep
    End If
    '
    Set h1 = Sheets("IMPRESION")
    For i = ini To Fin
        h1.Range("C2").Value = i
        If IMPRESION.ComboBox1.Value = h1.Range("C23").Value Then
            h1.PageSetup.PrintArea = "$A$1:$I$29"
            'h1.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
        End If
        '
        If (con * 100) / Fin >= rep Then
            UpdateProgressBar rep
            'rep = rep + 10
            If wflag Then
                rep = rep + winc
                If rep > 100 Then
                    UpdateProgressBar 100
                End If
            Else
                rep = rep + 10
            End If
        End If
        con = con + 1
    Next
    h1.Range("C2") = ""
    Application.ScreenUpdating = True
    Label1 = "Proceso Terminado"
    MsgBox "FINALIZADO!", vbInformation, ""
    Unload Me
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas