Problema con un formulario y combobox en excel

Hola Expertos:
Soy bastante novato con el tema de VBA pero poco a poco voy
aprendiendo más y cada vez me veo más suelto aunque tengo que decir que empecé
hace una semana.
Paso a explicar mi problema:
Tengo un formulario en Excel el cual carga de una base de datos
los datos de los pacientes con la posibilidad de si se cambia algún dato que lo
guarde dándole a un botón (CommandButton1)
En este formulario hay un combobox principal (ComboBox15)
Donde carga los nombres de los pacientes y con la rutina que veis abajo carga
los datos de las siguientes columnas de ese paciente.
Cuando carga los datos hay otros combobox dentro del
paciente que deja elegir por ejemplo su profesión, su patología…..
El problema que tengo es que si me viene un paciente con una
patología que no está introducida en la hoja donde tengo las patologías me
toca:
1)
Salir del formulario.
2)
Ir a la hoja donde guardo las patologías.
3)
Introducir la patología nueva.
4)
Volver a la hoja donde tengo la base de datos de
los pacientes.
5)
Darle al formulario. Etc
Mi idea es en el formulario poner al lado de los combobox
secundarios un botón con la rutina para introducir un nuevo dato y guardarlo
para que después sin tener que salir del formulario abrir el desplegable y que
me salga para poder guardar los nuevos datos del paciente.
El inconveniente que tengo es que si pongo esa rutina dentro
del formulario deseleccionare al paciente que está en la base de datos para que
seleccione donde tengo las patologías por lo que al guardar ya no tendrá como
referencia la casilla del nombre de mi paciente y no podrá guardar según la
rutina en las columnas adyacentes.
Espero haberme explicado con algo de claridad.
¿Alguna solución para este inexperto?
Muchas gracias!
Private Sub ComboBox15_Enter()
On Error
Resume Next
ComboBox1.Clear
Hoja1.Select
Range("B8").Select
Do While
Not IsEmpty(ActiveCell)
ComboBox15.AddItem
ActiveCell.Value
ActiveCell.Offset(1,
0).Select
Loop
End Sub
Private Sub
ComboBox15_Change()
Cells(ComboBox15.ListIndex
+ 8, 2).Select
TextBox2 =
ActiveCell.Offset(0, 1)
TextBox3 =
ActiveCell.Offset(0, 2)
TextBox4 =
ActiveCell.Offset(0, 3)
TextBox5 =
ActiveCell.Offset(0, 4)
TextBox6 =
ActiveCell.Offset(0, 5)
TextBox7 =
ActiveCell.Offset(0, 6)
TextBox8 =
ActiveCell.Offset(0, 7)
TextBox9 = ActiveCell.Offset(0,
8)
ComboBox1 =
ActiveCell.Offset(0, 9)
ComboBox2 =
ActiveCell.Offset(0, 10)
ComboBox3 =
ActiveCell.Offset(0, 19)
TextBox13 =
ActiveCell.Offset(0, 11)
TextBox14 =
ActiveCell.Offset(0, 12)
TextBox15 =
ActiveCell.Offset(0, 13)
TextBox16 =
ActiveCell.Offset(0, 14)
TextBox17 =
ActiveCell.Offset(0, 15)
TextBox18 =
ActiveCell.Offset(0, 16)
TextBox19 =
ActiveCell.Offset(0, 17)
TextBox20 =
ActiveCell.Offset(0, 18)
TextBox21 =
ActiveCell.Offset(0, 20)
ComboBox4 =
ActiveCell.Offset(0, 24)
ComboBox5 =
ActiveCell.Offset(0, 25)
ComboBox6 =
ActiveCell.Offset(0, 26)
ComboBox7 =
ActiveCell.Offset(0, 27)
ComboBox8 =
ActiveCell.Offset(0, 28)
ComboBox9 =
ActiveCell.Offset(0, 29)
TextBox21 =
ActiveCell.Offset(0, 30)
TextBox22 =
ActiveCell.Offset(0, 31)
ComboBox10
= ActiveCell.Offset(0, 32)
TextBox23 =
ActiveCell.Offset(0, 33)
TextBox24 =
ActiveCell.Offset(0, 34)
ComboBox11
= ActiveCell.Offset(0, 35)
TextBox25 =
ActiveCell.Offset(0, 36)
TextBox26 =
ActiveCell.Offset(0, 37)
ComboBox12
= ActiveCell.Offset(0, 38)
TextBox27 =
ActiveCell.Offset(0, 39)
TextBox28 =
ActiveCell.Offset(0, 40)
ComboBox13
= ActiveCell.Offset(0, 41)
TextBox29 =
ActiveCell.Offset(0, 42)
TextBox30 =
ActiveCell.Offset(0, 43)
ComboBox14
= ActiveCell.Offset(0, 44)
TextBox31 =
ActiveCell.Offset(0, 45)
TextBox32 =
ActiveCell.Offset(0, 46)
End Sub
Private Sub
CommandButton1_Click()
If ComboBox15 <> "" Then
ActiveSheet.Unprotect ("*")
Cells(ComboBox15.ListIndex
+ 8, 2).Select
ActiveCell.Offset(0,
1) = (TextBox2)
ActiveCell.Offset(0,
2) = (TextBox3)
ActiveCell.Offset(0,
3) = (TextBox4)
ActiveCell.Offset(0,
4) = TextBox5
ActiveCell.Offset(0,
5) = TextBox6
ActiveCell.Offset(0,
6) = TextBox7
ActiveCell.Offset(0,
7) = TextBox8
ActiveCell.Offset(0,
8) = TextBox9
ActiveCell.Offset(0,
9) = ComboBox1
ActiveCell.Offset(0,
10) = ComboBox2
ActiveCell.Offset(0,
19) = ComboBox3
ActiveCell.Offset(0,
11) = TextBox13
ActiveCell.Offset(0,
12) = TextBox14
ActiveCell.Offset(0,
13) = TextBox15
ActiveCell.Offset(0,
14) = TextBox16
ActiveCell.Offset(0,
15) = TextBox17
ActiveCell.Offset(0,
16) = TextBox18
ActiveCell.Offset(0,
17) = TextBox19
ActiveCell.Offset(0,
18) = TextBox20
ActiveCell.Offset(0,
24) = ComboBox4
ActiveCell.Offset(0,
25) = ComboBox5
ActiveCell.Offset(0,
26) = ComboBox6
ActiveCell.Offset(0,
27) = ComboBox7
ActiveCell.Offset(0,
28) = ComboBox8
ActiveCell.Offset(0,
29) = ComboBox9
ActiveCell.Offset(0,
30) = TextBox21
ActiveCell.Offset(0,
31) = TextBox22
ActiveCell.Offset(0,
32) = ComboBox10
ActiveCell.Offset(0,
33) = TextBox23
ActiveCell.Offset(0,
34) = TextBox24
ActiveCell.Offset(0,
35) = ComboBox11
ActiveCell.Offset(0,
36) = TextBox25
ActiveCell.Offset(0,
37) = TextBox26
ActiveCell.Offset(0,
38) = ComboBox12
ActiveCell.Offset(0,
39) = TextBox27
ActiveCell.Offset(0,
40) = TextBox28
ActiveCell.Offset(0,
41) = ComboBox13
ActiveCell.Offset(0,
42) = TextBox29
ActiveCell.Offset(0,
43) = TextBox30
ActiveCell.Offset(0,
44) = ComboBox14
ActiveCell.Offset(0,
45) = TextBox31
ActiveCell.Offset(0, 46) = TextBox32
ComboBox15.Clear
TextBox2 =
""
TextBox3 =
""
TextBox4 =
""
TextBox5 =
""
TextBox6 =
""
TextBox7 =
""
TextBox8 =
""
TextBox9 =
""
ComboBox1 =
""
ComboBox2 =
""
ComboBox3 =
""
TextBox13 =
""
TextBox14 =
""
TextBox15 =
""
TextBox16 =
""
TextBox17 =
""
TextBox18 =
""
TextBox19 =
""
TextBox20 =
""
TextBox21 =
""
TextBox22 =
""
ComboBox10
= ""
TextBox23 =
""
TextBox24 =
""
ComboBox11
= ""
TextBox25 =
""
TextBox26 =
""
ComboBox12
= ""
TextBox27 =
""
TextBox28 =
""
ComboBox13
= ""
TextBox29 =
""
TextBox30 =
""
ComboBox14
= ""
TextBox31 =
""
TextBox32 =
""
TextBox2.SetFocus
ActiveSheet.Protect...

1 Respuesta

Respuesta
1
Dale no hay problemas.
Necesitas pasarle la nueva patologuia a la hoja desde el form y luego de eso volver a cargar el combo de pataloguias para que se registre el nuevo dato agregado.
Entonces, suponiendo que la hoja en cuestión es la Hoja2 y las patologuias las cargas en la columna A.
Lo primero es contar cualtos elementos existen en A de hoja 2, a fin de poder ubicar el nuevo dato en la fila que le corresponde
Entonces
Private Sub CommandButton1_Click()
Dim fila As Long
Dim i As Long
    If Len(Trim(TextBox1)) = 0 Then Exit Sub
    'contamos elementos de A hoja2
    fila = Application.WorksheetFunction.CountA(Sheets(2).Range("A:A")) + 1
    Sheets(2). Range("A" & fila) = Trim(TextBox1) ' pasamos valor det text a col A hoja2
    ' limpia combo y carga lista actualizada
    ComboBox1.Clear
    For i = 1 To (fila + 1)
        ComboBox1.AddItem Sheets(2).Range("A" & i)
    DoEvents
    Next
    ComboBox1.ListIndex = 0
    TextBox1 = ""
End Sub
Nota: Evita ser bloqueado CIERRA las preguntas si la respuesta es satisfactoria

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas