Informática para Engenharia das Ciências Agrárias

Resolução de alguns exercícios: VBA

Ano Lectivo de 1999/2000

1.

Function ÉPar(arg As Integer) As Boolean

   ÉPar = arg Mod 2 = 0

End Function


2.

Function OuExclusivo(b1 As Boolean, b2 As Boolean) As Boolean

   OuExclusivo = Not (b1 = b2)

End Function


3.

Function MeuFactorial(arg As Integer) As Double

   MeuFactorial = arg

   While arg > 1

      arg = arg - 1

      MeuFactorial = MeuFactorial * arg

   Wend

End Function


4.

Function MeuFibonacci(arg As Integer) As Long

   Dim um_antes As Long

   Dim dois_antes As Long

   Dim n As Integer

   If arg = 0 Then MeuFibonacci = 0: Exit Function

   If arg = 1 Then MeuFibonacci = 1: Exit Function

   dois_antes = 0

   um_antes = 1

   For n = 2 To arg

      MeuFibonacci = um_antes + dois_antes

      dois_antes = um_antes

      um_antes = MeuFibonacci

   Next n

End Function


5.

Function Idade(dnasc As Date) As Integer

   Dim hoje As Date

   hoje = Date

   Idade = Year(hoje) - Year(dnasc)

   If Month(dnasc) > Month(hoje) Then

      Idade = Idade - 1

   ElseIf Month(dnasc) = Month(hoje) And Day(dnasc) > Day(hoje) Then

      Idade = Idade - 1

   End If

End Function


6.6.

Function CelMin(rng As Range) As Integer

   Dim celula As Range

   CelMin = rng.Cells(1, 1)

   For Each celula In rng

      If celula < CelMin Then CelMin = celula

   Next

End Function

Function CelSoma(rng As Range) As Integer

   Dim celula As Range

   CelSoma = 0

   For Each celula In rng

      CelSoma = CelSoma + celula

   Next

End Function

Function CelMed(rng As Range) As Single

   Dim celula As Range

   Dim soma As Integer

   Dim total As Integer

   soma = 0

   total = 0

   For Each celula In rng

      soma = soma + celula

      total = total + 1

   Next

   CelMed = soma / total

End Function


7.3.

Private Sub CommandButtonCalc_Click()

   FormCalc.Show

End Sub


7.7.

Private Sub Numero_Click(num As Integer)

   If inicio Then

      inicio = False

      memoria = Visor

      Visor = num

   Else

      Visor = Visor * 10 + num

   End If

End Sub


7.8.

Private Sub Sinal_Click(s As String)

   If Not inicio Then

      inicio = True

      Select Case sinal

         Case "+"

            Visor = memoria + Visor

         Case "-"

            Visor = memoria - Visor

         Case "*"

            Visor = memoria * Visor

         Case "/"

            Visor = memoria / Visor

      End Select

   End If

   sinal = s

End Sub


Última Actualização: 15 de Novembro de 1999