Wa0033

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Visual Basic Editor

Visual Basic Editor


Project Explorer

• All open Workbooks – within each workbook (which is also called a project), you can
have the following objects:
• Worksheet object for each worksheet in the workbook
• ThisWorkbook object which represents the workbook itself
• Chartsheet object for each chart sheet (these are not as common as worksheets)
• Modules – This is where the code that is generated with a macro recorder goes.
You can also write or copy-paste VBA code here.

Properties Window

Properties window allows us to change the properties of a


selected object. For example, if I want to make a worksheet
hidden (or very hidden), I can do that by changing the Visible
Property of the selected worksheet object.
The modules are organized into 5 different types.
1. Standard modules – most of your code will go into this type of module. When you record a
macro, it gets put into a standard module.    When you write a general procedure to be used
throughout your workbook, it also normally goes into a standard module.
2. Workbook modules – this module holds the code the is unique to that individual workbook.
Most of the code in these type of modules are known as EVENTS.   An event can occur when a
workbook is opened or closed for example. The module can also contain code that is written
by yourself and used by the events.
3. Sheet modules – this module holds the code that is unique to that individual sheet.  They can
occur when a sheet is clicked on for example (the Click Event), or when you change data in a
cell.  This module can also hold code that is written by yourself and called by the Events.
4. Form modules – this is the module behind a custom form that you may create. For example
you may create a form to hold details for an invoice, with an OK button, the code behind the
button (the Click Event) contains the code that will run  when the button is clicked.
5. Class modules – this module is used to create objects at run time. Class module are used by
Advanced VBA programmers and will be covered at a later stage.
MsgBox "Entered value is " & Range("A1").
Value

MsgBox "Line 1" & vbNewLine & "Line 2"

MsgBox Worksheets.Count

Workbooks.Add

Workbooks("close-open.xlsm").Close
Workbook Objects

MsgBox "Entered value is " & Range("A1").Value

MsgBox "Entered value is " & Range("A1").


Value

MsgBox "Line 1" & vbNewLine & "Line 2"

MsgBox Worksheets.Count

Workbooks.Add

Workbooks("close-open.xlsm").Close

Workbooks.Open ("sales.xlsx")
Range Objects
The Range object, which is the representation of a cell (or cells) on your worksheet, is the most important object of Excel
VBA.

Range("B3").Value = 2
Range("A1:A4").Value = 5
Range("A1:A2,B3:C4").Value = 10
Range("Prices").Value = 15
Cells(3, 2).Value = 2
Range(Cells(1, 1), Cells(4, 1)).Value = 5
Dim example As Range
Dim example As Range Set example = Range("A1:C4")
Set example = Range("A1:C4")
example.Rows(3).Select
example.Value = 8
Dim example As Range
Worksheets(3).Activate Set example = Range("A1:C4")
Worksheets(3).Range("B7").Select
example.Columns(2).Select
Range("A1:A2").Select
Selection.Copy

Range("C3").Select
ActiveSheet.Paste

Range("C3:C4").Value = Range("A1:A2").Value

Range("A1").ClearContents

Range("A1").Value = ""

Dim example As Range


Set example = Range("A1:C4")

MsgBox example.Count
Variables

Dim x As Integer
x=6
Range("A1").Value = x

Dim book As String


book = "bible"
Range("A1").Value = book
Dim x As Integer
x = 5.5
MsgBox "value is " & x
Dim x As Double
x = 5.5
MsgBox "value is " & x

Dim continue As Boolean


continue = True

If continue = True Then MsgBox "Boolean variables are cool"


Dim score As Integer, result As String
score = Range("A1").Value

If score >= 60 Then result = "pass"

Range("B1").Value = result

Dim score As Integer, result As String


score = Range("A1").Value

If score >= 60 Then


result = "pass"
Else
result = "fail"
End If

Range("B1").Value = result
Input Box
Dim example As Variant
example = InputBox("Give me some input")
Range("A1").Value = example

example = InputBox("Give me some input", "Hi", 1)

Dim Length As Double


Dim Width As Double
Length = InputBox("Enter Length ", "Enter a Number")
Width = InputBox("Enter Width", "Enter a Number")
findArea = Length * Width
Range("A1").Value = findArea
Loops-For Next
Dim i As Integer
For i = 1 To 6
Cells(i, 1).Value = 100
Next i

Dim i As Integer
For i = 1 To 6
Cells(i, 1).Value = 100 +i
Next i

Dim i As Integer, j As Integer Dim c As Integer, i As Integer, j As Integer


For i = 1 To 6
For j = 1 To 2 For c = 1 To 3
Cells(i, j).Value = 100 For i = 1 To 6
Next j For j = 1 To 2
Next i Worksheets(c).Cells(i, j).Value = 100
Next j
Next i
Next c
Loops- Do While
Dim i As Integer Dim i As Integer
i=1 i=1

Do While i < 6 Do While Cells(i, 1).Value <> ""


Cells(i, 1).Value = 20 +i Cells(i, 2).Value = Cells(i, 1).Value + 10
i=i+1 i=i+1
Print Cells(i,1).Value Loop
Loop
Dim i As Integer Dim i As Integer Dim j As Integer
i=1
For i = 1 To 6 Step 2 For j = 8 To 3 Step -1
Do Until i > 6 Cells(6, j).Value = 50
Cells(i, 1).Value = 20 + i Cells(i, 1).Value = 100 Next j
i=i+1
Loop Next i
Select …Case
• In VBA, the Select Case Statement is an alternative to the If-Then
statement, allowing you to test if conditions are met, running specific code
for each condition. The Select Statement is preferable to the If Statement
when there are multiple conditions to process.
Ranges

Dim Score As Integer Dim Score As Integer


Dim LetterGrade As String Dim LetterGrade As String

Score = InputBox("Enter Student Score") Score = InputBox("Enter Student Score")

Select Case Score Select Case Score


Case 90 To 100 Case 90 To 100: LetterGrade = "A"
LetterGrade = "A" Case 80 To 90: LetterGrade = "B"
Case 80 To 90 Case 70 To 80: LetterGrade = "C"
LetterGrade = "B" Case 60 To 70: LetterGrade = "D"
Case 70 To 80 Case Else: LetterGrade = "F"
LetterGrade = "C" End Select
Case 60 To 70
LetterGrade = "D" MsgBox "The Student's Grade is: " & LetterGrade
Case Else
LetterGrade = "F"
End Select

MsgBox "The Student's Grade is: " & LetterGrade


Select Case Is
Exact Match – Text
Select Case Range("a1").Value
Dim Score As Integer Case "Beets"
Dim LetterGrade As String MsgBox "Vegetable"
Case "Apple", "Orange"
Score = InputBox("Enter Student Score") MsgBox "Fruit"
End Select
Select Case Score
Case Is >= 90 To turn case-sensitivity off add Option Compare
LetterGrade = "A" Text to the top of your module:
Case Is >= 80
LetterGrade = "B" Option Compare Text
Case Is >= 70
LetterGrade = "C" Select Case Range("a1").Value
Case Is >= 60 Case "Beets"
LetterGrade = "D" MsgBox "Vegetable"
Case Else Case "Apple", "Banana", "Orange"
LetterGrade = "F" MsgBox "Fruit"
End Select End Select

MsgBox "The Student's Grade is: " & LetterGrade


Sub CheckOddEven() 1.Find the square of the number.
Dim n As Integer
n = InputBox("Enter a number")

Select Case n Mod 2


Case 0
MsgBox "The number is even."
Case 1
MsgBox "The number is odd."
End Select

End Sub
Offset
Using OFFSET with the range object, you can navigate from one cell to another in the worksheet and you
can also select a cell or a range. It also gives you access to the properties and methods that you have with
the range object to use, but you need to specify the arguments in the OFFSET to use it.

OFFSET from a cell A1


' Refer to cell itself
' Move 0 rows and 0 columns
Range("A1").Offset(0, 0) = "A1"

' Move 1 rows and 0 columns


Range("A1").Offset(1, 0) = "A2"

' Move 0 rows and 1 columns


Range.Offset(number_of_rows, number_of_columns) Range("A1").Offset(0, 1) = "B1"

Range("B3").Offset(5).Select ' Move 1 rows and 1 columns


Range("A1").Offset(1, 1) = "B2"
Range("B1").Offset(5, 3).Select
' Move 10 rows and 5 columns
Range("A1").Offset(10, 5) = "F11"
Dim i As Integer, j As Integer

For i = 1 To 5 Step 2
For j = 1 To 5 Step 2
Cells(i, j).Interior.ColorIndex = 15
Next j
Next i
Create Pattern
Cells(i, j).Offset(1, 1).Interior.ColorIndex = 15

Sort the following numbers


Dim i As Integer, j As Integer, temp As Integer, rng As Range
Set rng = Range("A1").CurrentRegion
For i = 1 To rng.Count
For j = i + 1 To rng.Count

If rng.Cells(j) > rng.Cells(i) Then

Else

temp = rng.Cells(i)
rng.Cells(i) = rng.Cells(j)
rng.Cells(j) = temp

End If

Next j

Next i
Resize
Resize is a property available in VBA to change or resize the range of cells from the active cell as needed.
For example, assume you are in cell B5. If you want to select 3 rows and two columns from this cell, we can
change the range size by using the RESIZE property of VBA.
If one of the arguments from row size or column size is not provided with this function selects the entire row or the
column from the range.

Range("A1").Resize (RowSize, ColumnSize)

Range.Resize(number_of_rows, number_of_columns)

Range(“B2:C4").Resize(3, 2).Select
Range("A1:C4").Resize(, 1).Select
Range("A1:C4").Resize(3, 2).Select
Worksheets("Sheet1").Activate
numRows = Selection.Rows.Count
numColumns = Selection.Columns.Count
Selection.Resize(numRows + 2, numColumns + 2).Select
Range("A2:B3").Select
Range("A2:B3").Resize(1, 2).Offset(-1, 1).Select
Range("A2:B3").Offset(-1, 1).Resize(1, 2).Select

The Resize property will always use the lower-line and lower-column cell as the starting point. The size
of the Range reference is irrelevant:
Range("A1:J20").Resize(2, 2).Select
Union and Intersect
• The Union method in Excel VBA returns a Range object that
represents the union of two or more ranges
Union(Range("B2:C7"), Range("C6:F8")).Select

• The Intersect method in Excel VBA returns a Range object that


represents the intersection of two or more ranges
Intersect(Range("B2:C7"), Range("C6:F8")).Select
CurrentRegion
• Range("A1").CurrentRegion.Select

• Range("B3").CurrentRegion.Select
Entire Rows and Columns
• Cells.Select
Worksheets(2).Activate
Worksheets(2).Cells.Select

Columns(2).Select

Rows(7).Select

Rows("5:7").Select

Columns("B:E").Select

ActiveCell.EntireRow.Select
ActiveCell.EntireColumn.Cells(1).Value = 2
ActiveCell.EntireRow.Offset(1, 0).Cells(1).Value = 3

You might also like