Es común ver que el diseño de formularios en VBA Excel es tan simple que solo basta con seleccionarlo y arrastrarlo al formulario. Existe la forma de escribir código en el que nos permite manipular el control mediante la codificación haciendo uso de la librería Controls.
Para ello debemos seguir los siguientes pasos:
1.-Ingresar al editor VBA. Alt+F11.
2.-Ir al menú Insertar, Userform.
3.-Escribimos el siguiente código:
Private Sub UserForm_Click()
UserForm1.Caption = "Controles"
UserForm1.Width = 458.25
UserForm1.Height = 247.5
'1ra forma
Dim controles As New Control
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblmatricula"
controles.Caption = "MATRICULA:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 12
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblnombre"
controles.Caption = "NOMBRE:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 42
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblsexo"
controles.Caption = "SEXO:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 72
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lbltelefono"
controles.Caption = "TELEFONO:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 102
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblemail"
controles.Caption = "EMAIL:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 132
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblobservaciones"
controles.Caption = "OBSERVACION:"
controles.Width = 120
controles.Height = 18
controles.Left = 18
controles.Top = 162
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
'2da forma
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtMatricula"
.Width = 114
.Height = 25.5
.Left = 120
.Top = 12
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
End With
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtNombre"
.Width = 198
.Height = 25.5
.Left = 120
.Top = 42
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
End With
Set CuadroCombinado = Me.Controls.Add("Forms.ComboBox.1")
With CuadroCombinado
.Name = "cboSexo"
.Width = 114
.Height = 25.5
.Left = 120
.Top = 72
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.AddItem ("Femenino")
.AddItem ("Masculino")
End With
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtTelefono"
.Width = 114
.Height = 25.5
.Left = 120
.Top = 102
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
End With
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtEmail"
.Width = 198
.Height = 25.5
.Left = 120
.Top = 132
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
End With
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtObservaciones"
.Width = 114
.Height = 34.5
.Left = 120
.Top = 162
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
.MultiLine = True
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdNuevo"
.Width = 78
.Height = 36
.Left = 336
.Top = 12
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Nuevo"
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdGuardar"
.Width = 78
.Height = 36
.Left = 336
.Top = 48
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Guardar"
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdBuscar"
.Width = 78
.Height = 36
.Left = 336
.Top = 84
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Buscar"
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdModificar"
.Width = 78
.Height = 36
.Left = 336
.Top = 120
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Modificar"
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdEliminar"
.Width = 78
.Height = 36
.Left = 336
.Top = 156
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Eliminar"
End With
End Sub
controles.Height = 18
controles.Left = 18
controles.Top = 12
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblnombre"
controles.Caption = "NOMBRE:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 42
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblsexo"
controles.Caption = "SEXO:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 72
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lbltelefono"
controles.Caption = "TELEFONO:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 102
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblemail"
controles.Caption = "EMAIL:"
controles.Width = 96
controles.Height = 18
controles.Left = 18
controles.Top = 132
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
Set controles = Me.Controls.Add("Forms.Label.1")
controles.Name = "lblobservaciones"
controles.Caption = "OBSERVACION:"
controles.Width = 120
controles.Height = 18
controles.Left = 18
controles.Top = 162
controles.Font.Bold = True
controles.FontName = "Arial"
controles.FontSize = 12
'2da forma
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtMatricula"
.Width = 114
.Height = 25.5
.Left = 120
.Top = 12
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
End With
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtNombre"
.Width = 198
.Height = 25.5
.Left = 120
.Top = 42
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
End With
Set CuadroCombinado = Me.Controls.Add("Forms.ComboBox.1")
With CuadroCombinado
.Name = "cboSexo"
.Width = 114
.Height = 25.5
.Left = 120
.Top = 72
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.AddItem ("Femenino")
.AddItem ("Masculino")
End With
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtTelefono"
.Width = 114
.Height = 25.5
.Left = 120
.Top = 102
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
End With
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtEmail"
.Width = 198
.Height = 25.5
.Left = 120
.Top = 132
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
End With
Set CajadeTexto = Me.Controls.Add("Forms.TextBox.1")
With CajadeTexto
.Name = "txtObservaciones"
.Width = 114
.Height = 34.5
.Left = 120
.Top = 162
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Text = ""
.MultiLine = True
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdNuevo"
.Width = 78
.Height = 36
.Left = 336
.Top = 12
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Nuevo"
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdGuardar"
.Width = 78
.Height = 36
.Left = 336
.Top = 48
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Guardar"
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdBuscar"
.Width = 78
.Height = 36
.Left = 336
.Top = 84
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Buscar"
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdModificar"
.Width = 78
.Height = 36
.Left = 336
.Top = 120
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Modificar"
End With
Set BotondeComando = Me.Controls.Add("Forms.CommandButton.1")
With BotondeComando
.Name = "cmdEliminar"
.Width = 78
.Height = 36
.Left = 336
.Top = 156
.Font.Bold = True
.FontName = "Arial"
.FontSize = 12
.Caption = "Eliminar"
End With
End Sub
4.-Ejecutamos la aplicación o presionar la tecla F5 y obtendremos como resultado:
Diseño de Userform con macro
No hay comentarios.:
Publicar un comentario