Encontrar dato y modificar "fila" mediante Userform

De todoexpertos, tengo un problema que aun no le he podido dar solución, bien les explicare tengo una hoja donde esta la base de datos de alumnos(que esta oculta) que tengo (nombre, apellido, asistencia, 1 examen, 2 examen, 3 examen, Estado), y he hecho un userform donde en el Combox1 filtra por nombres que están en la columna A, pongo el nombre y en los demás textbox's me aparece la información lo mismo en Combox2 que filtra por apellidos que están en la columna B, hasta el momento todo bien, ahora necesito poder modificar los datos en los textbox y grabarlos en el mismo sitio,,, ejm...

      A             B         C      D

1  jose         cuba    18   11

2  miguel    ortiz    15   13

Bien cuando filtro en el Combox1 miguel ; me aparece en los textbox's el nombre, apellido, nota1, nota2, etc, ahora quiero modificar las notas que son 15,13 por 11,12 por ejemplo, y con un CommanButton me "actualice los datos".. Gracias

he intentado hacer lago como esto pero me guarda los datos donde no corresponden

Private Sub CommandButton3_Click()
Sheets("alumnos").Activate  -> tiene que estar oculta 
If TextBox1 = "" Or TextBox2 = "" Or TextBox3 = "" Or TextBox4 = "" Then
MsgBox "Campos requeridos vacios favor complete"
TextBox2.SetFocus
Else
If CheckBox1.Value = True Then
ActiveCell.Offset(0, 1) = TextBox2.Value
ActiveCell.Offset(0, 2) = TextBox3.Value
ActiveCell.Offset(0, 3) = TextBox4.Value
ActiveCell.Offset(0, 4) = TextBox5.Value
ActiveCell.Offset(0, 5) = TextBox6.Value
ActiveCell.Offset(0, 6) = TextBox7.Value
ActiveCell.Offset(0, 7) = TextBox8.Value
MsgBox "Datos actualizados correctamente", vbInformation, "Almacen"
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
TextBox5 = ""
TextBox6 = ""
TextBox7 = ""
TextBox8 = ""
TextBox2.Locked = True
TextBox3.Locked = True
TextBox4.Locked = True
TextBox5.Locked = True
TextBox6.Locked = True
TextBox7.Locked = True
TextBox8.Locked = True
Else
ActiveCell = TextBox2.Value
ActiveCell.Offset(0, 1) = TextBox2.Value
ActiveCell.Offset(0, 2) = TextBox4.Value
ActiveCell.Offset(0, 3) = TextBox5.Value
ActiveCell.Offset(0, 4) = TextBox6.Value
ActiveCell.Offset(0, 5) = TextBox7.Value
ActiveCell.Offset(0, 6) = TextBox8.Value
MsgBox "Datos actualizados correctamente", vbInformation, "Almacen"
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
TextBox5 = ""
TextBox6 = ""
TextBox7 = ""
TextBox8 = ""
TextBox2.Locked = True
TextBox3.Locked = True
TextBox4.Locked = True
TextBox5.Locked = True
TextBox6.Locked = True
TextBox7.Locked = True
TextBox8.Locked = True
End If
CheckBox1.Value = False
CheckBox2.Value = False
End If
End Sub

No se que falta paa que funcione bien,.

1 respuesta

Respuesta
1

Puedes poner cómo estás cargando los datos en el combo1 y en el combo2.

También dime en cuál columna debe ir el textbox2

Y también explícame si tiene el checkbox = true en qué columnas va la información y si no, en qué columnas va cada textbox.

¿También dime porqué preguntas por el textbox1 y más adelante no lo ocupas?

También dime si en la hoja tienes encabezados y en qué fila empiezan los alumnos.

Muchas gracias por responder,, bien te envió lo que me pediste

1.- Cargo los datos en al iniciar la aplicación tanto en el combox1 y combox2:

Private Sub UserForm_Initialize()

Dim sd As New Collection
Dim celda As Range
Dim dato
Dim r As String
Dim uf As Integer


Dim mc As New Collection

Dim dat
Dim v As String
Dim hm As Integer

Application.ScreenUpdating = False
On Error Resume Next
ComboBox1.Clear
uf = Sheets("alumnos").Range("A" & Rows.Count).End(xlUp).Row
r = "A2:A" & uf
For Each celda In Sheets("alumnos").Range(r)
sd.Add celda.Value, CStr(celda.Value)
Next celda
For Each dato In sd
ComboBox1.AddItem dato
Next dato

ComboBox2.Clear
hm = Sheets("alumnos").Range("B" & Rows.Count).End(xlUp).Row
v = "B2:B" & hm
For Each celda In Sheets("alumnos").Range(v)
mc.Add celda.Value, CStr(celda.Value)
Next celda
For Each dat In mc
ComboBox2.AddItem dat
Next dat

Application.ScreenUpdating = True

End Sub

esta es ( tal vez  no es el código "limpio" recién empece en vba =) )

tambien te envio el codigo de los combox:

Private Sub ComboBox1_Change()

Application.ScreenUpdating = False
Dim fila, a As Integer
Dim dato, var As String
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
TextBox5 = Empty
TextBox6 = Empty
TextBox7 = Empty
TextBox8 = Empty
TextBox9 = Empty
TextBox10 = Empty
TextBox11 = Empty
TextBox12 = Empty
TextBox13 = Empty
TextBox14 = Empty
TextBox15 = Empty
TextBox16 = Empty
TextBox17 = Empty
TextBox18 = Empty


a = 0
fila = 1

While Sheets("alumnos").Cells(fila, 1) <> Empty
dato = ComboBox1

var = Sheets("alumnos").Cells(fila, 1)
If var = dato Then
dir = Sheets("alumnos").Cells(fila, 1).Address(False, False)
TextBox1 = Sheets("alumnos").Cells(fila, 1)
TextBox2 = Sheets("alumnos").Cells(fila, 2)
TextBox3 = Sheets("alumnos").Cells(fila, 3)
TextBox4 = Sheets("alumnos").Cells(fila, 5)
TextBox6 = Sheets("alumnos").Cells(fila, 6)
TextBox7 = Sheets("alumnos").Cells(fila, 7)
TextBox8 = Sheets("alumnos").Cells(fila, 8)
TextBox9 = Sheets("alumnos").Cells(fila, 9)
TextBox10 = Sheets("alumnos").Cells(fila, 10)
TextBox11 = Sheets("alumnos").Cells(fila, 11)
TextBox12 = Sheets("alumnos").Cells(fila, 12)
TextBox13 = Sheets("alumnos").Cells(fila, 13)
TextBox14 = Sheets("alumnos").Cells(fila, 14)
TextBox15 = Sheets("alumnos").Cells(fila, 15)
TextBox16 = Sheets("alumnos").Cells(fila, 16)
TextBox17 = Sheets("alumnos").Cells(fila, 17)
TextBox18 = Sheets("alumnos").Cells(fila, 18)
End If

fila = fila + 1
Wend


Application.ScreenUpdating = True
End Sub

- El combox2 es igual solo que en la fila2

- Bueno el código de arriba es algo que intente hacer con unos manuales que encontré y no se bien si esta bien utilizado pero los textbox reflejan los datos buscados por ejemplo si pongo jose en el combox1, en el texbox1 me aparece jose, en el texbox2= cuba, textxbox3=18, textbox4=11,,, ¿no se si me dejo entender?..

- Los checkbox son solamente para habilitar y desahabilitar los combox no tiene mayor relevancia,

-Bueno el texbox 1 me refleja el nombre del alumno a buscar..

- La hoja de datos si tienen encabezados que empiezan desde la A1 hasta la R1 con bastantes datos, como nombre, apellido, asistencia, 1examen, 2examen, 3examen, estado, etc) los demás campos ya los puedo añadir yo si me ayudas como hacer los 2 o 3 primeros

Y los datos empiezan desde la fila 2

Nuevamente gracias y si no fui muy claro por favor déjamelo saber y así puedo darte más información, gracias.

Te anexo la macro para actualizar el registro.

Private Sub CommandButton3_Click()
'Act.Por.Dante Amor
    Set h1 = Sheets("alumnos")
    If TextBox1 = "" Or TextBox2 = "" Or TextBox3 = "" Or TextBox4 = "" Then
        MsgBox "Campos requeridos vacíos favor complete"
        TextBox2.SetFocus
        Exit Sub
    End If
    '
    If ComboBox1 <> "" Then f = ComboBox1.ListIndex + 2
    If ComboBox2 <> "" Then f = ComboBox2.ListIndex + 2
    h1.Cells(f, "A") = TextBox1
    h1.Cells(f, "B") = TextBox2
    h1.Cells(f, "C") = TextBox3
    h1.Cells(f, "D") = TextBox4
    h1.Cells(f, "E") = TextBox5
    h1.Cells(f, "F") = TextBox6
    h1.Cells(f, "G") = TextBox7
    h1.Cells(f, "H") = TextBox8
    MsgBox "Datos actualizados correctamente", vbInformation, "ALUMNOS"
    limpia
End Sub
Sub limpia()
'Por.Dante Amor
    ComboBox1 = ""
    ComboBox2 = ""
    TextBox1 = ""
    TextBox2 = ""
    TextBox3 = ""
    TextBox4 = ""
    TextBox5 = ""
    TextBox6 = ""
    TextBox7 = ""
    TextBox8 = ""
    TextBox2.Locked = True
    TextBox3.Locked = True
    TextBox4.Locked = True
    TextBox5.Locked = True
    TextBox6.Locked = True
    TextBox7.Locked = True
    TextBox8.Locked = True
End Sub

Saludos.Dante Amor

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas