Excel - COMO DAR SALTO DE FILA AL AGREGAR DATOS DE UN FORM

 
Vista:
sin imagen de perfil
Val: 7
Ha aumentado su posición en 3 puestos en Excel (en relación al último mes)
Gráfica de Excel

COMO DAR SALTO DE FILA AL AGREGAR DATOS DE UN FORM

Publicado por Adriam Jesus (1 intervención) el 26/06/2019 23:22:47
Hace poco estuve creando esta macro que utiliza cajas de texto de un Form creado.
Como verán son simples. Mi duda viene en base a un error al momento de agregar los datos a una tabla
de una hoja de excel. Necesito ayuda para saber como dar el salto de fila a la de abajo pues al agregar con esta
macro se sobreescribe los datos.

AQUI la macro:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Dim fila As Long
Dim duplicados As Boolean
 
'obteniendo fila disponible
fila = Application.WorksheetFunction.CountA(Range("A5:L307")) + 1
duplicados = False
 
'Validar si se han ingresado datos duplicados
For i = 1 To fila
    If Cells(i, 1).Value = UserForm5.txtFec.Value Then
    If Cells(i, 2).Value = UserForm5.txtFac.Value Then
    If Cells(i, 3).Value = UserForm5.cboLleno.Value Then
    If Cells(i, 4).Value = UserForm5.txtIni.Value Then
    If Cells(i, 5).Value = UserForm5.txtFin.Value Then
    If Cells(i, 7).Value = UserForm5.txtLitros.Value Then
    If Cells(i, 8).Value = UserForm5.txtPrecio.Value Then
    If Cells(i, 10).Value = UserForm5.cboTipoCom.Value Then
    If Cells(i, 11).Value = UserForm5.cboTipoRut.Value Then
    If Cells(i, 12).Value = UserForm5.txtObs.Value Then
        'SE ENCONTRO DATOS DUPLICADOS
        MsgBox "Datos duplicados en la fila " & i
        duplicados = True
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
Next i
 
If Not duplicados Then
    'Insertando los datos
    Worksheets("MODELO").Range("A7").Value = UserForm5.txtFec.Value
    Worksheets("MODELO").Range("B7").Value = UserForm5.txtFac.Value
    Worksheets("MODELO").Range("C7").Value = UserForm5.cboLleno.Value
    Worksheets("MODELO").Range("D7").Value = UserForm5.txtIni.Value
    Worksheets("MODELO").Range("E7").Value = UserForm5.txtFin.Value
    Worksheets("MODELO").Range("G7").Value = UserForm5.txtLitros.Value
    Worksheets("MODELO").Range("H7").Value = UserForm5.txtPrecio.Value
    Worksheets("MODELO").Range("J7").Value = UserForm5.cboTipoCom.Value
    Worksheets("MODELO").Range("K7").Value = UserForm5.cboTipoRut.Value
    Worksheets("MODELO").Range("L7").Value = UserForm5.txtObs.Value
 
    'Limpiando cajas de texto
    UserForm5.txtFec.Value = ""
    UserForm5.txtFac.Value = ""
    UserForm5.cboLleno.Value = ""
    UserForm5.txtIni.Value = ""
    UserForm5.txtFin.Value = ""
    UserForm5.txtLitros.Value = ""
    UserForm5.txtPrecio.Value = ""
    UserForm5.cboTipoCom.Value = ""
    UserForm5.cboTipoRut.Value = ""
    UserForm5.txtObs.Value = ""
 
    'Notificando
    MsgBox "Datos insertados en la fila " & fila
   End If

Me ayudaría mucho si resuelvo esto. Llevo días intentándolo
Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Antoni Masana
Val: 4.908
Oro
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

COMO DAR SALTO DE FILA AL AGREGAR DATOS DE UN FORM

Publicado por Antoni Masana (2481 intervenciones) el 27/06/2019 13:18:48
Esta macro está llena de errores.

- ¿Buscar la primera fila disponible? Desde luego esto no lo hace

1
2
'obteniendo fila disponible
fila = Application.WorksheetFunction.CountA(Range("A5:L307")) + 1

- ¿En que hoja estas trabajando?
- Demasiados IF
- Escribes siempre en la linea 7 , esto no es como los carteles que se pegan uno encima otro

Hay dos formas de insertar por arriba o por abajo. Este lo hace por abajo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Sub Macro
    Dim fila As Long
 
    With UserForm5
        Sheets("MODELO").Select
 
        Fila = 1
        WHILE Cells(Fila(1,"A") <> ""
            If Cells(i,  1).Value = .txtFec.Value And _
               Cells(i,  2).Value = .txtFac.Value And _
               Cells(i,  3).Value = .cboLleno.Value And _
               Cells(i,  4).Value = .txtIni.Value And _
               Cells(i,  5).Value = .txtFin.Value And _
               Cells(i,  7).Value = .txtLitros.Value And _
               Cells(i,  8).Value = .txtPrecio.Value And _
               Cells(i, 10).Value = .cboTipoCom.Value And _
               Cells(i, 11).Value = .cboTipoRut.Value And _
               Cells(i, 12).Value = .txtObs.Value Then
 
                ' ---&--- SE ENCONTRO DATOS DUPLICADOS
                MsgBox "Datos duplicados en la fila " & i
                Exit Sub
            End If
            Fila = Fila +1
        Wend
 
        ' ---&--- Insertando los datos en la primera fila vacía
 
        Range("A" & Fila).Value = .txtFec.Value
        Range("B" & Fila).Value = .txtFac.Value
        Range("C" & Fila).Value = .cboLleno.Value
        Range("D" & Fila).Value = .txtIni.Value
        Range("E" & Fila).Value = .txtFin.Value
        Range("G" & Fila).Value = .txtLitros.Value
        Range("H" & Fila).Value = .txtPrecio.Value
        Range("J" & Fila).Value = .cboTipoCom.Value
        Range("K" & Fila).Value = .cboTipoRut.Value
        Range("L" & Fila).Value = .txtObs.Value
 
        ' ---&--- Limpiando cajas de texto
 
        .txtFec.Value = ""
        .txtFac.Value = ""
        .cboLleno.Value = ""
        .txtIni.Value = ""
        .txtFin.Value = ""
        .txtLitros.Value = ""
        .txtPrecio.Value = ""
        .cboTipoCom.Value = ""
        .cboTipoRut.Value = ""
        .txtObs.Value = ""
    End With
 
    ' ---&--- Notificando
 
    MsgBox "Datos insertados en la fila " & fila
End Sub

Saludos.
\\//_
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar