Generar códigos correlativos con numero y fecha al ingresar un registro en una base de datos

Tengo un documento que es de ingreso de instrumentos de medición. Además es necesario buscar estos instrumentos para terminar el proceso en el que se encuentran, para ello es necesario un código (Numero) correlativo para cada instrumento agregado.
Ejemplo de lo que se necesita:

ITEM         FECHA INGRESO             CÓDIGO                NOMBRE                     ESTADO        CORRELATIVO 

1                01-02-2017                       4565123              Llave de torque           Aprobado            0001-0217

2                03-04-2017                        M4578                Multimetro                   Aprobado            0002-0417

3               05-04-2017                         258963               Filler                              Rechazado           0003-0417

4               27-08-2017                         MKP-123             Micrómetro                 Aprobado            0004-0827

5                05-11-2017                       M4578                 Multimetro                   Rechazado           0005-1117

___________________________________________________________________________________________________

Como se puede observar los instrumentos 2 y 5 son los mismos pero ingresados en distintas fechas, por lo tanto cada vez que se vaya ingresando un instrumento, este debe tener un numero correlativo único.

Esto lo necesito hacer mediante un formulario, ya que si lo hago directo desde la hoja de Excel, al momento de buscar el instrumento por el numero de correlativo este no lo encuentra, ya que es una función y no un valor fijo. A su vez al momento de que me genere el numero, es necesario que me mande un msgbox diciendo el numero de correlativo que se genero.

Formulario realizado:

1 respuesta

Respuesta
1

H o la 

Prueba esto 

Private Sub CommandButton1_Click()
Range("A1") = Format(Val(TextBox1), "0000") & "-" & Format(TextBox2, "mm-yy")
End Sub

Valora la respuesta para finalizar saludos!

¿Faltó algo?

No me sirvió de mucho :/ por que me ingresa en la hoja uno y en una sola ubicación y yo necesito que me vaya ingresando por ejemplo en la hoja 2 desde la celda H8 y ahí hacia abajo cada vez que ingrese un registro, además necesito que al final la operación me arroje un msgbox con el numero de correlativo que se creó.

Te paso la macro actualizada

Private Sub CommandButton1_Click()
Dim cod, num, fecha
'
Set h2 = Sheets("Hoja2")
'
u = h2.Range("H" & Rows.Count).End(xlUp).Row + 1
If u < 8 Then u = 8
    num = Format(Val(TextBox1), "0000")
    fecha = Format(TextBox2, "mm-yy")
    cod = num & "-" & fecha
'
    h2.Cells(u, "H") = cod
    MsgBox "Se ha generado el correlativo: " & cod
End Sub

Recuerda cambiar la valoración Excelente

Si falta algo házmelo saber

una consulta en la parte que dice:

num=Format(val(TextBox1), "0000")

puedo hacer la numeración sin realizar lo desde un textbox, ya que ese numero no tengo de donde sacarlo en el formulario. por ejemplo porner:

num = "000" + 1 

pero la idea es que me quede como 0001-0217  no como 1-0217

Entonces que te muestre el código siguiente en el Textbox1

Te paso la macro actualizada

Private Sub CommandButton1_Click()
Dim cod, num, fecha
'
Set h2 = Sheets("Hoja2")
'
If TextBox1 = "" Or TextBox2 = "" Then
MsgBox "ingrese fecha", vbInformation
Exit Sub
End If
u = h2.Range("H" & Rows.Count).End(xlUp).Row + 1
'
If u < 8 Then u = 8
'
    num = Format(Val(TextBox1), "0000")
    fecha = Format(TextBox2, "mm-yy")
    cod = num & "-" & fecha
'
    h2.Cells(u, "H") = cod
    MsgBox "Se ha generado el correlativo: " & cod
    End
End Sub


Private Sub UserForm_Activate()
Dim fin
'
Set h2 = Sheets("Hoja2")
'
u1 = h2.Range("H" & Rows.Count).End(xlUp).Row
'
    fin = h2.Cells(u1, "H")
    k = Mid(fin, 1, 4) + 1
    TextBox1 = Format(k, "0000")
End Sub

Te adjunto el archivo

https://www.dropbox.com/s/gvwrjeysy9ebppe/ejemplo%20para%20macros.xlsm?dl=0

Me comentas que tal te fue

Estimado,

Me sigue saliendo igual. Hay alguna posibilidad de que te lo envíe por correo y tu me lo puedas modificar ??

Envíame [email protected] y me explicas como deseas

listo

No me llegó tu correo, haber envíame [email protected]

listo

H o l a

reemplaza la macro por esto 

'************************
Dim cod, num, fecha
Set h2 = Sheets("CORRELATIVO")
u = h2.Range("H" & Rows.Count).End(xlUp).Row + 1
If u < 8 Then u = 8
 u1 = h2.Range("H" & Rows.Count).End(xlUp).Row
'
    fin = h2.Cells(u1, "H")
    k = Mid(fin, 1, 4) + 1
    num = Format(k, "0000")
 fecha = Format(txtFechaIngreso, "mmyy")
 cod = num & "-" & fecha
 h2.Cells(u, "H") = cod
 MsgBox "Se ha guardado el correlativo:" & cod

me comentas 

me tira un error en:

k = Mid(fin, 1, 4) + 1

Te envío el archivo.

Me está funcionando bien

a mi no me lo puedes mandar por favor 

por favor me lo puedes enviar 

Está pésimo mi línea de internet

Te paso el código del commandbuton correlativo

Private Sub cmdGenerarCorrelativo_Click()
If Len(frmGenerarCorrelativo.txtCodigo) = 0 Then
        MsgBox "Por Favor Ingresar el N° de Instrumento!"
        Exit Sub
End If
If Len(frmGenerarCorrelativo.txtFechaCalibracion) = 0 Then
MsgBox "Por favor complete los datos faltantes !", 64, "Faltan Datos"
    Exit Sub
End If
If Len(frmGenerarCorrelativo.txtEstado) = 0 Then
MsgBox "Por favor complete los datos faltantes !", 64, "Faltan Datos"
    Exit Sub
End If
If Len(frmGenerarCorrelativo.txtFechaIngreso) = 0 Then
        MsgBox "Por favor complete los datos faltantes !", 64, "Faltan Datos"
    Exit Sub
End If
If Len(frmGenerarCorrelativo.txtNombre) = 0 Then
MsgBox "Por favor complete los datos faltantes !", 64, "Faltan Datos"
    Exit Sub
End If
If Len(frmGenerarCorrelativo.txtFechaRegistro) = 0 Then
MsgBox "Por favor complete los datos faltantes !", 64, "Faltan Datos"
    Exit Sub
End If
If Len(frmGenerarCorrelativo.txtOrdenTrabajo) = 0 Then
MsgBox "Por favor complete los datos faltantes !", 64, "Faltan Datos"
    Exit Sub
End If
If Len(frmGenerarCorrelativo.txtLaboratorio) = 0 Then
MsgBox "Por favor complete los datos faltantes !", 64, "Faltan Datos"
    Exit Sub
End If
If Len(frmGenerarCorrelativo.txtRealizado) = 0 Then
MsgBox "Por favor complete los datos faltantes !", 64, "Faltan Datos"
    Exit Sub
End If
Dim LastRow As Object
Set LastRow = Correlativo.Range("B65536").End(xlUp)
LastRow.Offset(1, 0).Value = txtFechaIngreso
LastRow.Offset(1, 1).Value = txtCodigo.Text
LastRow.Offset(1, 2).Value = txtNombre.Text
LastRow.Offset(1, 3).Value = txtArea.Text
LastRow.Offset(1, 4).Value = txtVariable.Text
LastRow.Offset(1, 5).Value = txtFechaRegistro
LastRow.Offset(1, 7).Value = txtFechaCalibracion
LastRow.Offset(1, 9).Value = txtRealizado.Text
LastRow.Offset(1, 10).Value = txtEstado.Text
LastRow.Offset(1, 12).Value = txtOrdenTrabajo.Text
LastRow.Offset(1, 13).Value = txtLaboratorio.Text
MsgBox "Instrumento almacenado exitosamente", 64, "Ingresar Instrumento"
'************************
Dim cod, num, fecha
Set h2 = Sheets("CORRELATIVO")
u = h2.Range("H" & Rows.Count).End(xlUp).Row + 1
If u < 8 Then u = 8
 u1 = h2.Range("H" & Rows.Count).End(xlUp).Row
'
    fin = h2.Cells(u1, "H")
    k = Mid(fin, 1, 4) + 1
    num = Format(k, "0000")
 fecha = Format(txtFechaIngreso, "mmyy")
 cod = num & "-" & fecha
 h2.Cells(u, "H") = cod
 MsgBox "Se ha guardado el correlativo:" & cod
 '******************************************
responde = MsgBox("¿Desea añadir otro instrumento?", vbYesNo)
If responde = vbYes Then
txtFechaIngreso.Text = ""
txtCodigo.Text = ""
txtNombre.Text = ""
txtArea.Text = ""
txtVariable.Text = ""
txtFechaRegistro.Text = ""
txtFechaCalibracion.Text = ""
txtRealizado.Text = ""
txtEstado.Text = ""
txtOrdenTrabajo.Text = ""
txtLaboratorio.Text = ""
Else
Unload Me
End If
End Sub

me tira error nuevamente :/

Ah comento la macro funciona si tiene ya un correlativo existente

Por ejemplo

Si en la hoja correlativo en la columna H, ya tienes

0010-0217

Automáticamente genera el 0011-0217.

Y si está vacío arroja error.

Dime si coincidimos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas