¿Cómo hago para que mi macro valide la buqueda de fecha y hora Ej: "02-10-2022 01:47:29 PM" en un combobox?

Cuando escribo en mi combobox la "hora y fecha" tal cual como esta en la celda de la hoja clientes, ¿no me arrja los datos de los campos requeridos y al buscar por los otros item me funciona perfecto que puedo hacer en este caso?

Private Sub Transponer_Change()
Dim h As Variant
Dim A As Variant
Set h = Sheets("CLIENTES")
Set A = h.Range("A3:E1048576").Find(Transponer.Value, LookAt:=xlWhole)
    If Not A Is Nothing Then
            txtCodigo.Value = h.Cells(A.Row, "A")
                cboxClientes.Text = h.Cells(A.Row, "B")
            txtCedula.Value = Format(h.Cells(A.Row, "C"), "###,###")
        txtTelefono.Value = h.Cells(A.Row, "D")
    txtFecha.Value = Format(h.Cells(A.Row, "E"), "dd-mm-yyyy HH:mm:ss am/pm")
End If
End Sub

1 Respuesta

Respuesta
2

Si en el combo tienes algo como esto:

04/10/2022  11:43:00 a. m.

Y en las celdas también tienes algo como esto:

04/10/2022  11:43:00 a. m.

Entonces prueba lo siguiente:

Private Sub Transponer_Change()
  Dim sh As Worksheet
  Dim f As Range
  '
  If Transponer.Value = "" Or Transponer.ListIndex = -1 Then Exit Sub
  Set sh = Sheets("CLIENTES")
  Set f = sh.Range("E:E").Find(CDate(Transponer.Value), , xlFormulas, xlWhole)
  If Not f Is Nothing Then
    txtCodigo.Value = sh.Range("A" & f.Row).Value
    cboxClientes.Text = sh.Range("B" & f.Row).Value
    txtCedula.Value = Format(sh.Range("C" & f.Row).Value, "###,###")
    txtTelefono.Value = sh.Range("D" & f.Row).Value
    txtFecha.Value = Format(sh.Range("E" & f.Row).Value, "dd-mm-yyyy HH:mm:ss am/pm")
  End If
End Sub

Notas: Si declaras las variables, debes declararlas de manera correcta. Utiliza nemotecnia para los nombres de las variables, por ejemplo sh para sheeet, f para Find, etc.


Comparte los enlaces en tus redes sociales.


Sal u dos

Amigo la macro se me detiene en la siguiente línea y me sombrea "ListIndex"

If Transponer.Value = "" Or Transponer.ListIndex = -1 Then Exit Sub

¿Y qué dice el mensaje de error?

¿Transponer es un combobox?

Dice error de compilación

No se encontró el dato miembro

Si amigo como usted dijo cambie el combobox por listbox y ya funciona

Una ultima pregunta: en caso de buscar por diferentes item no más debo cambiar en la "E:E" por decir algo por E3:E1048576.?

Sí, cambia el rango.

Prueba y me comentas


Comparte los enlaces en tus redes sociales.

Amigo lo hice de esta forma y me funciona, no se si este bien la programacion, si algo y puedes corregirme te agradezco, tus respuestas fueron un aporte importante

Private Sub Transponer_Change()
  Dim sh As Worksheet
  Dim f As Range
  '
 If cboxCategoria = "FECHA Y HORA" Then
  If Transponer.Value = "" Or ListBox1.ListIndex = -1 Then Exit Sub
    Set sh = Sheets("CLIENTES")
    Set f = sh.Range("A3:E1048576").Find(CDate(Transponer.Value), , xlFormulas, xlWhole)
        If Not f Is Nothing Then
            txtCodigo.Value = sh.Range("A" & f.Row).Value
                cboxClientes.Text = sh.Range("B" & f.Row).Value
                    txtCedula.Value = Format(sh.Range("C" & f.Row).Value, "###,###")
                txtTelefono.Value = sh.Range("D" & f.Row).Value
            txtFecha.Value = Format(sh.Range("E" & f.Row).Value, "dd-mm-yyyy")
        End If
    Else
    If Transponer.Value = "" Or ListBox1.ListIndex = -1 Then Exit Sub
    Set sh = Sheets("CLIENTES")
    Set f = sh.Range("A3:E1048576").Find(Transponer.Value, , xlFormulas, xlWhole)
        If Not f Is Nothing Then
            txtCodigo.Value = sh.Range("A" & f.Row).Value
                cboxClientes.Text = sh.Range("B" & f.Row).Value
                    txtCedula.Value = Format(sh.Range("C" & f.Row).Value, "###,###")
                txtTelefono.Value = sh.Range("D" & f.Row).Value
            txtFecha.Value = Format(sh.Range("E" & f.Row).Value, "dd-mm-yyyy")
        End If
    End If
End Sub

Toma nota de los siguientes consejos:

Observa mi código, reduce la duplicidad de instrucciones.

La sangría es para identificar las estructuras, por ejemplo:

IF Then
  'instrucciones
Else
  'instrucciones
End If
   

No utilices la sangría simplemente por ponerla, se pierde el orden y es difícil de leer el código.


El código se puede simplificar de la siguiente manera.

Private Sub Transponer_Change()
  Dim sh As Worksheet
  Dim f As Range
  '
  Set sh = Sheets("CLIENTES")
  If Transponer.Value = "" Or ListBox1.ListIndex = -1 Then
    Exit Sub
  End If
  '
  If cboxCategoria = "FECHA Y HORA" Then
    Set f = sh.Range("A3:E1048576").Find(CDate(Transponer.Value), , xlFormulas, xlWhole)
  Else
    Set f = sh.Range("A3:E1048576").Find(Transponer.Value, , xlFormulas, xlWhole)
  End If
  '
  If Not f Is Nothing Then
     txtCodigo.Value = sh.Range("A" & f.Row).Value
     cboxClientes.Text = sh.Range("B" & f.Row).Value
     txtCedula.Value = Format(sh.Range("C" & f.Row).Value, "###,###")
     txtTelefono.Value = sh.Range("D" & f.Row).Value
     txtFecha.Value = Format(sh.Range("E" & f.Row).Value, "dd-mm-yyyy")
   End If
End Sub

¡Musimas Gracias Amigo! valoro mucho el tiempo que te tomaste y el apoyo que me has dado, tendré muy en cuenta cada uno de los tips que me diste para poner en práctica.

Con mucho gusto.


Comparte los enlaces en tus redes sociales.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas