Você está na página 1de 8

Resumo VB (com Excel Avanado)

Autor: Everton M. Messias

Teste Inicial "Ol Mundo"


Private Sub CommandButton1_Click()
End
End Sub
1o Programa: "Amor" OBS: comentario 'OBJETO.PROPRIEDADE = VALOR'
Private Sub opt_namorada_Click() 'OptionButton'
pic_dd(0).Visible = True 'PictureBox'
pic_familia(1).Visible = False
pic_filha(2).Visible = False
lbl1.Caption = "Amo por que minha namorada " 'Label'
lbl2.Caption = Empty
lbl3.Caption = Empty
End Sub
Private Sub opt_familia_Click()
pic_dd(0).Visible = False
pic_familia(1).Visible = True
pic_filha(2).Visible = False
lbl1.Caption = Empty
lbl2.Caption = "Amo por que minha famlia "
lbl3.Caption = Empty
End Sub
Private Sub opt_filha_Click()
pic_dd(0).Visible = False
pic_familia(1).Visible = False
pic_filha(2).Visible = True
lbl1.Caption = Empty
lbl2.Caption = Empty
lbl3.Caption = "Amo por que minha filha "
End Sub
Private Sub cmdExit_Click() 'CommandButton'
End
End Sub
2o Programa: Centralizador de formulrio genrico
Private Sub Form_Load()
Form1.Top = (Screen.Height - Height) / 2
Form1.Left = (Screen.Width - Width) / 2
End Sub
Inicializao de Variveis
Public VARIAVEL As Integer
Dim/Private VARIAVEL As Double
Public Const NOME As String = valor

String 1Bytes
Integer 2Bytes
Long 4Bytes
Double - 8Bytes

Funes de Converso
Cint(string) Inteiro
Val(string) - Long (mais usada)
Cdbl(string) Double
Cdate(string) Data

Ex: salario = 1200


Ex: pi = 3.1416 (ponto no virgula)
Ex 6.02E23
Ex: data = #25/05/1976#

3o Programa Multiplicador
Private Sub Command1_Click()
v1 = Val(Text1(0).Text) 'variavel = conversor(objeto.propriedade)'
v2 = Val(Text1(1).Text)
Label2.Caption = v1 * v2 'objeto.propr recebe operao'
End Sub
Operadores
= atribuio , + , - , * , / , ^ , mod (resto da diviso)
and , or , not
< , <= , > , >= , = , <> (diferente)
Condies
If <condio> Then
<comandos>
ElseIf <condio> Then
<comandos>
Else
<comandos>
End If
Teste If
Private Sub CommandButton1_Click()
v = CInt(TextBox1.Text)
r = v Mod 2
If r = 0 Then
Label2.Caption = "O nmero par"
Else
Label2.Caption = "O nmero mpar"
End If
End Sub
4o Programa Calculadora
Private Sub Command1_Click()
v1 = Val(Text1(0).Text) 'variavel = conversor(objeto.propriedade)'
v2 = Val(Text1(1).Text)
Label2.Caption = v1 + v2 'objeto.propr recebe operao'
End Sub
Private Sub Command2_Click()
v1 = Val(Text1(0).Text)
v2 = Val(Text1(1).Text)
Label2.Caption = v1 - v2
End Sub

Private Sub Command3_Click()


v1 = Val(Text1(0).Text)
v2 = Val(Text1(1).Text)
Label2.Caption = v1 * v2
End Sub
Private Sub Command4_Click()
v1 = Val(Text1(0).Text)
v2 = Val(Text1(1).Text)
If v2 = 0 Then 'condio'
MsgBox "No ha diviso por zero !"
End
Else
Label2.Caption = v1 / v2
End If
End Sub
Private Sub limpar_Click() 'boto apaga tudo'
Text1(0).Text = Empty
Text1(1).Text = Empty
Label2.Caption = Empty
End Sub
Unio de comandos
With <objeto>
<propriedades>
End With
Ex:

With txtTeste
txtTeste.Visible = True
txtTeste.Text = Everton
txtTeste.FontName = Arial
txtTeste.FontSize = 20

.Visible = True
.Text = Everton
.FontName = Arial
.FontSize = 20
Ende With

Teste With
Private Sub CommandButton1_Click()
With Label1
.ForeColor = &H0&
.Caption = "Ol Mundo"
.Font = "Arial"
.ForeColor = &HFF&
End With
End Sub
Repetio ou Laos

Do While <condio> [enqto for verdade]


<comandos>
Loop

Do
<comandos>
Loop While <condio>

Do Until <condio> [enqto for falso]


<comandos>
Loop

Do
<comandos>
Loop Until <condio>

For <VARIAVEL> = <valor_inicial> To <valor_final> [Step]


<comandos>
Next <VARIAVEL>

Select Case <VARIAVEL>


Case 0 <comandos> Case 1 <comandos> ....
End Select

5o Programa Muda Cor do Texto


Public Sub mudacor() 'funo em General = publica'
Label1.ForeColor = QBColor(cor) 'funo muda cor'
With Label3
Select Case cor
Case 0
.Caption = "0 - preto"
Case 1
.Caption = "1 - azul"
Case 2
.Caption = "2 - Verde"
Case 3
.Caption = "3 - Ciano"
Case 4
.Caption = "4 - Vermelho"
Case 5
.Caption = "5 - Magenta"
Case 6
.Caption = "6 - Amarelo"
Case 7
.Caption = "7 - Branco"
Case 8
.Caption = "8 - Cinza"
Case 9
.Caption = "9 - Azul Claro"
Case 10
.Caption = "10 - Verde Claro"
Case 11
.Caption = "11 - Ciano Claro"
Case 12
.Caption = "12 - Vermelho Claro"
Case 13
.Caption = "13 - Magenta Claro"

Case 14
.Caption = "14 - Amarelo Claro"
Case Else
.Caption = "15 - Branco Brilhante"
End Select
End With
End Sub
Private Sub Command1_Click()
cor = cor - 1
If cor < 0 Then
cor = 15
End If
mudacor
End Sub
Private Sub Command2_Click()
cor = cor + 1
If cor > 15 Then
cor = 0
End If
mudacor
End Sub
Private Sub Form_Load()
cor = 0
End Sub
6o Programa Fatorial
Public num As Double, fat As Double, resp As Integer
Private Sub Command1_Click()
num = Val(Text1.Text)
If num < 0 Then
Beep
Label3.Caption = "ERRO"
ElseIf num = 0 Or num = 1 Then
Label3.Caption = "1"
Else
fat = 1
Do
fat = fat * num
num = num - 1
Loop While num > 0
End If
Label3.Caption = fat
* resp = MsgBox("Deseja continuar?", 4, "Continuar?")
If resp = 7 Then
End
End If
End Sub

Caixa de Mensagens
MsgBox <mensagem> , <valor-botes> , <ttulo da caixa>
Valor
0
1
2
3
4
5

Descrio
OK
OK , Cancelar
Abortar, Repetir, Ignorar
Sim, No, Cancelar
Sim, No
Repetir , Cancelar

Retorno das caixas de Mensagem


Dim RESP As Integer
RESP = MsgBox (<mensagem> , <valor-botes> , <ttulo da caixa>)
Valor
1
2
3
4
5
6
7

Descrio
OK
Cancelar
Abortar
Repetir
Ignorar
Sim
No

7o Programa Tipo de Caixa de Mensagens


Private Sub Command1_Click()
MsgBox "Everton Adverte !!!", 0, "Meu alerta"
End Sub
Private Sub Command2_Click()
MsgBox "Everton Adverte !!!", 1, "Meu alerta"
End Sub
Private Sub Command3_Click()
MsgBox "Everton Adverte !!!", 2, "Meu alerta"
End Sub
Private Sub Command4_Click()
MsgBox "Everton Adverte !!!", 3, "Meu alerta"
End Sub
Private Sub Command5_Click()
MsgBox "Everton Adverte !!!", 4, "Meu alerta"
End Sub
Private Sub Command6_Click()
MsgBox "Everton Adverte !!!", 5, "Meu alerta"
End Sub
Private Sub Command7_Click()
End
End Sub

ImputBox
VARIAVEL-STRING = InputBox (<mensagem>,<caixa>,<val. se nd digitado>)
8o Programa InputBox
Public V As String
Sub Macro1()
'
' Macro1 Macro
'
' Atalho do teclado: Ctrl+i
V = InputBox("Digite um Valor", "VALOR")
If V = "" Then Exit Sub
* Worksheets(1).Cells(1, 1).Value = V
End Sub
Clulas (ex)
Worksheets(1).Range("S4").Value = 1
Worksheets(1).Cells(4, 19).Value = 1
Worksheets(1).Range("S4").Formula = "=SUM(A1:A5)"
Worksheets(1).Cells(4, 19).Formula = "=SUM(A1:A5)"
9o Funes
Public a, b, c, x1, x2, delta As Long
Sub eq2o_grau()
'
' eq2o_grau Macro
'
' Atalho do teclado: Ctrl+r
'
Selection.ClearContents
Range("A3").Select
Selection.ClearContents
Range("B3").Select
Selection.ClearContents
Range("C3").Select
Selection.ClearContents
Range("D3").Select
Selection.ClearContents
Range("E3").Select
Selection.ClearContents
a = InputBox("Digite o valor do termo A")
If a = "" Then End
a = Val(a)
Worksheets(1).Cells(3, 1).Value = a

b = InputBox("Digite o valor do termo B")


If b = "" Then End
b = Val(b)
Worksheets(1).Cells(3, 2).Value = b
c = InputBox("Digite o valor do termo C")
If c = "" Then End
c = Val(c)
Worksheets(1).Cells(3, 3).Value = c
Call equacao(a, b, c)
End Sub
Public Function equacao(ByVal a As Long, ByVal b As Long, ByVal c As Long)
delta = (b * b) - (4 * a * c)
x1 = (((-1) * b) + (Sqr(delta))) / (2 * a)
x2 = (((-1) * b) - (Sqr(delta))) / (2 * a)
Worksheets(1).Cells(3, 4).Value = x1
Worksheets(1).Cells(3, 5).Value = x2
MsgBox "X1 = " & x1 & " e X2 = " & x2
End Function
---------------------------------------------------------------------------------------Public c, f As Long
Public Sub temperatura()
c = InputBox("Digite o valor em Graus Celsius", "Conversor de Temperatura")
If c = "" Then End
c = Val(c)
Call converttemp(c, f)
MsgBox "Resp.: " & f & " Graus Fahrenheit"
End Sub
Public Function converttemp(ByVal c As Long, ByRef f As Long)
f = 32 + ((9 * c) / 5)
End Function
---------------------------------------------------------------------------------------Public x, y, a As Double
Public Sub arearetangulo()
x = InputBox("Digite o valor de X", "Area do Retangulo")
x = CDbl(x)
y = InputBox("Digite o valor de Y", "Area do Retangulo")
y = CDbl(y)
a = area(x, y)
MsgBox "A Area : " & a
End Sub
Function area(ByVal x As Double, ByVal y As Double) As Double
area = x * y
Exit Function
End Function

Você também pode gostar