Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
69 views

Vba Excel

This document discusses VBA macros in Excel including recording macros, inserting buttons to run macros, using modules and comments, working with objects like sheets and cells, using variables and data types, conditional statements, loops, formatting, and more. It provides examples of using VBA to automate tasks like sorting, formatting cells, finding and selecting ranges, and creating your own functions. The document also outlines some VBA development tools like the Immediate window, Object Browser, and adding debugging commands to a custom toolbar.

Uploaded by

sergio
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Vba Excel

This document discusses VBA macros in Excel including recording macros, inserting buttons to run macros, using modules and comments, working with objects like sheets and cells, using variables and data types, conditional statements, loops, formatting, and more. It provides examples of using VBA to automate tasks like sorting, formatting cells, finding and selecting ranges, and creating your own functions. The document also outlines some VBA development tools like the Immediate window, Object Browser, and adding debugging commands to a custom toolbar.

Uploaded by

sergio
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

VBA Excel

Workbook macro options


Link macro to buttons
Developer Tab > Insert
VBA Scripting
Modules
Grammar
Comments
Starts with '
Option Explicit
Force to use Dim As to declare variables
Execution
Call Statement for execution
Call method_name
Objects
Sheets
Sheets(1)
Sheets("Sheet1").Select
ActiveSheet
ActiveSheet.Name = "Name"
Paste content
ActiveSheet.Paste
ActiveSheet.PasteSpecial
ActiveCell and Selection
Return cell direction method
Selection.Address
ActiveCell.Address
WorkSheets
Count how worksheets exist
Worksheets.Count
Sending to print or preview
Worksheets.PrintOut
Worksheets.PrintPreview
Worksheets(1).PrintOut
Worksheets(1).PrintPreview
Worksheets("sheet1").PrintOut
Worksheets("sheet1").PrintPreview
Tables
Charts
Cells
Cells.Select
Columns
Rows
Rows("1:1").Insert
Rows("1:1").Font.Bold = True
- Color Options in VBA
Examples
Range("A1").Font.Color = vbRed
Range("A1").Font.Color = RGB(1,1,1)
Range("A1").Font.Color = 65535
Range("A1").Font.ThemeColor = xlThemeColorLight1
Theme Colors
xlThemeColorLight1
xlThemeColorLight2
xlThemeColorDark1
xlThemeColorDark2
xlThemeColorAccent1
xlThemeColorAccent2
xlThemeColorAccent3
xlThemeColorAccent4
xlThemeColorAccent5
xlThemeColorAccent6
VB Colors
vbBlack
vbWhite
vbRed
vbLine
vbYellow
vbBlue
vbMagenta
vbCyan
RGB Colors
Blue - (0,0,255)
Yellow - (255,255,0)
Cyan - (0,255,255)
Magenta - (255,0,255)
Hexadecimal (HEX)
Blue - 16711680
Yellow - 65535
Cyan - 16776960
Magenta - 16711935
Range and Selection
Range
Different ways to express Range
Range("C3")
Range("B2").Range("B2")
Cells(3,3)
[C3]
Range("A1").Offset(2,2)
myRange = "C3"
Range(myRange)
Range("A1").Select
Range("A1").Value = "text"
Range("A1").Clear
Selection
Works as dynamic pointer of any previous selected cell
Selection.Font.Name = "Font Name"
Selection.Value = "text"
Selection.Clear
Selection.CurrentRegion.Select
Travel from current location and select
Selection.Offset(y,x).Select
Copy cell content previously selected
Selection.Copy
Move from selection
Selection.End(xlDown)
xlUp
ToRight
ToLeft
Selection.Columns.AutoFit
Selection.Style = "Currency"
Data Storage
Variables
Declare
Dim (declare in memory)
Dim variable_name As Type
Multiple variables declaration
Dim var1, var2, var3 As DataType
Give it a value
variable_name = value
Use the variable
Range("A5").Value = variable_name
Data Types
Misc
Date (8 bytes)
1900 - 9999
Object (4 -32 bytes)
Any
Range (4 -32 bytes)
Any
Text
Variant (16-32bytes)
All
String (2B characters)
2B characters
Char (2bytes)
0 - 65535
Number
Decimal (14bytes)
28 decimal placeholders
Double (8bytes)
Gazillians
Integer (2bytes)
-2M - 2M
Boolean/Byte
Boolean (2bytes)
True/False
Byte (1 byte)
0 - 255
Inputs and Outputs
Message Boxes
inputreceived = MsgBox "Message", vbOption, "Message Box Title"
vbOption
vbYesNo
?vbYes: 6
?vbNo: 7
vbMsgBoxHelpButton
vbOkCancel
vbOkOnly
vbInformation
vbYesNoCancel
vbRetryCancel
The vbCrLf Constant
Continue a string into the next line
"string" & vbCrLf & "string 2"
Underscore to continue the line of msgbox
MsgBox "Large Prompt", _
vbYesNo, "title"
Input Boxes
inputVariable = InputBox "Question","Title"
Conditional Statement
If Statement
If compare_statement Then
statement if yes
ElseIf second_compare_statement Then
statement if yes
Else:
statement if no
End if
Select Case Statement
Select Case variable
Case variable_value
statement to do
Case variable_value
statement to do
Case Else
statement to do
End Select
Loops
For
For Loop
Dim x As Integer
For x = 1 To 10
Cells(x,1).Value = 100
Next x
For Each Loop
Dim x As WorkSheet
For Each x In Worksheets
Msgbox "Found Worksheet: " & x.Name
Next x
Exit For
Use it to end for loop before ends normally
Do
Do While Loop
Dime x as Integer
x = 1
Do While x < 10
Cells(x,1).Value = 100
x = x + 1
Loop
Do Until Loop
Dim x as Integer
x = 1
Do Until IsEmpty(Cells(x,1))
Cells(x,1).Value = "new value"
x = x + 1
Loop
Do Loop Until
Dim x as Integer
x = 1
Do
Cells(x,1).Value = "new value"
x = x + 1
Loop Until IsEmpty(Cells(x,1))
Find Tool (With Macro Recorder)
Cells.Find(args)
Cells.FindNext(After:=ActiveCell).Activate
- What we do?
Create your own functions
Formatting
Change colors, typography, etc
Automate Sorting
Lock and Unlock Cells
Using Search cells to select specific range with values or type of values
OOP as statement work
- Requirements
Enable Developer Tab
File > Options > Ribbon > Developer Tab
Tools
- Record Macro
Recreate excel user actions converted into code
Once started our computer saves each step on worksheet even clicks
By default record macro works with absolute cell referencing
If use relative references is selected any code will be applied to other active
cells when execute
Properties using Record macro:
Macro name: assigne a name
Shortcut key: Assigne a execution shortcut even cand override any excel shortcut
Store macro in:
Can be saved in
Current workbook
New workbook
Personal macro workbook (ready locally for all files)
Description: Describe what the macro does
- Immediate Window
View Tab > Immediate Window
Create questions about the workbook to be answered
?Worksheets.Count
?Range("B2")
Execute code
Selection.HorizontalAligment =
xlCenterAcrossSelection
xlLeft
- Object Browser
View Tab > Object Browser
List of all Ojects with properties and options
- VBA Custom bar
Right clic on bar > Custom
Select from Commands > Debug
Select "Step into" and Drag into bar
Select "Compile project" and Drag into bar
Select "Toogle Breakpoint" and Drag into bar

You might also like