Sei sulla pagina 1di 15

INGRESAR DATOS MEDIANTE UN FORMULARIO

Ejercicio 1:

TextBox1
TextBox2
TextBox3

CommandButton1

CAJAS DE TEXTO
Private Sub TextBox1_Change()
Range("A2").Select
ActiveCell.FormulaR1C1 = TextBox1
End Sub
Private Sub TextBox2_Change()
Range("B2").Select
ActiveCell.FormulaR1C1 = TextBox2
End Sub
Private Sub TextBox3_Change()
Range("C2").Select
ActiveCell.FormulaR1C1 = TextBox3
End Sub

BOTN DE INSERTAR
Private Sub CommandButton1_Click()
Selection.EntireRow.Insert
TextBox1 = Empty
TextBox2 = Empty
TextBox1.SetFocus
End Sub

INGRESAR DATOS CON UN FORMULARIO RENOMBRANDO LAS CAJAS DE TEXTO


Ejercicio 2:

UFNombre
UFEdad
UFFecha

|
Uf_Agregar

CommandButton2

CAJAS DE TEXTO
Private Sub UFNombre_Change()
Range("A3").Select
ActiveCell.FormulaR1C1 = UFNombre
End Sub
Private Sub UFEdad_Change()
Range("B3").Select
ActiveCell.FormulaR1C1 = UFEdad
End Sub
Private Sub UFFecha_Change()
Range("C3").Select
ActiveCell.FormulaR1C1 = UFFecha
End Sub

BOTONES AGREGAR Y CANCELAR


Private Sub Uf_Agregar_Click()
Selection.EntireRow.Insert
UFNombre = Empty
UFEdad = Empty
UFFecha = Empty
UFNombre.SetFocus
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub

HACIENDO CLCULOS DESDE UN TEXTBOX


Ejercicio 3:

TextBox1
TextBox2
TextBox3
CommanBotton

CAJAS DE TEXTO
Private Sub TextBox1_Change()
Range("A2").Select
ActiveCell.FormulaR1C1 = TextBox1
End Sub

Private Sub TextBox2_Change()


Range("B2").Select
ActiveCell.FormulaR1C1 = TextBox2
Rem aqu se crea la Formula
TextBox3 = Val(TextBox2) * 365
Rem El Textbox3 guardara el total de la multiplicacin del Textbox2 por 365
Rem El Comando Val permite convertir un valor de Texto a un Valor Numrico
Rem Esto se debe a que los Textbox no son Numricos y debemos de Convertirlos
End Sub

Private Sub TextBox3_Change()


Range("C2").Select
ActiveCell.FormulaR1C1 = TextBox3
End Sub

BOTN DE RESULTADO
Private Sub CommandButton1_Click()
Selection.EntireRow.Insert
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox1.SetFocus
End Sub

HACIENDO CLCULOS DESDE UN TEXTBOX


Ejercicio 4:

TextBox1

TextBox2
TextBox3
TextBox4
TextBox5

CommandButton

CAJAS DE TEXTO
Private Sub TextBox1_Change()
Range("A3").Select
ActiveCell.FormulaR1C1 = TextBox1
End Sub

Private Sub TextBox2_Change()


Range("B3").Select
ActiveCell.FormulaR1C1 = TextBox2
End Sub

Private Sub TextBox3_Change()


Range("C3").Select
ActiveCell.FormulaR1C1 = TextBox3
End Sub
Private Sub TextBox4_Change()
Range("D3").Select
ActiveCell.FormulaR1C1 = TextBox4
Rem aqu se crea la formula
TextBox5 = Val(TextBox2) * Val(TextBox3) + Val(TextBox4)

Rem El TextBox5 guardara el total


End Sub
Private Sub TextBox5_Change()
Range("E3").Select
ActiveCell.FormulaR1C1 = TextBox5
End Sub

BOTN INSERTAR
Private Sub CommandButton1_Click()
Selection.EntireRow.Insert
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
TextBox5 = Empty
TextBox1.SetFocus
End Sub

BUSCANDO INFORMACIN CON UN TEXTBOX


Ejercicio 5: Disea un formulario como el siguiente:
Ver solucin paso a paso en pgina 31en el documento GUIA06COM118_Macros_2012.pdf.

Inserta cada una de las etiquetas, textbox y botn de comando necesarios.

CAJA DE TEXTO
Private Sub TextBox1_Change()
Range("A3").Select
ActiveCell.FormulaR1C1 = TextBox1
End Sub

BOTN BUSCAR AHORA


Private Sub CommandButton1_Click()
Cells.Find(What:=TextBox1, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
End Sub

ELABORANDO UNA CONSULTA


Ejercicio 6: Disea un formulario como el siguiente:
Ver solucin paso a paso en pgina 32 del documento GUIA06COM118_Macros_2012.pdf.

CUADROS DE TEXTO
Private Sub TextBox1_Change()
Range("A3").FormulaR1C1 = TextBox1
Rem esta primer lnea reemplaza a estas dos que te parece todava ms corta
Range ("A3").Select
ActiveCell.FormulaR1C1 = TextBox1
End Sub

Private Sub TextBox2_Change()


Range("B3").FormulaR1C1 = TextBox2
End Sub

Private Sub TextBox3_Change()


Range("C3").FormulaR1C1 = TextBox3
End Sub

BOTON DE CONSULTA
Private Sub CommandButton1_Click()
On Error Goto noencontro
Rem esta lnea genera una trampa de error si Excel encuentra un error se le dice que se vaya a
la etiqueta noencontro que esta definida mas adelante en el cdigo. No use la trampa de error
si no tiene problemas a la hora de que no encuentra a la persona. Recuerde si usted comete
cualquier error Excel se dirigir a la etiqueta noencontro.y esquivara cualquier error, hasta
uno que usted cometa en la programacin.
Cells.Find(What:=TextBox1, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _

False).Activate
ActiveCell.Offset(0, 1).Select
TextBox2 = ActiveCell
ActiveCell.Offset(0, 1).Select
TextBox3 = ActiveCell
Rem Tambin se puede utilizar este cdigo para leer la informacin de las celdas lo que esta
en azul. La diferencia es que se asignan los valores a variables y despus se descargan a los
TextBoxs.
ActiveCell.Offset(0, 1).Select
Direccion = Activecell
ActiveCell.Offset(0, 1).Select
Telefono = Activecell
TextBox2 = Direccion
TextBox3 = Telefono
noencontro:
Rem Aqu se esquiva el error
End Sub

BOTON BAJA
Private Sub CommandButton2_Click()
Selection.EntireRow.Delete
Range("A3").Select
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox1.SetFocus
End Sub

BOTON INSERTAR
Private Sub CommandButton3_Click()
Range("A3").Select
Selection.EntireRow.Insert
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox1.SetFocus
End Sub

FORMULARIO CON COMBOBOX Y LISTBOX


Ejercicio 7: Disea un formulario que tenga ComboBox y ListBox

Insertar un cuadro combinado


disponible en las herramientas

Insertar un cuadro de lista


disponible en las herramientas

CUADRO DE TEXTO:
Private Sub TextBox1_Change()
Range("A3").Select
ActiveCell.FormulaR1C1 = TextBox1
End Sub

CUADRO COMBINADO:
Private Sub ComboBox1_Change()
Range("B3").Select
ActiveCell.FormulaR1C1 = ComboBox1
End Sub

CUADRO DE LISTA:
Private Sub ListBox1_Click()
Range("C3").Select
ActiveCell.FormulaR1C1 = ListBox1
End Sub

BOTN INSERTAR:
Private Sub CommandButton1_Click()
Selection.EntireRow.Insert
TextBox1 = Empty
TextBox1.SetFocus
End Sub

BOTN CERRAR:
Private Sub CommandButton2_Click()
Unload Me
End Sub

Nota:

Para cargar automticamente los elementos del ListBox y comboBox debes ir al final del cdigo y
pegar lo siguiente:
Private Sub UserForm_Activate()
Rem: Cargar los elementos del Combobox
ComboBox1.AddItem "Femenino"
ComboBox1.AddItem "Masculino"
Rem: Ahora los listbox
ListBox1.AddItem "ventas"
ListBox1.AddItem "Compras"
ListBox1.AddItem "Recursos Humanos"
ListBox1.AddItem "Contabilidad"
End Sub

Ejercicio 8: Disea un formulario que permita seleccionar de un listbox y de un combobox


Ahora si deseas tomar informacin de una celda y enviarla a un Combobox o Listbox puedes ver su
cdigo en la pgina 36, pero debes presionar un botn para que el listado se active.
Ahora si deseas agregar los datos al Listbox o Combobox sin ningn botn que presionar escribe el
cdigo que inicia al final de la pgina 37 del archivo GUIA06COM118_Macros_2012.pdf.

Label8, colocar en su
interior el nmero 2

CAJAS DE TEXTO:

Private Sub TextBox1_Change()


Range("A" + Label8).FormulaR1C1 = TextBox1
End Sub
Private Sub TextBox2_Change()
Range("B" + Label8).FormulaR1C1 = TextBox2
End Sub
Private Sub TextBox3_Change()
Range("C" + Label8).FormulaR1C1 = TextBox3
End Sub
Private Sub TextBox4_Change()
Range("D" + Label8).FormulaR1C1 = TextBox4
End Sub

Private Sub TextBox5_Change()


Range("E" + Label8).FormulaR1C1 = TextBox5
End Sub
Private Sub TextBox6_Change()
Range("F" + Label8).FormulaR1C1 = TextBox6
End Sub
Combobox:
Private Sub ComboBox1_Change()
Range("G" + Label8).FormulaR1C1 = ComboBox1
End Sub
Botn insertar
Private Sub CommandButton1_Click()
Rem si no se escribe nada en los Textboxs a la hora de insertar escribe No Tiene
If TextBox1 = Empty Then Range("A2").FormulaR1C1 = "No Tiene"
If TextBox2 = Empty Then Range("B2").FormulaR1C1 = "No Tiene"
If TextBox3 = Empty Then Range("C2").FormulaR1C1 = "No Tiene"
If TextBox4 = Empty Then Range("D2").FormulaR1C1 = "No Tiene"
If TextBox5 = Empty Then Range("E2").FormulaR1C1 = "No Tiene"
If TextBox6 = Empty Then Range("F2").FormulaR1C1 = "No Tiene"
Range("A2").Select
Selection.EntireRow.Insert
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
TextBox5 = Empty
TextBox6 = Empty
TextBox1.SetFocus
End Sub
Botn consultar:
Private Sub CommandButton2_Click()
On Error GoTo noencontro
Rem Cdigo para buscar, ya lo conocemos
Cells.Find(What:=TextBox1, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
ActiveCell.Offset(0, 1).Select
TextBox2 = ActiveCell
ActiveCell.Offset(0, 1).Select
TextBox3 = ActiveCell
ActiveCell.Offset(0, 1).Select
TextBox4 = ActiveCell
ActiveCell.Offset(0, 1).Select

TextBox5 = ActiveCell
ActiveCell.Offset(0, 1).Select
TextBox6 = ActiveCell
ActiveCell.Offset(0, 1).Select
TextBox7 = ActiveCell
Rem la etiqueta 4 toma el valor del rengln activo y permite modificar la informacin que
encontr, ya que modifiques la informacin presionas el botn actualizar.
Label8 = ActiveCell.Row
noencontro:
End Sub
Botn modificar
Private Sub CommandButton3_Click()
Rem Vuelve a indicar el rengln 9 para escribir en los Textboxs
Label8 = "2"
Range("A2").Select
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
TextBox5 = Empty
TextBox6 = Empty
TextBox1.SetFocus
End Sub
Debes ingresar al cdigo, ubicarse al final y agregar el siguiente cdigo para que aparezcan
automticamente las opciones en el combobox.
Private Sub UserForm_Activate()
ComboBox1.AddItem "Femenino"
ComboBox1.AddItem "Masculino"
End Sub

Ejercicio 9: Disea un formulario que permita consultar y modificar el dato que se encontr
Ver solucin paso a paso en pgina 41 del documento GUIA06COM118_Macros_2012.pdf.

Observa que en la Label4


se ha escrito en su interior
el nmero 2

Botn insertar:
Private Sub CommandButton1_Click()
Rem si no se escribe nada en los Textboxs a la hora de insertar escribe No Tiene
If TextBox1 = Empty Then Range("A2").FormulaR1C1 = "No Tiene"
If TextBox2 = Empty Then Range("B2").FormulaR1C1 = "No Tiene"
If TextBox3 = Empty Then Range("C2").FormulaR1C1 = "No Tiene"
Range("A2").Select
Selection.EntireRow.Insert
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox1.SetFocus
End Sub

Botn Consulta:
Private Sub CommandButton2_Click()
On Error GoTo noencontro
Rem Cdigo para buscar, ya lo conocemos
Cells.Find(What:=TextBox1, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
ActiveCell.Offset(0, 1).Select
TextBox2 = ActiveCell
ActiveCell.Offset(0, 1).Select
TextBox3 = ActiveCell
Rem la etiqueta 4 toma el valor del rengln activo y permite modificar la informacin que
encontr, ya que modifiques la informacin presionas el botn actualizar.
Label4 = ActiveCell.Row
noencontro:
End Sub

Botn Actualizar:

Private Sub CommandButton3_Click()


Rem Vuelve a indicar el rengln 2 para escribir en los Textboxs
Label4 = "2"
Range("A2").Select
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox1.SetFocus
End Sub

Caja de texto1:
Private Sub TextBox1_Change()
Range("A" + Label4).FormulaR1C1 = TextBox1
End Sub

Caja de texto2:
Private Sub TextBox2_Change()
Range("B" + Label4).FormulaR1C1 = TextBox2
End Sub

Caja de texto3:
Private Sub TextBox3_Change()
Range("C" + Label4).FormulaR1C1 = TextBox3
End Sub

Potrebbero piacerti anche