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) ica-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 VBVBA CompiladoInterpretado 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 typeRange Byte0 to 255 BooleanTrue 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) E38 to E-45 for negative values; E-45 to E38 for positive values Double (double-precision floating-point) E308 to E-324 for negative values; E-324 to E308 for positive values Currency (scaled integer) -922,337,203,685, to 922,337,203,685,
Tipos de variáveis II Decimal+/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/ with 28 places to the right of the decimal; DateJanuary 1, 100 to December 31, 9999 ObjectAny 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 = 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 Modulo SubRotina Private Nome as Tipo Public Nome as Tipo Dim Nome as Tipo
Sub-procedures & functions Sub Rotinas Sub teste (File As String) Open (File) ReadFirstLine (File)... End Sub Call teste (A) Funções Function 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 = Dim Teste(3,3) As Boolean Teste(0,1) = True ; Teste (2,0) = True True False True
Exemplo VI Sheet1
Estruturas de loop I Do While... Loop Do 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...Next For 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 ou...).