📌 What is VBA?VBA = Visual Basic for ApplicationsProgramming language integrated into Excel (and other Office programs)Allows you to automate tasks, create custom functions and interact with spreadsheets in an advanced way🔧 How to Access the VBA EditorPress ALT + F11 (universal shortcut)Or visit:Developer Guide > Visual BasicIf you don't see the "Developer" tab:File > Options > Customize Ribbon > Check "Developer"📝 Basic VBA StructureIn the VBA Editor, you will work with:Módulos: Onde escreve os códigos (Inserir > Módulo)Sub: Executa ações (macros)Function: Retorna valores (funções personalizadas)Sub Example:Sub WelcomeMessage()MsgBox "Hello, VBA world!"End SubThe above terms mean:Sub: Começa uma "receita" (chamamos de macro)' (Apostrofo): É um comentário - o Excel ignora, serve para você anotar MsgBox: Mostra uma mensagem na tela End Sub: Termina a "receita"Function Example:Function Double(number As Double) As DoubleDouble = number * 2End Function🛠 Getting Started: Creating a Simple MacroObjective: To greet you!Code:Sub SayHello()Dim name As Stringname = InputBox("What is your name?")MsgBox "Hello, " & name & "!"End SubHow to run:- Press F5 in the VBA Editor- Or assign to a button in the spreadsheetAssign macro to buttonGo back to Excel and follow:Guia Inserir > Formas > Escolha um retângulo ou botãoDesenhe o botão na planilhaClique com o botão direito no botão criadoSelecione Atribuir Macro...Escolha sua macro na lista (MensagemSimples)Clique em OKPara formatar o botão Clique direito > Editar Texto para mudar o rótulo (ex: "Clique Aqui")Use a guia Formatar para mudar cores e estilo📌 Variables and Data TypesBasic Declaration: Dim Variable Name As TypeMain types:Integer: Números inteirosDouble: Números decimaisString: TextoBoolean: Verdadeiro/FalsoRange: Intervalo de célulasExample:Dim nome As StringDim idade As IntegerDim salario As DoubleDim ativo As BooleanDim celula As Range🔄 Control Structures1. Conditionals (IF)If condition Then'code if trueElse'code if falseEnd IfExample:Sub CheckAge()Dim Age As Integerage = Range("B1").ValueIf age >= 18 ThenMsgBox "Of legal age"ElseMsgBox "Minor"End IfEnd Sub2. Loops (For, Do While)For example:For i = 1 To 10'repeated codeNext iDo While:Do While condition'repeated codeLoopPractical example:Sub ColorEvenLines()Dim i As IntegerFor i = 1 To 10 Step 2Rows(i).Interior.Color = RGB(200, 200, 200)Next iEnd Sub📊 Working with SpreadsheetsReferencing Cells'Ways to reference:Range("A1").Value = 100Cells(1, 1).Value = 100 'Row 1, Column 1Practical Example: Automated AdditionSub SumRange()Dim total As Doubletotal = Application.WorksheetFunction.Sum(Range("A1:A10"))MsgBox "The total is: " & totalEnd SubExercises1. Add Two Numbers. Objective: To create a macro that reads two numbers from cells A1 and B1, calculates their sum, and displays the result in C1. (Use Range("A1").Value to access values.)2. Create a macro that formats cell A1 with: Yellow background color. Bold text. Red font. (Hints => .Interior.Color = RGB(255, 255, 0) (yellow), .Font.Bold = True, .Font.Color = RGB(255, 0, 0) (red)3. Convert to Uppercase. Objective: Create a macro that converts the text in cell A1 to UPPERCASE and displays the result in B1. (Hint: Use the UCase() function.)4. Create a Multiplication Table. Objective: Create a macro that fills in a multiplication table from 1 to 5 in cells A1:E5. (Hint: Use two nested For loops.)5. Clear Cell Contents. Objective: Create a macro that clears the contents of cells A1 through C10. (Hint: Use Range("A1:C10").ClearContents().)
— コメント 0
, 反応 1
最初にコメントする