Excel III - VBA - Formularios - I
-
Upload
roly-manuico-flores -
Category
Documents
-
view
39 -
download
13
Transcript of Excel III - VBA - Formularios - I
Mic
ro
so
ft O
ffic
e E
xc
el
20
07
FORMULARIO DE EXCEL - EDITOR DE VBA
Pasos para Crear Un Diseño De Formulario:
Lo primero que se debe hacer es ingresar al Microsoft Excel 2010,y Diseñar La Siguiente Lista De EMPLEADOS En Una Hoja Nueva Llamada “CONTROL DE EMPLEADOS”:
luego presionar las teclas ALT + F11, y se mostrara la ventana de Editor De Visual Basic ó VBA. Luego Ir Al Menú Insertar – UserForm.
Luego Insertar Los Siguientes Sobre El Formulario:
*- 7 ETIQUETAS. *- 5 CUADROS DE TEXTO.
*- 1 CUADRO COMBINADO. *- 2 BOTONES DE COMANDO.
El Diseño Se Mostrara Así:
Prof: Mañuico Flores, Roly. Sede: ATE – LOS OLIVOSCell: 9 - 9233 - 7013 P á g i n a | 1 E-Mail: [email protected]
Utilizar La PROPIEDAD CAPTION, Para Los Controles LABEL o Etiquetas.
Utilizar La PROPIEDAD NAME, Para Los Controles TEXTBOX o Cuadro De Texto.
Utilizar La PROPIEDAD NAME, Para Los Controles COMMANDBUTTON o Boton De Comando.
Utilizar La PROPIEDAD NAME, Para El Formulario, Su Nombre Sera: FRMINGRESO.
Mic
ro
so
ft O
ffic
e E
xc
el
20
07
Luego realizar Doble Clic En El Formulario y Ubicar El Evento “Initialize()”, y Copiar El Siguiente Codigo:
Private Sub UserForm_Initialize()
‘Cargamos Los Datos En El Cuadro Combinado
CBSEXO.AddItem ("Masculino")CBSEXO.AddItem ("Femenino")
'Autogeneramos El NumeroWorksheets("CONTROL DE EMPLEADOS").ActivateRange("B4").SelectIf ActiveCell = Empty Then TXTNUMERO.Enabled = False TXTNUMERO.Text = 1Else Range("B2").End(xlDown).Select TXTNUMERO.Enabled = False TXTNUMERO.Text = ActiveCell.Value + 1End If
End Sub
Luego realizar Doble Clic En El Formulario y Ubicar El Evento “QueryClose”y Copiar El Siguiente Codigo:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
‘Anulamos El Boton Cerrar (X), y Solo Cerraremos El Formulario Con El Boton SALIR.
If CloseMode = vbFormControlMenu Then Cancel = True MsgBox ("Utilize El Boton Salir") End If
End Sub
Luego realizar Doble Clic En El BOTON SALIR y Copiar El Siguiente Codigo:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
‘Anulamos El Boton Cerrar (X), y Solo Cerraremos El Formulario Con El Boton SALIR.
Dim Mensaje As String
Mensaje = MsgBox("Desea Salir", vbQuestion + vbYesNo, "Aviso")
If Mensaje = vbYes Then Unload MeEnd If
End Sub
Prof: Mañuico Flores, Roly. Sede: ATE – LOS OLIVOSCell: 9 - 9233 - 7013 P á g i n a | 2 E-Mail: [email protected]
Mic
ro
so
ft O
ffic
e E
xc
el
20
07
Luego realizar Doble Clic En El BOTON INGRESAR y copiar los siguiente códigos:
Private Sub CmdIngresar_Click()
‘Verificamos Que Todos Los Campos Esten LlenosIf TXTNOMBRE = "" Or TXTDIRECCION = "" Or TXTTELEFONO = "" Or CBSEXO = "" Or TXTEDAD = "" Then MsgBox ("Ingrese Todos Los Datos...!") Exit SubEnd If
'Verificamos Que El Campo De Edad Sea NumericoIf Not IsNumeric(TXTEDAD) Then MsgBox ("Ingrese Una Edad Numerica...!") TXTEDAD.Text = "" Exit SubEnd If
'Verificamos Que Se Ingrese Una Edad RazonableIf TXTEDAD.Value <= 0 Or TXTEDAD.Value > 60 Then MsgBox ("Ingrese Una Edad Adecuada...!") TXTEDAD.Text = "" Exit SubEnd If
'Comensamos El Ingreso De Los Datos.Sheets("CONTROL DE EMPLEADOS").ActivateRange("B2").Select
If ActiveCell.Offset(1, 0).Value = "" Then Exit SubEnd If
Selection.End(xlDown).ActivateActiveCell.Offset(1, 0).Activate
ActiveCell.Value = Val(TXTNUMERO.Text)ActiveCell.Offset(0, 1).Value = TXTNOMBRE.TextActiveCell.Offset(0, 2).Value = TXTDIRECCION.TextActiveCell.Offset(0, 3).Value = TXTTELEFONO.TextActiveCell.Offset(0, 4).Value = CBSEXO.TextActiveCell.Offset(0, 5).Value = Val(TXTEDAD.Text)
'Aqui Volvemos A Autogenerar El CodigoRange("B2").End(xlDown).SelectTXTNUMERO.Text = ActiveCell.Value + 1
'Limpia Las Cajas De Texto Para Un Nuevo IngresoTXTNOMBRE.Text = ""TXTDIRECCION.Text = ""TXTTELEFONO.Text = ""CBSEXO.Text = ""TXTEDAD.Text = ""
TXTNOMBRE.SetFocus
End Sub
Prof: Mañuico Flores, Roly. Sede: ATE – LOS OLIVOSCell: 9 - 9233 - 7013 P á g i n a | 3 E-Mail: [email protected]
Mic
ro
so
ft O
ffic
e E
xc
el
20
07
Luego Ir Al Menú Insertar – MODULO.
En la ventana que se muestra crear el Siguiente Procedimiento:
Luego Activar La Ficha Programador e Insertar 2 Controles Botón(Controles De Formulario) y Asignarle Los procedimientos Creados (Limpiar_Filas – Llamar_Formulario), que en realidad es UNA MACRO que Limpia Un Fila Seleccionada y La Otra Llama Al Formulario Diseñado:
Luego realizar 12 ingresos Para Probar Nuestro Formulario Creado. Luego Limpiar 4 Registros Para Probar El BOTON ELIMINAR REGISTRO SELECCIONADO. Luego Proteger La Hoja De Excel 2007 – FICHA REVISAR – BOTON PROTEGER HOJA,
Asignarle Una Contraseña.
Prof: Mañuico Flores, Roly. Sede: ATE – LOS OLIVOSCell: 9 - 9233 - 7013 P á g i n a | 4 E-Mail: [email protected]