Está en la página 1de 6

Progamacion

- Crear formulario: entrar Visual Basic y entrar a (userform)


- Cuadros de texto= para escribir
- etiqueta= solo mensaje
- ab= botón de comando= para seleccionar, aceptar limpiar, calcular etc
- Prívate Sub = es para iniciar a editar
- CommanButton_Click = botón
- ‘ = se hace con el signo de pregunta y se usa para crear mensaje
- Texbox1.Value “” = cajas vacías
- Label = etiqueta (+)
- MsgBox : caja de mensaje
- Else if y if lleva Then
- If, ElseIf, Else, End If
- TextBox.Value =cajas de textos
- IsNumeric = Validar valores numericos
- Empty o “”= Cajas vacias. ‘Limpiar las cajas de texto Texbox1 = Empty o “”
- La función Val(Dato String), convierte una cadena de caracteres a valor numérico.
- Asignar una macro = Despues del formulario, crear modulo, poner Sub nombre () y abajo llamar el formulario con .
Show = UserForm1.Show
- String = Para texto

CALCULADORA
Elaborar una macro para calcular las operaciones básicas suma, resta multiplicación y división
Poner 1 etiqueta= calculadora
Poner 3 cuadros de texto = primer valor, segundo valor, resultado
Poner 2 tiquetas de = + y =
Poner un comando de suma, resta, mulplicacion y división

⁃ Se inicia con el primer botón dandole doble clic


Se abre CommanButton1
⁃ Primero que los textbox tengan valores = para validar se debe hacer con if, elseif, else, end if.

Info para poner Calculadora


Private Sub CommandButton1_Click()
'Macro para realizar la suma
'Condición para validar las cajas vacías
If (TextBox1.Value = "" Or TextBox2.Value = "") Then
MsgBox ("Ingreso invalido")
ElseIf (IsNumeric(TextBox1.Value) And IsNumeric(TextBox2.Value)) Then
TextBox3 = Round(Val(TextBox1.Value) + Val(TextBox2.Value), 2) redondear

Registro en celdas (Operación, valor 1, valor 2, total)


Range("A2") = "Suma"
Range("B2") = TextBox1.Value
Range("C2") = TextBox2.Value
Range("D2") = TextBox3.Value
Range("A2").Select
Selection.EntireRow.Insert para que se haga desde la celda A2

Else
MsgBox ("Valores invalidos")
'Limpiar las cajas de texto
TextBox1 = Empty
TextBox2 = ""
TextBox3 = ""

End If

End Sub

--------------------------------------------------------- Dar clic a siguiente caja


Private Sub CommandButton2_Click()
'Macro para realizar resta
'Condición para validar las cajas vacías
If (TextBox1.Value = "" Or TextBox2.Value = "") Then
MsgBox ("Ingreso invalido")
ElseIf (IsNumeric(TextBox1.Value) And IsNumeric(TextBox2.Value)) Then
TextBox3 = Round(Val(TextBox1.Value) - Val(TextBox2.Value), 2)

Range("A2") = "Resta"
Range("B2") = TextBox1.Value Para ubicar en una celda
Range("C2") = TextBox2.Value
Range("D2") = TextBox3.Value
Range("A2").Select
Selection.EntireRow.Insert
Else
MsgBox ("Valores invalidos")
'Limpiar las cajas de texto
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
End If
Label1 = "-"

End Sub
----------------------------------------------------------
Private Sub CommandButton3_Click()
'Macro para realizar multiplicacion
'Condicion para validar las cajas vacias
If (TextBox1.Value = "" Or TextBox2.Value = "") Then
MsgBox ("Ingreso invalido")
ElseIf (IsNumeric(TextBox1.Value) And IsNumeric(TextBox2.Value)) Then
TextBox3 = Round(Val(TextBox1.Value) * Val(TextBox2.Value), 2)

Range("A2") = "Multiplicacion"
Range("B2") = TextBox1.Value
Range("C2") = TextBox2.Value
Range("D2") = TextBox3.Value
Range("A2").Select
Selection.EntireRow.Insert
Else
MsgBox ("Valores invalidos")
'Limpiar las cajas de texto
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
End If
Label1 = "*"
End Sub
------------------------------------------------------------------------------
Private Sub CommandButton4_Click()
'Macro para realizar division
'Condicion para validar las cajas vacias
If (TextBox1.Value = "" Or TextBox2.Value = "") Then
MsgBox ("Ingreso invalido")
ElseIf (IsNumeric(TextBox1.Value) And IsNumeric(TextBox2.Value)) Then
TextBox3 = Round(Val(TextBox1.Value) / Val(TextBox2.Value), 2)

Range("A2") = "Division"
Range("B2") = TextBox1.Value
Range("C2") = TextBox2.Value
Range("D2") = Round(TextBox3.Value, 2)
Range("A2").Select
Selection.EntireRow.Insert
Else
MsgBox ("Valores invalidos")
'Limpiar las cajas de texto
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
End If
Label1 = "/"
End Sub

BRÚJULA
Elaborar una macro que solicite los grados (0 - 360)
mediante un formulario y de acuerdo al valor ingresado muestre los puntos cardinales Norte, Este, Oeste y Sur.
• El programa debe indicar el punto cardinal:
• a) Norte, al ingresar un valor entre 316 y 45 grados.
• b) Este, al ingresar un valor entre 46 y 135 grados.
• c) Sur, al ingresar un valor entre 136 y 225 grados.
° c) Oeste, al ingresar un valor entre 226 y 315 grados.
d) De lo contrario indicar "valor incorrecto
Formulario: ingrese un valor, punto cardinal , ab calcular

---------------------------------------------------------------------------
Private Sub CommandButton1_Click()

'Macro para indicar el punto cardinal


'Condición para validar la caja vacía
If (TextBox1.Value = "" Or TextBox1.Value < 0 Or TextBox1.Value > 360) Then
MsgBox ("Valores incorretos, ingresar grados entre (0 - 360)")

End If

If TextBox1.Value >= 46 And TextBox1.Value <= 135 Then


TextBox2.Value = "Este"
Range("E16") = "Este"

ElseIf TextBox1.Value >= 136 And TextBox1.Value <= 225 Then


TextBox2.Value = "Sur"
Range("D20") = "Sur"6

ElseIf TextBox1.Value >= 226 And TextBox1.Value <= 315 Then


TextBox2.Value = "Oeste"
Range("C16") = "Oeste"

ElseIf TextBox1.Value >= 0 And TextBox1.Value <= 45 Then


TextBox2.Value = "Norte"
Range("D12") = "Norte"

ElseIf TextBox1.Value >= 316 And TextBox1.Value <= 360 Then


TextBox2.Value = "Norte"
Range("D12") = "Norte"
Else
MsgBox ("Valores incorrectos")
TextBox1.Value = ""

End If

End Sub

VAR CADENA / NOTAS


Elaborar macro que pida tres notas de un alumno sin formulario.
mediante la función InputBox.
. Las notas se almacenarán en las celdas B1, B2 y B3 de la hoja activa.
• La macro calcula la media y entrega el resultado en B4.
• Si la media esta entre 0 y 2,9, colocar en B5 "Insuficiente"
* Si la media esta entre 3,0 y 4,0, colocar en B5 "Suficiente"
• Si la media esta entre 4,1 y 5,0, colocar en B5
"Sobresaliente"
WorksheetFunction.Average(Valor1,Valor2,ValorN)
Se asigna macro inicialmente
⁃ Abrió visual basic. Insertar- Modulo
-------------------------------------------------------------------------
Sub MacroString()
'1763556-ISABELA SALAZAR MARTINEZ
'Macro para el manejo de la variable String/Cadena
Dim VarCadena As String
VarCadena = InputBox("Ingrese un texto", "entrada o ingreso de datos")
MsgBox (VarCadena)
Range("D10") = VarCadena
End Sub
-----------------------------------------------------------------------------
Sub MacroInteger()
'1763556 - Isabela Salazar }
'Macro para el manejo de la variable Integer/Entera
Dim VarEntera1 As Integer
Dim VarEntera2 As Integer
VarEntera1 = InputBox("Ingrese in valor", "Ingreso de datos")
VarEntera2 = InputBox("Ingrese in valor", "Ingreso de datos")

Range("D12") = VarEntera1 + VarEntera2


Range("D13") = VarEntera1 - VarEntera2
Range("D14") = VarEntera1 * VarEntera2
Range("D15") = VarEntera1 / VarEntera2

End Sub
--------------------------------------------------------------------------------
Sub MacroDouble()
'1763556 - Isabela Salazar
'Macro para el manejo de la variable Double
Dim VarDouble1 As Double
Dim VarDouble2 As Double

VarDouble1 = InputBox("Ingrese un valor", "Ingreso de datos")


VarDouble2 = InputBox("Ingrese un valor", "Ingreso de datos")

Range("E12") = Round(VarDouble1 + VarDouble2, 2)


Range("E13") = Round(VarDouble1 - VarDouble2, 2)
Range("E14") = Round(VarDouble1 * VarDouble2, 2)
Range("E15") = Round(VarDouble1 / VarDouble2, 2)

End Sub
----------------------------------------------------------------------------------------
Sub MacroFecha()
'1763556 - Isabela Salazar
'Macro para el manejo de la variable Date
Dim VarDate As Date

VarDate = Now
Range("F12") = VarDate
Range("F13") = Time()
Range("F14") = Format(Weekday(VarDate), "dddd")
Range("F15") = Day(VarDate)
Range("F16") = Month(VarDate)
Range("F17") = Year(VarDate)
Range("F18") = Hour(VarDate)
Range("F19") = Minute(VarDate)
Range("F20") = Second(VarDate)
End Sub
--------------------------------------------------------------------------------------
Sub MacroNotas()
'1763556 - Isabela Salazar
'Macro para la variable Double
Dim VarDoble1 As Double
Dim VarDoble2 As Double
Dim VarDoble3 As Double

VarDoble1 = Val(InputBox("Ingrese un valor", "Ingreso de datos"))


VarDoble2 = Val(InputBox("Ingrese un valor", "Ingreso de datos"))
VarDoble3 = Val(InputBox("Ingrese un valor", "Ingreso de datos"))

Range("B1") = VarDoble1
Range("B2") = VarDoble2
Range("B3") = VarDoble3
Range("B4") = Round(WorksheetFunction.Average(VarDoble1, VarDoble2, VarDoble3), 2)

'Calificacion de notas
If (Range("B4") > 0 And Range("B4") <= 2.9) Then

Range("B5") = "Insuficiente"

ElseIf (Range("B4") >= 3 And Range("B4") <= 4) Then


Range("B5") = "Suficiente"

Else

Range("B5") = "Sobresaliente"
End If

End Sub

---------------------------------------------------------------------------

IMC

el Excel con dibujo


Sub MacroIMCGrabadora()
'
' MacroIMCGrabadora Macro
' 1763556 - Isabela Salazar Martinez

' Ingreso de la peso por el usuario


Range("D2") = InputBox("Ingrese su peso", "Peso en Kg")
' Ingreso de la estatura por el usuario
Range("D3") = InputBox("Ingrese su estatura", "Estatura en Mt")

Range("D4").Select
ActiveCell.FormulaR1C1 = "=(R[-2]C)/R[-1]C^2"
Range("D4").Select

'Construir la funcion Si
If (Range("D4") < 18.5) Then
Range("D5") = "Infrapeso"
ElseIf (Range("D4") >= 18.5 And Range("D4") < 25) Then Range("D5") = "Normal"
ElseIf (Range("D4") >= 25 And Range("D4") < 30) Then Range("D5") = "Sobrepeso"
Else
Range("D5") = "Obeso"
End If
End sud

UserForm1
Private Sub CommandButton1_Click()
'CALCULAR IMC
TextBox3.Value = TextBox2.Value / (TextBox1.Value * TextBox1.Value)

'Encontrar la clasificacion
If (TextBox3.Value < 18.5) Then
TextBox4.Value = "Infrapeso"

ElseIf (TextBox3.Value >= 18.5 And TextBox3.Value < 25) Then


TextBox4.Value = "Peso Normal"

ElseIf (TextBox3.Value >= 25 And TextBox3.Value < 30) Then


TextBox4.Value = "Sobrepeso"

Else
TextBox4.Value = "Obeso"
End If

También podría gustarte