Trabalho Computacional Mecânica de Fluidos Ambiental Guillaume Riflet, Pedro Pina, Luís Fernandes

Preview:

Citation preview

Trabalho Computacional Mecânica de Fluidos Ambiental

Guillaume Riflet,Pedro Pina,Luís Fernandes

Objectivos

Programming 101 MS-excel macros Visual Basic for Applications (VBA) 101

https://fenix.ist.utl.pt/homepage/ist146730/mecanica-dos-fluidos-ambiental

Interesse para engenheiros do ambiente

Utilização mais avançadas de ferramentas de folhas de cálculo.

Em muitas circunstâncias, o excel (ou derivado) é a melhor ferramenta realizar tarefas.

Qualquer empresa do sector público ou privado usa uma ferramenta de folha de cálculo. Todas permitem utilizar macros. 90% permitem programar em VBA.

Linguagens de programação

Visual Basic Fortran C/C++ C# Matlab Java (pôr linguagem de programação favorita)

Diferenças entre VB e VBA

VB VBA

Compilado Interpretado

IDE próprio (VS2Kx) IDE embebido no Office

Bibliotecas e classes externas importadas

Bibliotecas e classes nativas de Office

Exemplo I

Sheet1

Dica: Alt+F11

Exemplo I: glossário

Source-code, código-fonte Compiler/interpreter,

compilador/interpretador Subroutine, Subrotina String, Sequência de caracteres Propriedade dum objecto Excel macro, Macro de excel

Exemplo II

Sheet1

Exemplo III

Sheet1

Exemplo III: glossário

Argument, Argumento Argument passing, Passagem de argumento Argument type, Tipo de argumento

Exemplo IV

Sheet1

Exemplo IV: glossário

Function, Função Function type, Tipo de função

Exemplo V

Sheet1

Exemplo V: glossário

Variable, Variável Variable type, Tipo de variável

Variáveis

Dim Nome As Tipo

Dim Aluno as String

Dim Idade as Integer

Dim Nota as Single

Declaração Implícita/Explícita

Function SafeSqr(num) TempVal = Abs(num) SafeSqr = Sqr(TempVal)End Function

Function SafeSqr(num) TempVal = Abs(num) SafeSqr = Sqr(TemVal)End Function

Option Explicit

Tipos de variáveis I

Data type Range

Byte 0 to 255

Boolean True or False

Integer -32,768 to 32,767

Long(long integer)

-2,147,483,648 to 2,147,483,647

Single(single-precision floating-point)

-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values

Double(double-precision floating-point)

-1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values

Currency(scaled integer)

-922,337,203,685,477.5808 to 922,337,203,685,477.5807

Tipos de variáveis II

Decimal +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal;

Date January 1, 100 to December 31, 9999

Object Any Object reference

String (variable-length)

0 to approximately 2 billion

String

(fixed-length)

1 to approximately 65,400

Variant(with numbers)

Any numeric value up to the range of a Double

Variant(with characters)

Same range as for variable-length String

User-defined(using Type)

The range of each element is the same as the range of its data type.

Constantes

•Public Const conMaxPlanets As Integer = 9•Const conReleaseDate = #1/1/95#  •Const conPi = 3.14159265358979

Constantes são valores que aparecem várias vezes no programa e que ao contrário das variáveis não alteram o seu valor.

Projecto

ModuloSubRotina

Private Nome as TipoPublic Nome as TipoDim Nome as Tipo

Sub-procedures & functions

Sub RotinasSub teste (File As String)Open (File)ReadFirstLine (File)...End Sub

Call teste (A)

FunçõesFunction Hypotenuse (A As Integer, B As _ Integer) As Double Hypotenuse = Sqr(A ^ 2 + B ^ 2)

End Function  

strX = Hypotenuse(Width, Height)

Arrays

Dim Conta(6) As Integer

i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 Conta(0) = 12 ; Conta(3) = 21; Conta(5) = 3

i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 12 0 0 21 0 3

Dim Teste(3,3) As Boolean

Teste(0,1) = True ; Teste (2,0) = True

True

False

False

False

False

False

False

FalseTrue

Exemplo VI

Sheet1

Estruturas de loop I

Do While ... LoopDo While line < 10

Call ReadLine(line)

line = line+1 Loop For...Next

 For i = 0 To 10 For j = 0 to 10

Matriz(i,j) = cos(x)

Next Next

For Each...NextFor Each File In folder.File()

Call ReadFile (File) Next File

Exemplo VII

Sheet1

Configurações regionais

Atenção no que toca à linguagem nativa do office. SEN (português) ou SIN (inglês).

Atenção às definições de separadores de casas decimais (3.14 ou 3,14) e de formatos de data (13/05/1980 ou 1980-05-13 ou...).