Visual Basic - No se modifica fila deseada

Life is soft - evento anual de software empresarial
 
Vista:
sin imagen de perfil

No se modifica fila deseada

Publicado por Dan (8 intervenciones) el 28/11/2023 12:05:41
Muy buenos Dias
Estoy avanzando en un proyecto en Excel y esta casi acabando. No tengo mucho conocimiento de macros y vba. El proyecto que estoy haciendo lo he conseguido gracias a personas como ustedes que dedican también sus conocimientos para enseñar a otros. Muchisimas gracias de verdad.
Me gustaría por favor si fuera posible que me indicaran lo que tendría que hacer para poder solucionar un error que estoy dando vueltas. Seguro que será una tontería pero no doy con la solución al problema

En el proyecto que estoy trabajando y le expongo el problema, tengo dos hojas (una llamada DATOS y la otra Hoja 1)
En la de datos no tienen nada y en la hoja 1 hay unos celdas con algunos datos como se aprecian en la fotografías

Lo que sí funciona en el formulario es lo siguiente:
• Si quiero modificar el TRABAJADOR7, hago click en el listbox y selecciono el trabajador7, se pone la celda con bordes verdes como que esta seleccionada y añado un dato (fecha y hora) en el mes de febrero a dicho trabajador y tras pulsar el botón modificar, me cambia el dato.

• Problema:
Al buscar por ejemplo el TRABAJADOR6 en el buscador inteligente de arriba y añadiendo o modificando datos de ese trabajador en el mes de febrero, me cambia los datos del primer trabajador (trabajador1) y no el trabajador6. (En las otras casillas me pasa lo mismo).



Este es el código que tengo:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Public Ocultar As Boolean
Private Sub UserForm_Initialize()
            With ListBoxPrincipal
           .ColumnCount = 25
           '.ColumnHeads = True
           .List = Range(Cells(1, 1), Cells(1, .ColumnCount)).Value
           .ColumnWidths = "90;44;44;44;44"
            .ListIndex = 0
           .RemoveItem 0
           .Font.Size = 7.9
           .TextAlign = fmTextAlignLeft
        ActualizarLista
        End With
 
End Sub
 
Private Sub ListBoxPrincipal_Click()
        Dim x As Long
                Ocultar = True
        With ListBoxPrincipal
            If .ListIndex > -1 Then
                x = .ListIndex
                TextBox1.Text = .List(x, 1)
                TextBox2.Text = .List(x, 2)
                TextBox3.Text = .List(x, 3)
                TextBox4.Text = .List(x, 4)
 
               Range("C" & ListBoxPrincipal.ListIndex + 5).Activate
            End If
        End With
   Ocultar = False
            TextBox2.Text = Format(TextBox2.Text, "dd/mm/yy")
            TextBox4.Text = Format(TextBox4.Text, "dd/mm/YY")
End Sub
 
Private Sub ActualizarLista()
            cargar_listbox
            TextBox1 = Empty
            TextBox2 = Empty
            TextBox3 = Empty
            TextBox4 = Empty
End Sub
 
Private Sub CommandButton1_Click()
 
        If ListBoxPrincipal.ListIndex < 0 Then
           MsgBox "No se ha elegido ningún registro.", vbExclamation
        Else
        If MsgBox("¿Está seguro de MODIFICAR el registro?", vbYesNo + vbExclamation) = vbYes Then
            x = ListBoxPrincipal.ListIndex
               ListBoxPrincipal.RowSource = Empty
               ActiveCell.Offset(0, 1) = TextBox1
               ActiveCell.Offset(0, 2) = TextBox2
               ActiveCell.Offset(0, 3) = TextBox3
               ActiveCell.Offset(0, 4) = TextBox4
'--------------------------------------------------------------------------
               ActualizarLista
               ListBoxPrincipal.ListIndex = x
               cargar_listbox
            End If
        End If
End Sub
 
Private Sub CommandButton2_Click()
        If ListBoxPrincipal.ListIndex < 0 Then
           MsgBox "No se ha elegido ningún registro.", vbExclamation
        Else
           If MsgBox("¿Está seguro de ELIMINAR el registro?", vbYesNo + vbExclamation) = vbYes Then
              Rows(ActiveCell.Row).Delete
              ActualizarLista
              ListBoxPrincipal.ListIndex = -1
           End If
        End If
End Sub
 
Private Sub CommandButton3_Click()
        Unload Me
End Sub
 
Private Sub TextBox26_Change()
    Dim CFilas, i, Y As Long
    Dim Item As String
        CFilas = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
        ListBoxPrincipal.Clear
        For i = 5 To CFilas
            If LCase(ActiveSheet.Range("C" & i).Value) Like "*" & LCase(Me.TextBox26.Value) & "*" Then
                Me.ListBoxPrincipal.AddItem ActiveSheet.Range("C" & i).Value
                Me.ListBoxPrincipal.List(Me.ListBoxPrincipal.ListCount - 1, 1) = Format(ActiveSheet.Range("D" & i).Value, "DD/MM/YY")
                Me.ListBoxPrincipal.List(Me.ListBoxPrincipal.ListCount - 1, 2) = Format(ActiveSheet.Range("E" & i).Value, "hh:mm")
                Me.ListBoxPrincipal.List(Me.ListBoxPrincipal.ListCount - 1, 3) = Format(ActiveSheet.Range("F" & i).Value, "DD/MM/YY")
                Me.ListBoxPrincipal.List(Me.ListBoxPrincipal.ListCount - 1, 4) = Format(ActiveSheet.Range("G" & i).Value, "hh:mm")
            Else
            End If
        Next i
End Sub
 
 
Sub cargar_listbox()
Dim CFilas, i, Y, ii As Long
        CFilas = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
        ListBoxPrincipal.Clear
 
        For i = 5 To CFilas
                Me.ListBoxPrincipal.AddItem ActiveSheet.Range("C" & i).Value
                Me.ListBoxPrincipal.List(Me.ListBoxPrincipal.ListCount - 1, 1) = Format(ActiveSheet.Range("D" & i).Value, "DD/MM/YY")
                Me.ListBoxPrincipal.List(Me.ListBoxPrincipal.ListCount - 1, 2) = Format(ActiveSheet.Range("E" & i).Value, "hh:mm")
                Me.ListBoxPrincipal.List(Me.ListBoxPrincipal.ListCount - 1, 3) = Format(ActiveSheet.Range("F" & i).Value, "DD/MM/YY")
                Me.ListBoxPrincipal.List(Me.ListBoxPrincipal.ListCount - 1, 4) = Format(ActiveSheet.Range("G" & i).Value, "hh:mm")
        Next i
End Sub


Muchas gracias de antemano.
Un saludo

Formulario
Hoja1
Hoja2
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: 1.259
Plata
Ha mantenido su posición en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

No se modifica fila deseada

Publicado por Antoni Masana (558 intervenciones) el 01/12/2023 10:48:20
La solución esta en este procedimiento:

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
Private Sub CommandButton1_Click()
    If ListBoxPrincipal.ListIndex < 0 Then
        MsgBox "No se ha elegido ningún registro.", vbExclamation
    Else
        If MsgBox("¿Está seguro de MODIFICAR el registro?", vbYesNo + vbExclamation) = vbYes Then
            Nombre = ListBoxPrincipal.List(ListBoxPrincipal.ListIndex)
            CFilas = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
 
            For i = 5 To CFilas
                If LCase(ActiveSheet.Range("C" & i).Value) = LCase(Nombre) Then
                    ActiveSheet.Range("C" & i).Select
                    Exit For
                End If
            Next
 
            x = ListBoxPrincipal.ListIndex
            ListBoxPrincipal.RowSource = Empty
 
            ActiveCell.Offset(0, 1) = TextBox1
            ActiveCell.Offset(0, 2) = TextBox2
            ActiveCell.Offset(0, 3) = TextBox3
            ActiveCell.Offset(0, 4) = TextBox4
 
            ActualizarLista
            ListBoxPrincipal.ListIndex = x
            cargar_listbox
        End If
    End If
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
sin imagen de perfil

No se modifica fila deseada

Publicado por Dan (8 intervenciones) el 01/12/2023 11:17:02
Hola Antoni.
Lo estoy incorporando al archivo y funciona perfecto.
Muchísimas gracias.
En el original tiene 25 columnas y 2 textbox por mes y al hacerlo funcionar, va un poquito lento pero bueno. Incluso al hacer la búsqueda. Pero me vale perfectamente.

Nuevamente gracias por tu ayuda.
Un saludo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar