Al tener otras hojas abiertas se entrelazan con los combobox

Como puedes ver en la imagen al abrir otra planilla excel con datos en los combobox toma otros datos... No los de la Hoja del mismo form, dejo foto

1 Respuesta

Respuesta
2

Hay que especificar la hoja de donde ha de tomar los datos

Te anexo todo el código actualizado para que lea los datos de la hoja "ingresar"

Dim h1
'
Private Sub CommandButton1_Click()
'filtra por fechas
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    ListBox1.Clear
    If ComboBox1.Value = "" Then
        MsgBox "Seleccione una Fecha 'DESDE'"
        Exit Sub
    End If
    fec1 = CDate(ComboBox1.Value)
    If ComboBox2 = "" Then
        fec2 = fec1
    Else
        fec2 = CDate(ComboBox2.Value)
    End If
    '
    u = h1.Range("E" & Rows.Count).End(xlUp).Row
    For i = 3 To u
        lafecha = h1.Cells(i, "E").Value
        If h1.Cells(i, "E").Value >= fec1 And h1.Cells(i, "E") <= fec2 Then
            ListBox1. AddItem h1.Cells(i, "A")
            ListBox1. List(ListBox1.ListCount - 1, 1) = h1.Cells(i, "B")
            ListBox1. List(ListBox1.ListCount - 1, 2) = h1.Cells(i, "C")
            ListBox1. List(ListBox1.ListCount - 1, 3) = h1.Cells(i, "D")
            ListBox1. List(ListBox1.ListCount - 1, 4) = h1.Cells(i, "E")
            ListBox1. List(ListBox1.ListCount - 1, 5) = Format(h1.Cells(i, "F"), "hh:mm")
            ListBox1. List(ListBox1.ListCount - 1, 6) = Format(h1.Cells(i, "G"), "hh:mm")
            ListBox1. List(ListBox1.ListCount - 1, 7) = h1.Cells(i, "H")
        End If
    Next
End Sub
'
Private Sub CommandButton5_Click()
'   Por Dante Amor
'
'Filtra por turno
    If h1.AutoFilterMode Then h1.AutoFilterMode = False
    ListBox1.Clear
    If ComboBox3.Value = "" Then
        MsgBox "Seleccione un Turno"
        Exit Sub
    End If
    If ComboBox1.Value = "" Then
        fec1 = ""
    Else
        fec1 = CDate(ComboBox1.Value)
    End If
    If ComboBox2 = "" Then
        fec2 = fec1
    Else
        fec2 = CDate(ComboBox2.Value)
    End If
    '
    u = h1.Range("E" & Rows.Count).End(xlUp).Row
    lamisma = False
    For i = 3 To u
        lafecha = h1.Cells(i, "E").Value
        If fec1 = "" Then fec1 = h1.Cells(i, "E"): lamisma = True
        If fec2 = "" Then fec2 = h1.Cells(i, "E")
        If h1.Cells(i, "E").Value >= fec1 And h1.Cells(i, "E") <= fec2 And _
           h1.Cells(i, "A") = ComboBox3.Value Then
            ListBox1. AddItem h1.Cells(i, "A")
            ListBox1. List(ListBox1.ListCount - 1, 1) = h1.Cells(i, "B")
            ListBox1. List(ListBox1.ListCount - 1, 2) = h1.Cells(i, "C")
            ListBox1. List(ListBox1.ListCount - 1, 3) = h1.Cells(i, "D")
            ListBox1. List(ListBox1.ListCount - 1, 4) = h1.Cells(i, "E")
            ListBox1. List(ListBox1.ListCount - 1, 5) = Format(h1.Cells(i, "F"), "hh:mm")
            ListBox1. List(ListBox1.ListCount - 1, 6) = Format(h1.Cells(i, "G"), "hh:mm")
            ListBox1. List(ListBox1.ListCount - 1, 7) = h1.Cells(i, "H")
        End If
        If lamisma Then
            fec1 = ""
            fec2 = ""
        End If
    Next
End Sub
'
Private Sub CommandButton2_Click()
'Exporta
    'MsgBox "RECUERDE: Solo un PDF a la vez"
    If h1.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    u = h1.Range("F" & Rows.Count).End(xlUp).Row
    f1 = Format(ComboBox1.Value, "mm/dd/yyyy")
    If ComboBox2.Value = "" Then
        f2 = f1
    Else
        f2 = Format(ComboBox2.Value, "mm/dd/yyyy")
    End If
    '
    ruta = ThisWorkbook.Path & "\"
    arch = "BitacoraMantencion"
    prefijo = ""
    ver = ""
    ext = ".pdf"
    una = True
    Do While True
        If Dir(ruta & arch & prefijo & ver & ext) <> "" Then
            prefijo = "_v"
            If una Then
                ver = 1
                una = False
            Else
                ver = ver + 1
            End If
        Else
            Exit Do
        End If
    Loop
    '
    If ComboBox1 <> "" Then
        h1.Range("$A$2:$H$" & u).AutoFilter Field:=5, Criteria1:= _
            ">=" & f1, Operator:=xlAnd, Criteria2:="<=" & f2
    End If
    If ComboBox3 <> "" Then
        h1.Range("$A$2:$H$" & u).AutoFilter Field:=1, Criteria1:= _
            "=" & ComboBox3
    End If
    h1.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=ruta & arch & prefijo & ver & ext, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=True
    If h1.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
End Sub
Private Sub CommandButton4_Click()
'exportar a hoja
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    '
    Set l1 = ThisWorkbook
    'Set h1 = ActiveSheet
    ruta = ThisWorkbook.Path & "\"
    arch = ActiveSheet.Name
    If h1.AutoFilterMode Then h1.AutoFilterMode = False
    u = h1.Range("A" & Rows.Count).End(xlUp).Row
    h1.Copy
    Set l2 = ActiveWorkbook
    Set h2 = l2.Sheets(1)
    f1 = CDate(ComboBox1.Value)
    If ComboBox2.Value = "" Then
        f2 = f1
    Else
        f2 = CDate(ComboBox2.Value)
    End If
    '
    For i = u To 3 Step -1
        If h2.Cells(i, "E") >= f1 And h2.Cells(i, "E") <= f2 Then
        Else
            h2.Rows(i).Delete
        End If
    Next
    On Error Resume Next
    h2.DrawingObjects("Button 1").Delete
    On Error GoTo 0
    l2.SaveAs Filename:=ruta & arch & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    l2.Close
    MsgBox "Base de datos guardada en ESCRITORIO"
End Sub
'
Private Sub UserForm_Activate()
    Set h1 = Sheets("Ingresar")
    For i = 3 To h1.Range("E" & Rows.Count).End(xlUp).Row
        Call Agregar(ComboBox1, h1.Cells(i, "E").Value)
        Call Agregar(ComboBox2, h1.Cells(i, "E").Value)
        Call Agregar(ComboBox3, h1.Cells(i, "A").Value)
    Next
End Sub
'
Sub Agregar(combo As ComboBox, dato As String)
    For i = 0 To combo.ListCount - 1
        Select Case StrComp(combo.List(i), dato, vbTextCompare)
            Case 0: Exit Sub 'ya existe en el combo y ya no lo agrega
            Case 1: combo.AddItem dato, i: Exit Sub 'Es menor, lo agrega antes del comparado
        End Select
    Next
    combo.AddItem dato 'Es mayor lo agrega al final
End Sub
'
Private Sub TXTATRAS_Click()
    Unload Me
End Sub

La macro solamente funciona en la hoja "ingresar".

En la hoja "ingresar" en la columna E tienes las fechas, en la imagen que pusiste, las fechas aparecen en la columna A.

Pero supongo que quieres abrir el form en otra hoja pero que filtre según la hoja "ingresar"

sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas