Duda al introducir formulario que este lo ordene por fecha

Hola,

Hice un userform el cual es un formulario de llenado de boletas o vales, donde tengo:

N° Vale, Fecha, Desde, Hasta, Nombre Persona, Hora, Valor

El cual lo tengo listo, pero quiero que este al presionar el botón me lo valla ordenando por fecha automáticamente, en el caso de que se me olvide introducir algún vale o boleta de una fecha pasada.

Quiero que me lo ordene de más antiguo a más reciente. Les dejo lo que hice:


Private Sub CommandButton2_Click()

Dim fila As Integer

For fila = 5 To 1000
If Hoja1.Cells(fila, 1) = "" Then
Hoja1.Cells(fila, 1) = TextBox1.Text
Hoja1.Cells(fila, 2) = TextBox2.Text
Hoja1.Cells(fila, 3) = TextBox3.Text
Hoja1.Cells(fila, 4) = TextBox4.Text
Hoja1.Cells(fila, 5) = TextBox5.Text
Hoja1.Cells(fila, 6) = TextBox6.Text
Hoja1.Cells(fila, 7) = TextBox7.Text
MsgBox "EL VALE A SIDO INGRESADO"
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
Exit Sub
End If

Next


End Sub

1 respuesta

Respuesta

Te anexo la macro con los cambios

Private Sub CommandButton2_Click()
'Act.Por.Dante Amor
    Dim fila As Integer
    Dim fecha As Date
    For fila = 5 To 1000
        If Hoja1.Cells(fila, 1) = "" Then
            fecha = TextBox2
            Hoja1.Cells(fila, 1) = TextBox1.Text
            Hoja1.Cells(fila, 2) = fecha
            Hoja1.Cells(fila, 3) = TextBox3.Text
            Hoja1.Cells(fila, 4) = TextBox4.Text
            Hoja1.Cells(fila, 5) = TextBox5.Text
            Hoja1.Cells(fila, 6) = TextBox6.Text
            Hoja1.Cells(fila, 7) = TextBox7.Text
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            Hoja1.Sort.SortFields.Clear
            Hoja1.Sort.SortFields.Add Key:=Range("B5:B" & fila), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With Hoja1.Sort
                .SetRange Range("A4:G" & fila)
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            MsgBox "EL VALE A SIDO INGRESADO"
            Exit Sub
        End If
    Next
End Sub

No olvides valorar la respuesta.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas