4 - Curso VBA básico P2


Bem vindo ao nosso curso de Excel VBA básico grátis e completo (parte 2).


Com apenas 24 vídeos, mais o nosso e-book grátis, você aprenderá VBA e executará tarefas que as fórmulas não permitem. Qualquer pessoa é capaz de aplicar os conhecimentos aqui exibidos. Confira!

Sugestão: baixe o e-book para um melhor aprendizado, clique aqui p/ download.



Menu da página (vídeos):
10: a) condicional "If"
11: a) variáveis
12: a) variáveis
13: a) ocultar e exibir linha(s)
      b) deletar linha(s)
      c) proteger linha(s)
14: a) ocultar e exibir coluna(s)
      b) deletar coluna(s)
      c) proteger coluna(s)
15: a) células especiais P1
16: a) células especiais P2
17: a) união de "Ranges"
      b) interseção de "Ranges"
      c) End(Direction )
      d) Resize
18: a) tratamento de texto "Asc"
          Chr, Len, LCase, UCase
          Left, Right, Mid, Trim
          Instr e isnumeric.
19: a) formulários P1
20: a) formulários P2
21: a) funções P1
22: a) funções P2
23: a) gráficos


10 - Excel VBA Básico - ( vídeo 10 )

CÓDIGOS DESTE VÍDEO:
Sub Teste_Basico43_Condicional01()
    If Cells(661, 2).Value > 10 Then
        MsgBox "Este valor é maior que 10!"
    End If
End Sub


'----------------------------------------------
Sub Teste_Basico44_Condicional03()     
  If (Cells(699, 2).Value >= 0 And Cells(699, 2).Value < 30) Then
            MsgBox "Aluno reprovado."
        ElseIf (Cells(699, 2).Value >= 30 And Cells(699, 2).Value <= 60) Then
            MsgBox "Aluno situação crítica."
        ElseIf (Cells(699, 2).Value > 60 And Cells(699, 2).Value < 80) Then
            MsgBox "Aluno situação aceitável."
        ElseIf (Cells(699, 2).Value >= 80 And Cells(699, 2).Value < 101) Then
            MsgBox "Aluno excelente."
        Else
            MsgBox "ERRO! Foi impossível avaliar o aluno."
  End If
End Sub


'----------------------------------------------
Sub Teste_Basico45_Condicional04()
    If ActiveCell.Font.Bold = True Then
        MsgBox "A célula ativa está em negrito."
    ElseIf ActiveCell.Font.Italic = True Then
        MsgBox "A célula ativa está em itálico."
    Else
        MsgBox "A célula ativa não está em negrito nem em itálico."
    End If
End Sub



11 - Excel VBA Básico - ( vídeo 11 )



12 - Excel VBA Básico - ( vídeo 12 )

CÓDIGOS DESTE VÍDEO:
Sub Teste_Basico45B_Variavel01()
    Dim Var As Integer
    Var = Range("B808").Value
    MsgBox "O valor da variável é:" & Var
End Sub
 



Exercícios ( parte 3 )

Baixe o e-book para fazer os demais exercícios: download aqui


13 - Excel VBA Básico - ( vídeo 13 )

CÓDIGOS DESTE VÍDEO:
Sub Teste_Basico_Linha01()
    Rows("3").Select
    Selection.EntireRow.Hidden = True
End Sub


'----------------------------------------------
Sub Teste_Basico_Linha02()
    Dim shtBasico As Worksheet
    Set shtBasico = Sheets("VBA Básico")
    shtBasico.Rows("3:5").Select
    Selection.EntireRow.Hidden = True
End Sub


'----------------------------------------------
Sub Teste_Basico_Linha03()
    Rows("3").Select
    Selection.EntireRow.Hidden = False
End Sub


'----------------------------------------------   
Sub Teste_Basico_Linha04()
    Dim shtBasico As Worksheet
    Set shtBasico = Sheets("VBA Básico")
    shtBasico.Rows("3:5").Select
    Selection.EntireRow.Hidden = False
End Sub


'----------------------------------------------
Sub Teste_Basico_Linha05()
    Rows("4").Select
    Selection.Delete Shift:=xlUp
End Sub


'----------------------------------------------   
Sub Teste_Basico_Linha06()
    Dim shtBasico As Worksheet
    Set shtBasico = Sheets("VBA Básico")
    shtBasico.Rows("4").Select
    Selection.Delete Shift:=xlUp
End Sub


'----------------------------------------------
Sub Teste_Basico46_Linha07()
    Dim Lin As Integer
    Dim shtBasico As Worksheets
    Lin = ActiveCell.Row
    MsgBox "A linha da célula ativa é: " & Lin
End Sub


'----------------------------------------------
Sub Teste_Basico_Linha08()
    ActiveCell.EntireRow.Select
    Selection.Delete Shift:=xlUp
End Sub


'----------------------------------------------
Sub Teste_Basico_Linha09()
     If ActiveCell.Row < 11 Then
         MsgBox "Proibido remover linhas menores que 11!"
         Exit Sub
     End If
       
     ActiveCell.EntireRow.Select
     Selection.Delete Shift:=xlUp
End Sub



14 - Excel VBA Básico - ( vídeo 14 )

CÓDIGOS DESTE VÍDEO:
Sub Teste_Basico_Coluna01()
    Columns("B").Select
    Selection.EntireColumn.Hidden = True
End Sub


'----------------------------------------------
Sub Teste_Basico_Coluna02()
    Columns("B").Select
    Selection.EntireColumn.Hidden = False
End Sub


'----------------------------------------------
Sub Teste_Basico_Coluna03()
    Columns("B").Select
    Selection.Delete Shift:=xlToLeft
End Sub 


'----------------------------------------------
Sub Teste_Basico47_Coluna04()
    MsgBox ActiveCell.Column
End Sub


'----------------------------------------------
Sub Teste_Basico48_Coluna05()
    MsgBox "a coluna da célula ativa é: " & ActiveCell.Column & "."
End Sub


'----------------------------------------------
Sub Teste_Basico_Coluna06()
    ActiveCell.EntireColumn.Select
    Selection.Delete Shift:=xlToLeft
End Sub


'----------------------------------------------
Sub Teste_Basico_Coluna07()
    If ActiveCell.Column < 12 Then
         MsgBox "É proibido remover coluna < 12!"
        Exit Sub
    End If

    ActiveCell.EntireColumn.Select
    Selection.Delete Shift:=xlToLeft
End Sub



15 - Excel VBA Básico - ( vídeo 15 )


 
  

16 - Excel VBA Básico - ( vídeo 16 )

CÓDIGOS DESTE VÍDEO:
Sub Teste_Basico_SpecialCells01()
     On Error Resume Next
     With ActiveSheet.[A7:I18]
         .SpecialCells(xlCellTypeConstants).Interior.ColorIndex = 3
         .SpecialCells(xlCellTypeFormulas).Interior.ColorIndex = 5
     End With
End Sub

Segue algumas opções possíveis:
.SpecialCells (xlCellTypeAllFormatConditions)
.SpecialCells (xlCellTypeBlanks)
.SpecialCells (xlCellTypeComments)
.SpecialCells (xlCellTypeAllValidation)
.SpecialCells(xlCellTypeFormulas, xlErrors)



17 - Excel VBA Básico - ( vídeo 17 )

CÓDIGOS DESTE VÍDEO: 
Sub Teste_Basico49_DadosContiguos01()
    Union(Range("E1110:F1110"), Range("G1111:G1112")).Select
End Sub


'----------------------------------------------
Sub Teste_Basico50_DadosContiguos02()
    Union(Range("Horizontal"), Range("Vertical")).Select
End Sub


'----------------------------------------------
Sub Teste_Basico51_DadosContiguos03()
    Intersect(Range("D1140:G1140"), Range("F1139:F1143")).Select
End Sub


'----------------------------------------------
Sub Teste_Basico52_DadosContiguos04()
    Intersect(Range("Horizontal4"), Range("Vertical4")).Select
End Sub


'----------------------------------------------
Sub Teste_Basico53_DadosContiguos05()
    ActiveSheet.Range("E1171").End(xlDown).Select
End Sub


'----------------------------------------------
Sub Teste_Basico54_DadosContiguos05B()
    ActiveSheet.Range("J1171").End(xlDown).Select
End Sub

'----------------------------------------------
Sub Teste_Basico55_DadosContiguos06()
    ActiveSheet.Range("E1171", ActiveSheet.Range("E1171").End(xlDown)).Select
End Sub


'----------------------------------------------
Sub Teste_Basico56_DadosContiguos06B()
    ActiveSheet.Range("J1171", ActiveSheet.Range("J1171").End(xlDown)).Select
End Sub


'----------------------------------------------
Sub Teste_Basico57_DadosContiguos07()
    ActiveSheet.Range("B1202").End(xlUp).Select
End Sub


'----------------------------------------------
Sub Teste_Basico58_DadosContiguos08()
    ActiveSheet.Range("B1202", ActiveSheet.Range("B1202").End(xlUp)).Select
End Sub


'----------------------------------------------
Sub Teste_Basico59_DadosContiguos09()
    ActiveSheet.Range("H1217").End(xlDown).Select
End Sub


'----------------------------------------------
Sub Teste_Basico60_DadosContiguos10()
    ActiveSheet.Range("B1233", ActiveSheet.Range("B1233").End(xlDown).End(xlToRight)).Select
End Sub


'----------------------------------------------
Sub Teste_Basico61_Resize01()
    Cells(1258, 2).Resize(3, 4).Select
End Sub


'----------------------------------------------
Sub Teste_Basico62_Resize02()
    Cells(1258, 2).Resize(3).Select
End Sub


'----------------------------------------------           
Sub Teste_Basico63_Resize03()
    Cells(1258, 2).Resize(, 4).Select
End Sub



18 - Excel VBA Básico - ( vídeo 18A )

CÓDIGOS DESTE VÍDEO:
Sub Teste_Basico64_TratTexto01()
    MsgBox Asc("Abacaxi")
End Sub


'----------------------------------------------   
Sub Teste_Basico65_TratTexto02()
    On Error GoTo Fim
    MsgBox Asc(ActiveCell.Value)
Fim:
End Sub


'----------------------------------------------   
Sub Teste_Basico66_TratTexto03()
    MsgBox Chr(65)
End Sub


'----------------------------------------------   
Sub Teste_Basico67_TratTexto04()
    MsgBox Chr(ActiveCell.Column + 64)
End Sub


'----------------------------------------------   
Sub Teste_Basico68_TratTexto05()
    MsgBox Len("Abacaxi")
End Sub


'----------------------------------------------   
Sub Teste_Basico69_TratTexto06()
    MsgBox Len(ActiveCell.Value)
End Sub


'----------------------------------------------   
Sub Teste_Basico70_TratTexto07()
    MsgBox LCase("Abacaxi")
End Sub


'----------------------------------------------   
Sub Teste_Basico71_TratTexto08()
    ActiveCell.Value = LCase(ActiveCell.Value)
End Sub


'----------------------------------------------   
Sub Teste_Basico72_TratTexto09()
    MsgBox UCase("Abacaxi")
End Sub


'----------------------------------------------       
Sub Teste_Basico73_TratTexto10()
    ActiveCell.Value = UCase(ActiveCell.Value)
End Sub


'----------------------------------------------   
Sub Teste_Basico74_TratTexto11()
    MsgBox Left("Abacaxi", 6)
End Sub


'----------------------------------------------       
Sub Teste_Basico75_TratTexto12()
    MsgBox Left(ActiveCell.Value, 2)
End Sub


'----------------------------------------------   
Sub Teste_Basico76_TratTexto13()
    MsgBox Right("Abacaxi", 6)
End Sub


'----------------------------------------------       
Sub Teste_Basico77_TratTexto14()
    MsgBox Right(ActiveCell.Value, 2)
End Sub


'----------------------------------------------   
Sub Teste_Basico78_TratTexto15()
    MsgBox Mid("Abacaxi", 2, 4)
End Sub


'----------------------------------------------           
Sub Teste_Basico79_TratTexto16()
    MsgBox Mid(ActiveCell.Value, 2, 5)
End Sub


'----------------------------------------------   
Sub Teste_Basico80_TratTexto17()
   Dim Resultado As String
   Resultado = Trim("                    Paralelepípedo               ")
   MsgBox "" & Resultado & ""
End Sub


'----------------------------------------------   
Sub Teste_Basico81_TratTexto18()
   Dim Resultado As String
   Resultado = RTrim("                    Paralelepípedo               ")
   MsgBox "" & Resultado & ""
End Sub


'----------------------------------------------           
Sub Teste_Basico82_TratTexto19()
   Dim Resultado As String
   Resultado = LTrim("                    Paralelepípedo               ")
   MsgBox "" & Resultado & ""
End Sub


'----------------------------------------------   
Sub Teste_Basico83_TratTexto20()
   Dim Resultado As String
   Resultado = InStr("Par@lelepípedo", "@")
   MsgBox "" & Resultado & ""
End Sub


'----------------------------------------------   
Sub Teste_Basico84_TratTexto21()
   Dim Resultado As String
   Resultado = InStr(ActiveCell.Value, "@")
   MsgBox "" & Resultado & ""
End Sub


'----------------------------------------------   
Sub Teste_Basico85_TratTexto22A()
Dim Texto As Variant
Texto = Range("B1532").Value
    If IsNumeric(Texto) Then
       MsgBox "O Texto é numérico"
    Else
       MsgBox "O texto não é numérico"
    End If
End Sub



19 - Excel VBA Básico - ( vídeo 18B )

CÓDIGOS DESTE VÍDEO: 
Sub Teste_Basico86_AbrirForm01()
   Form01.Show
End Sub



20 - Excel VBA Básico - ( vídeo 18C )

CÓDIGOS DESTE VÍDEO: 
Sub Teste_Basico88_AbrirForm03()
   Form03.Show
End Sub



21 - Excel VBA Básico - ( vídeo 18D )

CÓDIGOS DESTE VÍDEO:
Sub Teste_Basico89_Funcao01()
   MsgBox WorksheetFunction.RandBetween(1, 20)
End Sub


'----------------------------------------------       
Sub Teste_Basico90_Funcao02()
     Dim X As Integer
     Dim shtBasico As Worksheet
   
     Set shtBasico = Sheets("VBA Básico")
     X = WorksheetFunction.RandBetween(1, 20)
   
     shtBasico.Range("B1695").Value = X
End Sub


'----------------------------------------------   
Sub Teste_Basico92_Funcao03()
    MsgBox WorksheetFunction.VLookup("a", Sheets("VBA Básico").Range("B1702:C1710"), 2, False)
End Sub


'----------------------------------------------       
Sub Teste_Basico93_Funcao04()
    MsgBox WorksheetFunction.Sum(Range("C1702:C1710"))
End Sub


'----------------------------------------------       
Sub Teste_Basico94_Funcao05()
    MsgBox WorksheetFunction.Average(Range("C1702:C1710"))
End Sub


'----------------------------------------------       
Sub Teste_Basico95_Funcao06()
    MsgBox WorksheetFunction.SumIf(Range("B1702:B1710"), "g", Range("C1702:C1710"))
End Sub


'----------------------------------------------       
Sub Teste_Basico96_Funcao07()
    MsgBox WorksheetFunction.Min(Range("C1702:C1710"))
End Sub


'----------------------------------------------       
Sub Teste_Basico97_Funcao08()
    MsgBox WorksheetFunction.Max(Range("C1702:C1710"))
End Sub


'----------------------------------------------       
Sub Teste_Basico98_Funcao09()
    MsgBox WorksheetFunction.CountIf(Range("B1702:B1710"), "a")
End Sub


'----------------------------------------------   
Sub Teste_Basico99_Funcao10()
    MsgBox WorksheetFunction.SumIfs(Range("D1746:D1754"), Range("B1746:B1754"), "a", Range("C1746:C1754"), "d")
End Sub


'----------------------------------------------   
Sub Teste_Basico100_Funcao11()
       Dim Funcao As WorksheetFunction
       Dim Area As Range
       
       Set Funcao = Application.WorksheetFunction
       Set Area = ActiveSheet.[B1770:B1780]
       
       With Funcao
            Range("E1770").Value = .Max(Area)
            Range("E1772").Value = .Min(Area)
            Range("E1774").Value = .Average(Area)
            Range("E1776").Value = .Median(Area)
       End With
End Sub



22 - Excel VBA Básico - ( vídeo 18E )

CÓDIGOS DESTE VÍDEO: 
Public Function Dobro(ByVal N As Integer) As Long
    Dobro = N * 2
End Function


'---------------------------------------------- 
Public Function MultNum(ByVal N1 As Integer, ByVal N2 As Integer) As Long
    MultNum = N1 * N2
End Function


'---------------------------------------------- 
Function SomaCelulas(Area As Range) As Long
    Dim Cell As Range
   
    For Each Cell In Area
        SomaCelulas = SomaCelulas + Cell.Value
    Next Cell
End Function



23 - Excel VBA Básico - ( vídeo 18F )

CÓDIGOS DESTE VÍDEO: 
Sub Teste_Basico109_Grafico09()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.PlotArea.Interior.ColorIndex = 1
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico110_Grafico10()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.PlotArea.Interior.ColorIndex = 2
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico111_Grafico11()
    Sheets("VBA Básico").Unprotect "thx1138"
    ActiveSheet.ChartObjects("Gráfico_01").Activate
        With ActiveSheet.Shapes("Gráfico_01").Fill
            .ForeColor.ObjectThemeColor = 3
        End With
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico112_Grafico12()
    Sheets("VBA Básico").Unprotect "thx1138"
    ActiveSheet.ChartObjects("Gráfico_01").Activate
        With ActiveSheet.Shapes("Gráfico_01").Fill
            .ForeColor.ObjectThemeColor = 7
        End With
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico113_Grafico13()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.Legend.Position = xlBottom
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico114_Grafico14()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.Legend.Position = xlTop
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico115_Grafico15()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.Legend.Position = xlRight
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico116_Grafico16()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.SeriesCollection(1).Select
    Selection.MarkerStyle = 1
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico117_Grafico17()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.SeriesCollection(1).Select
    Selection.MarkerStyle = 2
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico118_Grafico18()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.SeriesCollection(1).Select
    Selection.MarkerSize = 23
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_Basico119_Grafico19()
    Sheets("VBA Básico").Unprotect "thx1138"
    Sheets("VBA Básico").ChartObjects("Gráfico_01").Select
    ActiveChart.SeriesCollection(1).Select
    Selection.MarkerSize = 15
    Range("E3").Select
    Sheets("VBA Básico").Protect "thx1138", AllowFormattingCells:=True
End Sub


'---------------------------------------------- 
Sub Teste_01()
    Range("B3").Select
    Selection.Copy
    Range("D3").PasteSpecial xlPasteFormats
End Sub



Próxima página












 

Comentários