UNIDAD 4
Actividad 1
Insertar ventana de color usando " Userform"
Private Sub UserForm_Initialize()
Me.BackColor = vbBlue
End Sub
Actividad 2
Insertar botón para ventana personalizada
Private Sub CommandButton1_Click()
UserForm1.Show
End Sub
botón de ventana de mensaje
Private Sub CommandButton2_Click()
MsgBox "Hola, como te va?"
End Sub
boton de mensaje de exclamación
Private Sub CommandButton3_Click()
MsgBox "seguimos!", vbExclamation
End Sub
Mensaje con titulo "vamos estudiantes"
Private Sub CommandButton4_Click()
MsgBox "Vamos estudiantes", vbYesNo, "programacion electrica"
End Sub
Mensaje de si o no
Private Sub CommandButton5_Click()
MsgBox "Mighty Chase", vbYesNo + vbExclamation, "Electrica"
End Sub
Pintar una celda
Private Sub CommandButton6_Click()
Dim res As Variant
res = MsgBox("¿Quieres pintar de rojo A1?", vbYesNo, "Colorear A1")
If res = vbYes Then
Range("A1").Interior.Color = vbRed
Else
Range("A1").Interior.Color = vbWhite
End If
End Sub
ventana que pida un dato, Ejemplo: dame tu nombre".
Private Sub CommandButton7_Click()
Dim mivar As Variant
mivar = Application.InputBox("Dame tu nombre:", "Nombre")
Range("A2") = mivar
End Sub
Botón para pedir edad
Private Sub CommandButton1_Click()
Dim edad As Integer
edad = Application.InputBox(prompt:="indique su edad:", Type:=1) '1 es numerico
End Sub
preguntar si eres mayor de edad
Private Sub CommandButton2_Click()
Dim correcto As Variant
correcto = Application.InputBox(prompt:="Eres mayor de edad?", Type:=4)
'verdadero o falso
End Sub
Nombre de un amigo
Private Sub CommandButton3_Click()
Dim miamigo As String
miamigo = Application.InputBox(prompt:="indica el nombre de tu amigo", Default:="Lupita", Type:=2)
'type 2 es texto
Actividad 12
seleccionar valor
Private Sub CommandButton4_Click()
Dim sele As Range
'dim sele as integer
'sele = Application.InputBox(prompt:="selecciona una celda", Type:=8)
Set sele = Application.InputBox(prompt:="selecciona una celda", Type:=8) 'range
'Type 8 referencia a una celda
End Sub
Calculando números complejos
Suma de resistencias UNO
Sub resistencia()
Dim col1 As Integer
Dim col2 As Integer
MsgBox "0 negro,1 cafe, 2 rojo,3 naranja,4 amarillo,5 verde, 6 azul,7 violeta,8 gris,9 blanco"
col1 = Application.InputBox("color", "resistencia")
'Range("B1") = COL1
If col1 = 0 Then
Range("B1").Interior.Color = RGB(0, 0, 0)
End If
If col1 = 1 Then
Range("C1").Interior.Color = RGB(147, 81, 22)
End If
If col1 = 2 Then
Range("D1").Interior.Color = RGB(255, 0, 51)
End If
If col1 = 3 Then
Range("E1").Interior.Color = RGB(243, 156, 18)
End If
If col1 = 4 Then
Range("F1").Interior.Color = RGB(241, 196, 15)
End If
If col1 = 5 Then
Range("G1").Interior.Color = RGB(46, 204, 113)
End If
If col1 = 6 Then
Range("H1").Interior.Color = RGB(46, 134, 193)
End If
If col1 = 7 Then
Range("I1").Interior.Color = RGB(155, 89, 182)
End If
If col1 = 8 Then
Range("J1").Interior.Color = RGB(191, 201, 202)
End If
If col1 = 9 Then
Range("K1").Interior.Color = RGB(248, 249, 249)
End If
'***********************************************************************************************************
MsgBox "0 negro,1 cafe, 2 rojo,3 naranja,4 amarillo,5 verde, 6 azul,7 violeta,8 gris,9 blanco"
col2 = Application.InputBox("color", "resistencia")
'Range("B1") = COL1
If col2 = 0 Then
Range("C1").Interior.Color = RGB(0, 0, 0)
End If
If col2 = 1 Then
Range("C1").Interior.Color = RGB(147, 81, 22)
End If
If col2 = 2 Then
Range("C1").Interior.Color = RGB(255, 0, 51)
End If
If col2 = 3 Then
Range("C1").Interior.Color = RGB(243, 156, 18)
End If
If col2 = 4 Then
Range("C1").Interior.Color = RGB(241, 196, 15)
End If
If col2 = 5 Then
Range("C1").Interior.Color = RGB(46, 204, 113)
End If
If col2 = 6 Then
Range("C1").Interior.Color = RGB(46, 134, 193)
End If
If col2 = 7 Then
Range("C1").Interior.Color = RGB(155, 89, 182)
End If
If col2 = 8 Then
Range("C1").Interior.Color = RGB(191, 201, 202)
End If
If col2 = 9 Then
Range("C1").Interior.Color = RGB(248, 249, 249)
End If
Range("A1") = col1 * 10 + col2
End Sub
Actividad 15
usando la función "If"
Sub si_if()
'if simple
If Range("A1") = 1 Then
Range("A1").Interior.Color = vbYellow
End If
If Range("A1") = 2 Then Range("A1").Interior.Color = vbYellow
If Range("A1") = 3 Then
Range("A1").Interior.Color = vbRed
Else
Range("A1").Interior.Color = vbBlue
End If
'if anidado
'entre 10 y 20 es rosa, sino es azul claro
If Range("A2") > 10 And Range("a1") < 20 Then
Range("A2").Interior.Color = RGB(142, 36, 170)
ElseIf Range("A2") >= 21 Then
Range("A2").Interior.Color = RGB(3, 155, 229)
Else
Range("A2").Interior.Color = RGB(46, 204, 113)
End If
End Sub
Actividad 16
calculo de resistencia dos
Option Explicit
Sub resistenciados()
Dim num As Integer
Dim ban As Integer
Dim cuenta As Integer
Dim tot As Integer
cuenta = 1
ban = Application.InputBox(prompt:="1.dos bandas sin tolerancia,2.dos bandas con tolerancia,3.tres bandas sin tolerancia,4.tres bandas con tolerancia")
If ban = 1 Then tot = 2
If ban = 2 Then tot = 3
If ban = 3 Then tot = 3
If ban = 4 Then tot = 4
num = Application.InputBox("0 negro,1 cafe, 2 rojo,3 naranja,4 amarillo,5 verde, 6 azul,7 violeta,8 gris,9 blanco", Type:=1)
Do While cuenta <= tot
If num >= 0 And num <= 9 Then
Cells(cuenta, 1) = num
Else
MsgBox ("Numero no valido"), vbExclamation, "Error de captura"
End If
cuenta = cuenta + 1
Loop
End Sub
Contador de números "del uno al diez"
Sub for_1()
' For contador=inicio to final incremente/decremente
'codigo
'exit for
'Next
Dim c As Integer
For c = 1 To 10
Cells(c, 1) = c
Next
End Sub
contador con numeros de 2 en 2
Sub for_2()
Dim con As Integer
Dim a As Integer
a = 1
For con = 5 To 20 Step 2
Cells(a, 2) = con
a = a + 1
Next
End Sub
contador descendiente "del diez al uno"
Sub for_3()
Dim c As Integer
Dim i As Integer
i = 1
For c = 10 To 1 Step -1
Cells(i, 3) = c
i = i + 1
Next
End Sub
Actividad 20
Contador del Uno al Cien y un numero a llegar en especifico
Sub for_4()
Dim c As Integer
For c = 10 To 100
If c = 49 Then
MsgBox "llegaste a " & c
Exit For
End If
Next
End Sub
Resistencia versión 3
'Hoja 6
Sub resistenciver3()
Dim op As Integer
Do
op = Application.InputBox("Dame un numero:")
Loop While op <> 5
End Sub
Intento 4 con cuadro
Sub intento4()
Dim op2 As Integer
Do
op2 = Application.InputBox("numero entre 10 y 20")
Loop While op2 < 10 And op2 > 20
End Sub
valor de resistencias con o sin tolerancia
Sub resyacasi()
Dim ban As Integer 'cuantas bandas
Dim col As Integer 'color
Dim tol As Integer 'para saber si hay tolerancia
Dim vban1 As Integer 'extraer el color1 de la banda
Dim vban2 As Integer 'estraer el color 2 de la banda
Dim vtol As Integer 'asignar el % de tolerancia
Dim vban3 As Integer 'extraer el color 3 de la banda
' do hacer
' loop while circulo mientras
'case caso
'select seleccionar
'end fin
'application.inputbox caja de entrada de aplicacion (ventana)
'msgbox caja de mensaje
' if si then (si si) entonces else(si no) ademas
Do
ban = Application.InputBox("2. para dos bandas, 3. Para tres bandas")
Loop While ban < 2 Or ban > 3
Select Case ban
Case 2
Do
col = Application.InputBox("0 negro, 1 cafe, 2 rojo, 3 naranja, 4 amarillo, 5 verde,6 azul, 7 violeta, 8 gris, 9 blanco")
Loop While col < 0 Or col > 9
Cells(1, 1) = col
'otro color
Do
col = Application.InputBox("0 negro, 1 cafe, 2 rojo, 3 naranja, 4 amarillo, 5 verde,6 azul, 7 violeta, 8 gris, 9 blanco")
Loop While col < 0 Or col > 9
Cells(2, 1) = col
'extraer valores
vban1 = Cells(1, 1).Value
vban2 = Cells(2, 1).Value
Cells(3, 1) = (vban1 * 10) + vban2
Do
tol = Application.InputBox("1.- SI Tolerancia, 2.-No tolerancia")
Loop While tol <> 1 And tol <> 2
If tol = 1 Then
' MsgBox "Con tolerancia"
Do
vtol = Application.InputBox("1.- 5% 2.- 10% 3.- 15%")
Loop While vtol < 1 Or vtol > 10
If vtol = 1 Then
Cells(3, 1) = "Valor= " & Cells(3, 1).Value & "5%"
End If
If vtol = 2 Then
Cells(3, 1) = "Valor= " & Cells(3, 1).Value & "10%"
End If
If vtol = 2 Then
Cells(3, 1) = "Valor= " & Cells(3, 1).Value & "15%"
End If
Else
MsgBox "Sin tolerancia"
End If
Case 3
Do
col = Application.InputBox("0 negro, 1 cafe, 2 rojo, 3 naranja, 4 amarillo, 5 verde,6 azul, 7 violeta, 8 gris, 9 blanco")
Loop While col < 0 Or col > 9
Cells(1, 1) = col
'segundo color
Do
col = Application.InputBox("0 negro, 1 cafe, 2 rojo, 3 naranja, 4 amarillo, 5 verde,6 azul, 7 violeta, 8 gris, 9 blanco")
Loop While col < 0 Or col > 9
Cells(2, 1) = col
'tercer color
Do
col = Application.InputBox("0 negro, 1 cafe, 2 rojo, 3 naranja, 4 amarillo, 5 verde,6 azul, 7 violeta, 8 gris, 9 blanco")
Loop While col < 0 Or col > 9
Cells(3, 1) = col
'extraer valores
vban1 = Cells(1, 1).Value
vban2 = Cells(2, 1).Value
vban3 = Cells(3, 1).Value
Cells(4, 1) = ((vban1 * 10) + vban2) * vban3
'preguntar tolerancia
tol = 0
Do
tol = Application.InputBox("1.- SI Tolerancia, 2.-No tolerancia")
Loop While tol <> 1 And tol <> 2
If tol = 1 Then
Do
vtol = Application.InputBox("1.- 5% 2.- 10% 3.- 15%")
Loop While vtol < 1 Or vtol > 10
If vtol = 1 Then
Cells(4, 1) = "Valor= " & Cells(4, 1).Value & " 5%"
End If
If vtol = 2 Then
Cells(4, 1) = "Valor= " & Cells(4, 1).Value & " 10%"
End If
If vtol = 3 Then
Cells(4, 1) = "Valor= " & Cells(4, 1).Value & " 15%"
End If
Else
MsgBox "Sin tolerancia"
End If
End Select
End Sub
Actividad 24
valor de la resistencia usando lista desplegable y boton
Nota: para realizar el calculo se deberán colocar listas desplegables dependiendo si la resistencia es de 2 o 3 barras.
Para el boton:
Private Sub CommandButton1_Click()
Dim c1, c2, c3 As String
Dim res As Integer
c1 = Cells(1, 2).Value
c2 = Cells(2, 2).Value
c3 = Cells(3, 2).Value
res = (CInt(Left(c1, 1)) * 10 + CInt(Left(c2, 1))) * CInt(Left(c3, 1))
Cells(4, 2) = res
End Sub
Pedir un nombre en minúsculas
Sub minusculas()
Dim nombre(25) As String
Dim x As Integer
For x = 0 To 4
nombre(x) = Application.InputBox("Dame un nombre")
MsgBox (nombre(x))
'Cells(x + 1, 2) = nombre(x)
Next
For x = 0 To 4
Cells(x + 1, 2) = LCase(nombre(x))
Next
End Sub
Actividad 26
Corrector de palabras "en este caso nombre de colores".
Sub corrector()
Dim col As String
Dim tmp As String
tmp = Cells(1, 2).Value
tmp = Left(tmp, 1)
' negro rojo amarillo verde blanco
If (tmp = "n" Or tmp = "N") Then
Cells(1, 2) = "negro"
End If
End Sub






Comentarios
Publicar un comentario