SlideShare ist ein Scribd-Unternehmen logo
1 von 63
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.1
© NCC Education LimitedV1.0
Uses of Macros in Microsoft Excel
‱ Macros can be used in Excel to automate tasks,
such as:
– Data entry and validation
– Data selection
– Interactive user forms
– Formatting features and functions
– Data output
– Navigation
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.2
© NCC Education LimitedV1.0
Creating a Macro
‱ Plan your macro - you should plan the steps that you want
a macro to perform before you record or write it (remember
the importance of planning was discussed in Topic 2, Slide
18).
‱ A macro that is used often can be assigned to shortcut
keys, buttons, pictures or icons, without having to open the
Macro dialogue box.
‱ A macro can be created by either recording it using the
Macro recorder or writing VBA code in the VB Editor. Each
development method will be used in this Topic and in Topic
9.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.3
© NCC Education LimitedV1.0
Overview of Recording a Macro
‱ The macro recorder is turned on.
‱ A name for the macro is entered, it is assigned to a
short cut key (optional), it is saved and a
description is given to it (optional).
‱ The recorder is started.
‱ Steps for the macro are entered.
‱ The recorder is stopped.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.4
© NCC Education LimitedV1.0
Turning on the Macro Recorder
1. Select the Macro option.
2. The Record Macro dialogue box is displayed and
here you can:
- name your macro
- assign your macro to short
cut keys
- save your macro
- describe your macro
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.5
© NCC Education LimitedV1.0
Naming a Macro
‱ The name must begin with a letter.
‱ It must not contain spaces.
‱ The underscore must be used to separate words.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.6
© NCC Education LimitedV1.0
Saving a Macro
‱ When a macro is created, it can be saved in one of the
following places:
- This Workbook (the macro will only be available in the
spreadsheet in which it was created.
- Personal Macro Book (the macro will be available to
all spreadsheets)
- New Workbook (the macro
will only be available in that
Spreadsheet which is
newer than previous)
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.7
© NCC Education LimitedV1.0
Starting to Record a Macro
1. Once you have began to record your macro, perform each
action that you want to include in the macro.
2. Note that the mouse can be used to click on commands
and option, but the macro recorder cannot record mouse
actions. To move the insertion point or select, copy, or
move text, you must use keyboard commands.
3. To stop recording your macro at any click on the stop
macro button on the bottom left corner of the screen.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.8
© NCC Education LimitedV1.0
Running a Macro
‱ If the macro has been assigned to a key
combination, that key combination can be pressed
to run the macro.
‱ Or, select the macro from the macro list and then
select Run.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.9
© NCC Education LimitedV1.0
Formatting Titles and Formulas - 1
‱ How to prepare a spreadsheet each month with the
same titles, formatting and formulas:
– When you want to create a new spreadsheet, you
usually open the previous month’s spreadsheet, delete
the figures and save it as the current month’s
spreadsheet.
– If you create a template spreadsheet that contains a
macro that automates the titles, formatting and
formulas, you can open a new spreadsheet, run the
macro and then all you will have to do is enter the
current month’s figures.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.10
© NCC Education LimitedV1.0
Formatting Titles and Formulas - 2
1. Open a new spreadsheet.
2. Open the macro dialogue box.
3. Enter MonthlyFinancialReport for the name of the
macro.
4. Save it in the Personal Macro Workbook.
5. Enter a description.
6. Click OK.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.11
© NCC Education LimitedV1.0
Formatting Titles and Formulas - 3
6. The following data is entered into the spreadsheet:
Each heading is in bold.
7. In cell B5, the formula =SUM(A3-A4) is entered.
8. The contents of cell B5 are copied to cells C5 and D5.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.12
© NCC Education LimitedV1.0
Formatting Titles and Formulas - 4
9. In cell F3 the formula =SUM(B3:E3) is entered.
10. In cell F4 the formula =SUM(B4:E4) is entered.
11. In cell F5 the formula =SUM(B5:E5) is entered.
12. The macro recorder is turned off.
13. Data is then entered into the worksheet to test that
the formulas work.
14. The macro is tested by opening a new spreadsheet
(or worksheet) and running the macro to display the
template.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.13
© NCC Education LimitedV1.0
Format a Table
‱ How to format a table (formatting color, font and border
styles):
1. Open a spreadsheet that either contains a table or
create one.
2. Open the macro dialogue box and enter the
required data.
3. Click OK.
4. Format the table.
5. Stop the recording the macro.
6. Test the macro.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.14
© NCC Education LimitedV1.0
Dates and Times - 1
1. Click in the cell in which you want the date to
appear.
2. Open the Record Macro dialogue box.
3. Enter the macro name, etc., Click OK
4. In the cell enter =TODAY()
5. Right click and choose format cells.
6. Select Month date year as the format, click OK.
7. Stop recording.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.15
© NCC Education LimitedV1.0
Dates and Times - 2
‱ If you do not want the current date to be displayed on
the spreadsheets, but want it to remain static:
1. Create the macro up until the step, ‘Select Month date
year as the format, click OK’.
2. Select Copy > Paste > Paste Values (the value in the
Formula bar changes from TODAY() to the actual date)
3. Press ESC.
4. Stop recording.
5. Test the macro.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.16
© NCC Education LimitedV1.0
Data Entry - 1
‱ In Topic 6 you wrote macros in the VB Editor that
displayed simple message boxes conveying
information to a user, allowing them to click on OK but
not enabling interaction. We will now create simple
interactive macros.
‱ The following is a macro that displays an input box
which requests a user to enter their name; when a user
enters their name, it is displayed on the spreadsheet.
‱ VBA will be used to write the macro in the VB Editor.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.17
© NCC Education LimitedV1.0
Data Entry - 2
Sub EnterName()
Dim strName As String
strName = InputBox("Please enter your name", "Enter Name")
Range("A1").Select
ActiveCell.FormulaR1C1 = strName
End Sub
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.18
© NCC Education LimitedV1.0
Message Boxes and Feedback
‱ The message box below provides feedback to a
user depending on which button is selected.
‱ The macro has been written so that if YES is
selected, another message box is then displayed.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.19
© NCC Education LimitedV1.0
Message Box Code
‱ The code for the previous message box is as
follows:
Sub Message()
x = MsgBox("Do you want to continue?", 3)
If x = 7 Then Exit Sub
MsgBox ("OK, we'll continue using this macro")
End Sub
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.20
© NCC Education LimitedV1.0
Explanation of Message Box Code
‱ These two lines of code need explaining:
x = MsgBox("Do you want to continue?", 3)
If x = 7 Then Exit Sub
‱ The 3 refers to a display of optional buttons that
can be displayed in a message box. There are five
combinations of buttons that can be displayed, as
can be seen on the next slide:
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.21
© NCC Education LimitedV1.0
Message Box Buttons
Name Value Description
vbOKCancel 1 Displays OK and Cancel buttons
vbAbortRetryIgnore 2 Displays Abort, Retry and Ignore
buttons
vbYesNoCancel 3 Displays Yes, No and Cancel buttons
vbYesNo 4 Displays Yes and No buttons
vbRetryCancel 5 Displays Retry and Cancel buttons
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.22
© NCC Education LimitedV1.0
Further Explanation
‱ The following line of code needs to be explained:
If x = 7 Then Exit Sub
‱ When the user clicks on a button, an action occurs
depending on what value has been assigned to the
button
‱ x contains the value 7, which means that when the
No button is clicked, the message box will no longer
be displayed – this value can be seen in with other
values on the following slide:
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.23
© NCC Education LimitedV1.0
Values Assigned to Buttons
Name Value Description
vbOK 1 Ok
VbCancel 2 Cancel
vbAbort 3 Abort
vbRetry 4 Retry
vbIgnore 5 Ignore
vbYes 6 Yes
vbNo 7 No
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.24
© NCC Education LimitedV1.0
Macro on Excel
Create a Macro: With Excel VBA you can
automate tasks in Excel by writing so called
macros.
MsgBox: The MsgBox is a dialog box in Excel VBA
you can use to inform the users of your program.
Range Object: The Range object, which is the
representation of a cell (or cells) on your
worksheet, is the most important object of Excel
VBA.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.25
© NCC Education LimitedV1.0
Variables:How to declare, initialize and display
a variable in Excel VBA.
If Then Statement: Use the If Then statement in
Excel VBA to execute code lines if a specific
condition is met.
Loop: Looping is one of the most powerful
programming techniques. A loop in Excel VBA
enables you to loop through a range of cells with
just a few codes lines.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.26
© NCC Education LimitedV1.0
String Manipulation: In this chapter you find the
most important functions to manipulate strings in
Excel VBA
Date and Time: Learn how to work with dates and
times in Excel VBA.
Events: Events are actions performed by users
which trigger Excel VBA to execute code.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.27
© NCC Education LimitedV1.0
Array: An array is a group of variables. In Excel
VBA, you can refer to a specific variable (element)
of an array by using the array name and the index
number.
Function and Sub: In Excel VBA, a function can
return a value while a sub cannot.
Userform: This chapter teaches you how to create
an Excel VBA Userform
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.28
© NCC Education LimitedV1.0
Range
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(Cells(1, 1), Cells(4, 1)) = 5
Dim example As Range
Set example = Range("A1:C4")
example.Value = 8
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.29
© NCC Education LimitedV1.0
RANGE
Dim example As Range
Set example = Range("A1:C4")
example.Value = 8
Dim example As Range
Set example = Range("A1:C4")
example.Select
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.30
© NCC Education LimitedV1.0
SELECT
Dim example As Range
Set example = Range("A1:C4")
example.Columns(2).Select
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.31
© NCC Education LimitedV1.0
COPY AND PASTE
Range("A1:A2").Select
Selection.Copy
Range("C3").Select
ActiveSheet.Paste
Dim example As Range
Set example = Range("A1:C4")
example.Select
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.32
© NCC Education LimitedV1.0
CLEAR
Range("A1").ClearContents
Range("A1").Value = ""
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.33
© NCC Education LimitedV1.0
COUNT
Dim example As Range
Set example = Range("A1:C4")
MsgBox example.Count
MsgBox example.Rows.Count
Then??
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.34
© NCC Education LimitedV1.0
VARIABLES
Variable Declaration
Dim x As Integer
Dim book As String
Dim x As Double
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.35
© NCC Education LimitedV1.0
VARIABLES
Dim continue As Boolean
continue = True
If continue = True Then
MsgBox "Boolean variables are cool“
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.36
© NCC Education LimitedV1.0
If Then Statement
Dim score As Integer, result As String
score = Range("A1").Value
If score >= 60 Then result = "pass"
Range("B1").Value = result
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.37
© NCC Education LimitedV1.0
For Loop
Dim i As Integer
For i = 1 To 6
Cells(i, 1).Value = 100
Next I
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.38
© NCC Education LimitedV1.0
For Loop
Dim i As Integer, j As Integer
For i = 1 To 6
For j = 1 To 2
Cells(i, j).Value = 100
Next j
Next i
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.39
© NCC Education LimitedV1.0
While Loop
Dim i As Integer
i = 1
While i < 3
Cells(i, 2).Value = 20
i = i + 1
Wend
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.40
© NCC Education LimitedV1.0
Do While Loop
Dim i As Integer
i = 1
Do While i < 6
Cells(i, 1).Value = 20
i = i + 1
Loop
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.41
© NCC Education LimitedV1.0
String Manipulation
Join Strings
Dim text1 As String, text2 As String
text1 = "Hi"
text2 = "Tim"
MsgBox text1 & " " & text2
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.42
© NCC Education LimitedV1.0
Dim text As String
text = "example text"
MsgBox Left(text, 4)
MsgBox Right(text, 4)
MsgBox Len("example text")
MsgBox Instr("example text", "am")
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.43
© NCC Education LimitedV1.0
Date and Time
Dim exampleDate As Date
exampleDate = DateValue("Jun 19, 2010")
MsgBox Year(exampleDate)
Dim firstDate As Date, secondDate As Date
firstDate = DateValue("Jun 19, 2010")
secondDate = DateAdd("d", 3, firstDate)
MsgBox secondDate
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.44
© NCC Education LimitedV1.0
Date and Time
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.45
© NCC Education LimitedV1.0
Workbook Open Event
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.46
© NCC Education LimitedV1.0
One-dimensional Array
Dim Films(1 To 5) As String
Films(1) = "Lord of the Rings"
Films(2) = "Speed"
Films(3) = "Star Wars"
Films(4) = "The Godfather"
Films(5) = "Pulp Fiction"
MsgBox Films(4)
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.47
© NCC Education LimitedV1.0
Two-dimensional Array
Dim Films(1 To 5, 1 To 2) As String
Dim i As Integer, j As Integer
For i = 1 To 5
For j = 1 To 2
Films(i, j) = Cells(i, j).Value
Next j
Next i
MsgBox Films(4, 2)
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.48
© NCC Education LimitedV1.0
Two-dimensional Array
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.49
© NCC Education LimitedV1.0
Function and Sub Procedure
The difference between a function and
a sub in Excel VBA is that a function can return a
value while a sub cannot.
Functions and subs become very useful as
program size increases.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.50
© NCC Education LimitedV1.0
Function
Function Area(x As Double,
y As Double) As Double
Area = x * y
End Function
Dim z As Double
z = Area(3, 5) + 2
MsgBox z
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.51
© NCC Education LimitedV1.0
Sub Procedure
Sub Area(x As Double, y As Double)
MsgBox x * y
End Sub
Area 3, 5
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.52
© NCC Education LimitedV1.0
User form
Form Controls are objects which you can place
onto an Excel Worksheet or User Forms, which
give you the functionality to interact with your data.
For example, drop-down boxes, list boxes,
spinners, and scroll bars are useful for selecting
items from a list
Option Buttons and Check Boxes allow selection
of various options. Buttons allow execution of VBA
code.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.53
© NCC Education LimitedV1.0
User form
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.54
© NCC Education LimitedV1.0
User form
By adding form controls to user forms, we can tell
Excel how the value entered in that should be
treated. This is done a special type of macros
called as Events.
By adding a control to a worksheet and linking it
to a cell, you can return a numeric value for the
current position of the control. You can use that
numeric value in conjunction with the Offset, Index
or other worksheet functions to return values from
lists.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.55
© NCC Education LimitedV1.0
Current Position
Dim emptyRow As Long
'Make Sheet1 active
Sheet1.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.56
© NCC Education LimitedV1.0
Inserting on Current Position
Sub insert_Date()
Cells(emptyRow,1).value=tebox1.value
Cells(emptyRow,2).value=tebox2.value
Cells(emptyRow,3).value=combo1.value
Cells(emptyRow,4).value=listbox1.value
End sub
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.57
© NCC Education LimitedV1.0
Current Position
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.58
© NCC Education LimitedV1.0
Add the Controls
To add the controls to the Userform, execute the
following steps.
1. Open the Visual Basic Editor.
2. Click Insert, Userform. If the Toolbox does not
appear automatically, click View, Toolbox. Your
screen should be set up as below.
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.59
© NCC Education LimitedV1.0
Add the Controls
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.60
© NCC Education LimitedV1.0
Show the Userform
Private Sub CommandButton1_Click()
DinnerPlannerUserForm.Show
End Sub
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.61
© NCC Education LimitedV1.0
References - 1
‱ Gonzalez, J., Meister, C., Ozgur, S., Dilworth, B., Troy, A.
and Brandt, T. (2006). Office VBA Macros You Can Use
Today: Over 100 Amazing Ways to Automate Word,
Excel, PowerPoint, Outlook & Access, Holy Macroi Press.
‱ Microsoft Office: Macros and Security. (2011).
http://www.msoffice-tutorial.com/macro-security.php
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.62
© NCC Education LimitedV1.0
References - 2
‱ Visual Basic/Getting Started. (2010). [Available Online]
http://en.wikibooks.org/wiki/Visual_Basic/Getting_Started
‱ VBScript Glossary. (1997). [Available Online]
http://tinyurl.com/69kxbe5
Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.63
© NCC Education LimitedV1.0
Topic 8 – Using Macros in Microsoft Excel
Part 1
Any Questions?

Weitere Àhnliche Inhalte

Was ist angesagt?

Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0Anjan Mahanta
 
Teaching Excel
Teaching ExcelTeaching Excel
Teaching Excelsam ran
 
Advanced Microsoft Excel
Advanced Microsoft ExcelAdvanced Microsoft Excel
Advanced Microsoft ExcelEric Metelka
 
Excel macro
Excel macroExcel macro
Excel macrokundanpgcil
 
MS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project reportMS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project reportPrafull Dhamankar
 
Excel Macro Magic
Excel Macro MagicExcel Macro Magic
Excel Macro MagicElaine Giles
 
E learning excel vba programming lesson 1
E learning excel vba programming  lesson 1E learning excel vba programming  lesson 1
E learning excel vba programming lesson 1Vijay Perepa
 
MS Access teaching powerpoint tasks
MS Access teaching powerpoint tasksMS Access teaching powerpoint tasks
MS Access teaching powerpoint tasksskomadina
 
Access lesson 03 Creating Queries
Access lesson 03 Creating QueriesAccess lesson 03 Creating Queries
Access lesson 03 Creating QueriesAram SE
 
Microsoft Excel
Microsoft ExcelMicrosoft Excel
Microsoft ExcelYasirKhan357
 
Excel for beginner
Excel for beginnerExcel for beginner
Excel for beginnerShashank Jain
 
ms excel presentation...
ms excel presentation...ms excel presentation...
ms excel presentation...alok1994
 
Scratch Lesson 1 – Basics
Scratch Lesson 1 – BasicsScratch Lesson 1 – Basics
Scratch Lesson 1 – BasicsDavid Oromaner
 
Lesson 1: Scratch Computer Programming
Lesson 1: Scratch Computer ProgrammingLesson 1: Scratch Computer Programming
Lesson 1: Scratch Computer ProgrammingSeniorInfants
 
Introduction to Excel VBA/Macros
Introduction to Excel VBA/MacrosIntroduction to Excel VBA/Macros
Introduction to Excel VBA/Macrosarttan2001
 

Was ist angesagt? (20)

Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
 
Teaching Excel
Teaching ExcelTeaching Excel
Teaching Excel
 
Advanced Microsoft Excel
Advanced Microsoft ExcelAdvanced Microsoft Excel
Advanced Microsoft Excel
 
Excel macro
Excel macroExcel macro
Excel macro
 
MS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project reportMS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project report
 
Excel Macro Magic
Excel Macro MagicExcel Macro Magic
Excel Macro Magic
 
E learning excel vba programming lesson 1
E learning excel vba programming  lesson 1E learning excel vba programming  lesson 1
E learning excel vba programming lesson 1
 
MS Excel Basics
 MS Excel Basics MS Excel Basics
MS Excel Basics
 
Vba part 1
Vba part 1Vba part 1
Vba part 1
 
MS Access teaching powerpoint tasks
MS Access teaching powerpoint tasksMS Access teaching powerpoint tasks
MS Access teaching powerpoint tasks
 
Access lesson 03 Creating Queries
Access lesson 03 Creating QueriesAccess lesson 03 Creating Queries
Access lesson 03 Creating Queries
 
Full project
Full projectFull project
Full project
 
Microsoft Excel
Microsoft ExcelMicrosoft Excel
Microsoft Excel
 
Excel for beginner
Excel for beginnerExcel for beginner
Excel for beginner
 
ms excel presentation...
ms excel presentation...ms excel presentation...
ms excel presentation...
 
Scratch Lesson 1 – Basics
Scratch Lesson 1 – BasicsScratch Lesson 1 – Basics
Scratch Lesson 1 – Basics
 
Ms access
Ms accessMs access
Ms access
 
Lesson 1: Scratch Computer Programming
Lesson 1: Scratch Computer ProgrammingLesson 1: Scratch Computer Programming
Lesson 1: Scratch Computer Programming
 
Excel formulas tf-jul1605
Excel formulas tf-jul1605Excel formulas tf-jul1605
Excel formulas tf-jul1605
 
Introduction to Excel VBA/Macros
Introduction to Excel VBA/MacrosIntroduction to Excel VBA/Macros
Introduction to Excel VBA/Macros
 

Ähnlich wie Using macros in microsoft excel part 1

Using macros in microsoft excel part 2
Using macros in microsoft excel   part 2Using macros in microsoft excel   part 2
Using macros in microsoft excel part 2Er. Nawaraj Bhandari
 
VT University Live Session 3
VT University Live Session 3VT University Live Session 3
VT University Live Session 3VisibleThread
 
ExcelMacros.pptx
ExcelMacros.pptxExcelMacros.pptx
ExcelMacros.pptxAshishDhiman62
 
Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1rupeshkanu
 
Autocad excel vba
Autocad excel vbaAutocad excel vba
Autocad excel vbarjg_vijay
 
Getting Started with Excel Macros – Day #1
Getting Started with Excel Macros – Day #1Getting Started with Excel Macros – Day #1
Getting Started with Excel Macros – Day #1Tariq Al-Basha
 
Working with Excel Macros
Working with Excel MacrosWorking with Excel Macros
Working with Excel MacrosJennifer Garbett
 
VBA
VBAVBA
VBATekish
 
0403-excel-macros-made-easy.pdf
0403-excel-macros-made-easy.pdf0403-excel-macros-made-easy.pdf
0403-excel-macros-made-easy.pdfABHISHEK MALIK
 
Spreadsheet Analytical Tools
Spreadsheet Analytical ToolsSpreadsheet Analytical Tools
Spreadsheet Analytical ToolsJoselito Perez
 
Pranavi verma-class-9-spreadsheet
Pranavi verma-class-9-spreadsheetPranavi verma-class-9-spreadsheet
Pranavi verma-class-9-spreadsheetPranaviVerma
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in Indiaibinstitute0
 

Ähnlich wie Using macros in microsoft excel part 1 (20)

Using macros in microsoft excel part 2
Using macros in microsoft excel   part 2Using macros in microsoft excel   part 2
Using macros in microsoft excel part 2
 
VT University Live Session 3
VT University Live Session 3VT University Live Session 3
VT University Live Session 3
 
ExcelMacros.pptx
ExcelMacros.pptxExcelMacros.pptx
ExcelMacros.pptx
 
Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1
 
Autocad excel vba
Autocad excel vbaAutocad excel vba
Autocad excel vba
 
Vbabook ed2
Vbabook ed2Vbabook ed2
Vbabook ed2
 
Excel 2007 Unit I
Excel 2007 Unit IExcel 2007 Unit I
Excel 2007 Unit I
 
VBA Tips
VBA TipsVBA Tips
VBA Tips
 
50 MS Excel Tips and Tricks
50 MS Excel Tips and Tricks 50 MS Excel Tips and Tricks
50 MS Excel Tips and Tricks
 
Getting Started with Excel Macros – Day #1
Getting Started with Excel Macros – Day #1Getting Started with Excel Macros – Day #1
Getting Started with Excel Macros – Day #1
 
Working with Excel Macros
Working with Excel MacrosWorking with Excel Macros
Working with Excel Macros
 
Vba 2 (students copy)
Vba 2 (students copy)Vba 2 (students copy)
Vba 2 (students copy)
 
VBA
VBAVBA
VBA
 
0403-excel-macros-made-easy.pdf
0403-excel-macros-made-easy.pdf0403-excel-macros-made-easy.pdf
0403-excel-macros-made-easy.pdf
 
Excel300
Excel300Excel300
Excel300
 
ExcelMacros.pptx
ExcelMacros.pptxExcelMacros.pptx
ExcelMacros.pptx
 
Spreadsheet Analytical Tools
Spreadsheet Analytical ToolsSpreadsheet Analytical Tools
Spreadsheet Analytical Tools
 
Pranavi verma-class-9-spreadsheet
Pranavi verma-class-9-spreadsheetPranavi verma-class-9-spreadsheet
Pranavi verma-class-9-spreadsheet
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
 
VBA
VBAVBA
VBA
 

Mehr von Er. Nawaraj Bhandari

Data mining approaches and methods
Data mining approaches and methodsData mining approaches and methods
Data mining approaches and methodsEr. Nawaraj Bhandari
 
Research trends in data warehousing and data mining
Research trends in data warehousing and data miningResearch trends in data warehousing and data mining
Research trends in data warehousing and data miningEr. Nawaraj Bhandari
 
Mining Association Rules in Large Database
Mining Association Rules in Large DatabaseMining Association Rules in Large Database
Mining Association Rules in Large DatabaseEr. Nawaraj Bhandari
 
Introduction to data mining and data warehousing
Introduction to data mining and data warehousingIntroduction to data mining and data warehousing
Introduction to data mining and data warehousingEr. Nawaraj Bhandari
 
Data warehouse physical design
Data warehouse physical designData warehouse physical design
Data warehouse physical designEr. Nawaraj Bhandari
 
Data warehouse logical design
Data warehouse logical designData warehouse logical design
Data warehouse logical designEr. Nawaraj Bhandari
 
Classification and prediction in data mining
Classification and prediction in data miningClassification and prediction in data mining
Classification and prediction in data miningEr. Nawaraj Bhandari
 
Chapter 3: Simplification of Boolean Function
Chapter 3: Simplification of Boolean FunctionChapter 3: Simplification of Boolean Function
Chapter 3: Simplification of Boolean FunctionEr. Nawaraj Bhandari
 
Chapter 5: Cominational Logic with MSI and LSI
Chapter 5: Cominational Logic with MSI and LSIChapter 5: Cominational Logic with MSI and LSI
Chapter 5: Cominational Logic with MSI and LSIEr. Nawaraj Bhandari
 
Chapter 4: Combinational Logic
Chapter 4: Combinational LogicChapter 4: Combinational Logic
Chapter 4: Combinational LogicEr. Nawaraj Bhandari
 
Chapter 2: Boolean Algebra and Logic Gates
Chapter 2: Boolean Algebra and Logic GatesChapter 2: Boolean Algebra and Logic Gates
Chapter 2: Boolean Algebra and Logic GatesEr. Nawaraj Bhandari
 
Introduction to Electronic Commerce
Introduction to Electronic CommerceIntroduction to Electronic Commerce
Introduction to Electronic CommerceEr. Nawaraj Bhandari
 
Evaluating software development
Evaluating software developmentEvaluating software development
Evaluating software developmentEr. Nawaraj Bhandari
 
Using macros in microsoft access
Using macros in microsoft accessUsing macros in microsoft access
Using macros in microsoft accessEr. Nawaraj Bhandari
 
Testing software development
Testing software developmentTesting software development
Testing software developmentEr. Nawaraj Bhandari
 
Application software and business processes
Application software and business processesApplication software and business processes
Application software and business processesEr. Nawaraj Bhandari
 
An introduction to end user software development
An introduction to end user software developmentAn introduction to end user software development
An introduction to end user software developmentEr. Nawaraj Bhandari
 

Mehr von Er. Nawaraj Bhandari (20)

Data mining approaches and methods
Data mining approaches and methodsData mining approaches and methods
Data mining approaches and methods
 
Research trends in data warehousing and data mining
Research trends in data warehousing and data miningResearch trends in data warehousing and data mining
Research trends in data warehousing and data mining
 
Mining Association Rules in Large Database
Mining Association Rules in Large DatabaseMining Association Rules in Large Database
Mining Association Rules in Large Database
 
Introduction to data mining and data warehousing
Introduction to data mining and data warehousingIntroduction to data mining and data warehousing
Introduction to data mining and data warehousing
 
Data warehouse testing
Data warehouse testingData warehouse testing
Data warehouse testing
 
Data warehouse physical design
Data warehouse physical designData warehouse physical design
Data warehouse physical design
 
Data warehouse logical design
Data warehouse logical designData warehouse logical design
Data warehouse logical design
 
Classification and prediction in data mining
Classification and prediction in data miningClassification and prediction in data mining
Classification and prediction in data mining
 
Chapter 3: Simplification of Boolean Function
Chapter 3: Simplification of Boolean FunctionChapter 3: Simplification of Boolean Function
Chapter 3: Simplification of Boolean Function
 
Chapter 6: Sequential Logic
Chapter 6: Sequential LogicChapter 6: Sequential Logic
Chapter 6: Sequential Logic
 
Chapter 5: Cominational Logic with MSI and LSI
Chapter 5: Cominational Logic with MSI and LSIChapter 5: Cominational Logic with MSI and LSI
Chapter 5: Cominational Logic with MSI and LSI
 
Chapter 4: Combinational Logic
Chapter 4: Combinational LogicChapter 4: Combinational Logic
Chapter 4: Combinational Logic
 
Chapter 2: Boolean Algebra and Logic Gates
Chapter 2: Boolean Algebra and Logic GatesChapter 2: Boolean Algebra and Logic Gates
Chapter 2: Boolean Algebra and Logic Gates
 
Chapter 1: Binary System
 Chapter 1: Binary System Chapter 1: Binary System
Chapter 1: Binary System
 
Introduction to Electronic Commerce
Introduction to Electronic CommerceIntroduction to Electronic Commerce
Introduction to Electronic Commerce
 
Evaluating software development
Evaluating software developmentEvaluating software development
Evaluating software development
 
Using macros in microsoft access
Using macros in microsoft accessUsing macros in microsoft access
Using macros in microsoft access
 
Testing software development
Testing software developmentTesting software development
Testing software development
 
Application software and business processes
Application software and business processesApplication software and business processes
Application software and business processes
 
An introduction to end user software development
An introduction to end user software developmentAn introduction to end user software development
An introduction to end user software development
 

KĂŒrzlich hochgeladen

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

KĂŒrzlich hochgeladen (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

Using macros in microsoft excel part 1

  • 1. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.1 © NCC Education LimitedV1.0 Uses of Macros in Microsoft Excel ‱ Macros can be used in Excel to automate tasks, such as: – Data entry and validation – Data selection – Interactive user forms – Formatting features and functions – Data output – Navigation
  • 2. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.2 © NCC Education LimitedV1.0 Creating a Macro ‱ Plan your macro - you should plan the steps that you want a macro to perform before you record or write it (remember the importance of planning was discussed in Topic 2, Slide 18). ‱ A macro that is used often can be assigned to shortcut keys, buttons, pictures or icons, without having to open the Macro dialogue box. ‱ A macro can be created by either recording it using the Macro recorder or writing VBA code in the VB Editor. Each development method will be used in this Topic and in Topic 9.
  • 3. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.3 © NCC Education LimitedV1.0 Overview of Recording a Macro ‱ The macro recorder is turned on. ‱ A name for the macro is entered, it is assigned to a short cut key (optional), it is saved and a description is given to it (optional). ‱ The recorder is started. ‱ Steps for the macro are entered. ‱ The recorder is stopped.
  • 4. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.4 © NCC Education LimitedV1.0 Turning on the Macro Recorder 1. Select the Macro option. 2. The Record Macro dialogue box is displayed and here you can: - name your macro - assign your macro to short cut keys - save your macro - describe your macro
  • 5. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.5 © NCC Education LimitedV1.0 Naming a Macro ‱ The name must begin with a letter. ‱ It must not contain spaces. ‱ The underscore must be used to separate words.
  • 6. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.6 © NCC Education LimitedV1.0 Saving a Macro ‱ When a macro is created, it can be saved in one of the following places: - This Workbook (the macro will only be available in the spreadsheet in which it was created. - Personal Macro Book (the macro will be available to all spreadsheets) - New Workbook (the macro will only be available in that Spreadsheet which is newer than previous)
  • 7. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.7 © NCC Education LimitedV1.0 Starting to Record a Macro 1. Once you have began to record your macro, perform each action that you want to include in the macro. 2. Note that the mouse can be used to click on commands and option, but the macro recorder cannot record mouse actions. To move the insertion point or select, copy, or move text, you must use keyboard commands. 3. To stop recording your macro at any click on the stop macro button on the bottom left corner of the screen.
  • 8. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.8 © NCC Education LimitedV1.0 Running a Macro ‱ If the macro has been assigned to a key combination, that key combination can be pressed to run the macro. ‱ Or, select the macro from the macro list and then select Run.
  • 9. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.9 © NCC Education LimitedV1.0 Formatting Titles and Formulas - 1 ‱ How to prepare a spreadsheet each month with the same titles, formatting and formulas: – When you want to create a new spreadsheet, you usually open the previous month’s spreadsheet, delete the figures and save it as the current month’s spreadsheet. – If you create a template spreadsheet that contains a macro that automates the titles, formatting and formulas, you can open a new spreadsheet, run the macro and then all you will have to do is enter the current month’s figures.
  • 10. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.10 © NCC Education LimitedV1.0 Formatting Titles and Formulas - 2 1. Open a new spreadsheet. 2. Open the macro dialogue box. 3. Enter MonthlyFinancialReport for the name of the macro. 4. Save it in the Personal Macro Workbook. 5. Enter a description. 6. Click OK.
  • 11. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.11 © NCC Education LimitedV1.0 Formatting Titles and Formulas - 3 6. The following data is entered into the spreadsheet: Each heading is in bold. 7. In cell B5, the formula =SUM(A3-A4) is entered. 8. The contents of cell B5 are copied to cells C5 and D5.
  • 12. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.12 © NCC Education LimitedV1.0 Formatting Titles and Formulas - 4 9. In cell F3 the formula =SUM(B3:E3) is entered. 10. In cell F4 the formula =SUM(B4:E4) is entered. 11. In cell F5 the formula =SUM(B5:E5) is entered. 12. The macro recorder is turned off. 13. Data is then entered into the worksheet to test that the formulas work. 14. The macro is tested by opening a new spreadsheet (or worksheet) and running the macro to display the template.
  • 13. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.13 © NCC Education LimitedV1.0 Format a Table ‱ How to format a table (formatting color, font and border styles): 1. Open a spreadsheet that either contains a table or create one. 2. Open the macro dialogue box and enter the required data. 3. Click OK. 4. Format the table. 5. Stop the recording the macro. 6. Test the macro.
  • 14. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.14 © NCC Education LimitedV1.0 Dates and Times - 1 1. Click in the cell in which you want the date to appear. 2. Open the Record Macro dialogue box. 3. Enter the macro name, etc., Click OK 4. In the cell enter =TODAY() 5. Right click and choose format cells. 6. Select Month date year as the format, click OK. 7. Stop recording.
  • 15. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.15 © NCC Education LimitedV1.0 Dates and Times - 2 ‱ If you do not want the current date to be displayed on the spreadsheets, but want it to remain static: 1. Create the macro up until the step, ‘Select Month date year as the format, click OK’. 2. Select Copy > Paste > Paste Values (the value in the Formula bar changes from TODAY() to the actual date) 3. Press ESC. 4. Stop recording. 5. Test the macro.
  • 16. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.16 © NCC Education LimitedV1.0 Data Entry - 1 ‱ In Topic 6 you wrote macros in the VB Editor that displayed simple message boxes conveying information to a user, allowing them to click on OK but not enabling interaction. We will now create simple interactive macros. ‱ The following is a macro that displays an input box which requests a user to enter their name; when a user enters their name, it is displayed on the spreadsheet. ‱ VBA will be used to write the macro in the VB Editor.
  • 17. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.17 © NCC Education LimitedV1.0 Data Entry - 2 Sub EnterName() Dim strName As String strName = InputBox("Please enter your name", "Enter Name") Range("A1").Select ActiveCell.FormulaR1C1 = strName End Sub
  • 18. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.18 © NCC Education LimitedV1.0 Message Boxes and Feedback ‱ The message box below provides feedback to a user depending on which button is selected. ‱ The macro has been written so that if YES is selected, another message box is then displayed.
  • 19. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.19 © NCC Education LimitedV1.0 Message Box Code ‱ The code for the previous message box is as follows: Sub Message() x = MsgBox("Do you want to continue?", 3) If x = 7 Then Exit Sub MsgBox ("OK, we'll continue using this macro") End Sub
  • 20. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.20 © NCC Education LimitedV1.0 Explanation of Message Box Code ‱ These two lines of code need explaining: x = MsgBox("Do you want to continue?", 3) If x = 7 Then Exit Sub ‱ The 3 refers to a display of optional buttons that can be displayed in a message box. There are five combinations of buttons that can be displayed, as can be seen on the next slide:
  • 21. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.21 © NCC Education LimitedV1.0 Message Box Buttons Name Value Description vbOKCancel 1 Displays OK and Cancel buttons vbAbortRetryIgnore 2 Displays Abort, Retry and Ignore buttons vbYesNoCancel 3 Displays Yes, No and Cancel buttons vbYesNo 4 Displays Yes and No buttons vbRetryCancel 5 Displays Retry and Cancel buttons
  • 22. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.22 © NCC Education LimitedV1.0 Further Explanation ‱ The following line of code needs to be explained: If x = 7 Then Exit Sub ‱ When the user clicks on a button, an action occurs depending on what value has been assigned to the button ‱ x contains the value 7, which means that when the No button is clicked, the message box will no longer be displayed – this value can be seen in with other values on the following slide:
  • 23. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.23 © NCC Education LimitedV1.0 Values Assigned to Buttons Name Value Description vbOK 1 Ok VbCancel 2 Cancel vbAbort 3 Abort vbRetry 4 Retry vbIgnore 5 Ignore vbYes 6 Yes vbNo 7 No
  • 24. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.24 © NCC Education LimitedV1.0 Macro on Excel Create a Macro: With Excel VBA you can automate tasks in Excel by writing so called macros. MsgBox: The MsgBox is a dialog box in Excel VBA you can use to inform the users of your program. Range Object: The Range object, which is the representation of a cell (or cells) on your worksheet, is the most important object of Excel VBA.
  • 25. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.25 © NCC Education LimitedV1.0 Variables:How to declare, initialize and display a variable in Excel VBA. If Then Statement: Use the If Then statement in Excel VBA to execute code lines if a specific condition is met. Loop: Looping is one of the most powerful programming techniques. A loop in Excel VBA enables you to loop through a range of cells with just a few codes lines.
  • 26. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.26 © NCC Education LimitedV1.0 String Manipulation: In this chapter you find the most important functions to manipulate strings in Excel VBA Date and Time: Learn how to work with dates and times in Excel VBA. Events: Events are actions performed by users which trigger Excel VBA to execute code.
  • 27. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.27 © NCC Education LimitedV1.0 Array: An array is a group of variables. In Excel VBA, you can refer to a specific variable (element) of an array by using the array name and the index number. Function and Sub: In Excel VBA, a function can return a value while a sub cannot. Userform: This chapter teaches you how to create an Excel VBA Userform
  • 28. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.28 © NCC Education LimitedV1.0 Range 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(Cells(1, 1), Cells(4, 1)) = 5 Dim example As Range Set example = Range("A1:C4") example.Value = 8
  • 29. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.29 © NCC Education LimitedV1.0 RANGE Dim example As Range Set example = Range("A1:C4") example.Value = 8 Dim example As Range Set example = Range("A1:C4") example.Select
  • 30. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.30 © NCC Education LimitedV1.0 SELECT Dim example As Range Set example = Range("A1:C4") example.Columns(2).Select
  • 31. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.31 © NCC Education LimitedV1.0 COPY AND PASTE Range("A1:A2").Select Selection.Copy Range("C3").Select ActiveSheet.Paste Dim example As Range Set example = Range("A1:C4") example.Select
  • 32. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.32 © NCC Education LimitedV1.0 CLEAR Range("A1").ClearContents Range("A1").Value = ""
  • 33. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.33 © NCC Education LimitedV1.0 COUNT Dim example As Range Set example = Range("A1:C4") MsgBox example.Count MsgBox example.Rows.Count Then??
  • 34. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.34 © NCC Education LimitedV1.0 VARIABLES Variable Declaration Dim x As Integer Dim book As String Dim x As Double
  • 35. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.35 © NCC Education LimitedV1.0 VARIABLES Dim continue As Boolean continue = True If continue = True Then MsgBox "Boolean variables are cool“
  • 36. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.36 © NCC Education LimitedV1.0 If Then Statement Dim score As Integer, result As String score = Range("A1").Value If score >= 60 Then result = "pass" Range("B1").Value = result
  • 37. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.37 © NCC Education LimitedV1.0 For Loop Dim i As Integer For i = 1 To 6 Cells(i, 1).Value = 100 Next I
  • 38. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.38 © NCC Education LimitedV1.0 For Loop Dim i As Integer, j As Integer For i = 1 To 6 For j = 1 To 2 Cells(i, j).Value = 100 Next j Next i
  • 39. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.39 © NCC Education LimitedV1.0 While Loop Dim i As Integer i = 1 While i < 3 Cells(i, 2).Value = 20 i = i + 1 Wend
  • 40. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.40 © NCC Education LimitedV1.0 Do While Loop Dim i As Integer i = 1 Do While i < 6 Cells(i, 1).Value = 20 i = i + 1 Loop
  • 41. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.41 © NCC Education LimitedV1.0 String Manipulation Join Strings Dim text1 As String, text2 As String text1 = "Hi" text2 = "Tim" MsgBox text1 & " " & text2
  • 42. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.42 © NCC Education LimitedV1.0 Dim text As String text = "example text" MsgBox Left(text, 4) MsgBox Right(text, 4) MsgBox Len("example text") MsgBox Instr("example text", "am")
  • 43. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.43 © NCC Education LimitedV1.0 Date and Time Dim exampleDate As Date exampleDate = DateValue("Jun 19, 2010") MsgBox Year(exampleDate) Dim firstDate As Date, secondDate As Date firstDate = DateValue("Jun 19, 2010") secondDate = DateAdd("d", 3, firstDate) MsgBox secondDate
  • 44. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.44 © NCC Education LimitedV1.0 Date and Time
  • 45. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.45 © NCC Education LimitedV1.0 Workbook Open Event
  • 46. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.46 © NCC Education LimitedV1.0 One-dimensional Array Dim Films(1 To 5) As String Films(1) = "Lord of the Rings" Films(2) = "Speed" Films(3) = "Star Wars" Films(4) = "The Godfather" Films(5) = "Pulp Fiction" MsgBox Films(4)
  • 47. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.47 © NCC Education LimitedV1.0 Two-dimensional Array Dim Films(1 To 5, 1 To 2) As String Dim i As Integer, j As Integer For i = 1 To 5 For j = 1 To 2 Films(i, j) = Cells(i, j).Value Next j Next i MsgBox Films(4, 2)
  • 48. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.48 © NCC Education LimitedV1.0 Two-dimensional Array
  • 49. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.49 © NCC Education LimitedV1.0 Function and Sub Procedure The difference between a function and a sub in Excel VBA is that a function can return a value while a sub cannot. Functions and subs become very useful as program size increases.
  • 50. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.50 © NCC Education LimitedV1.0 Function Function Area(x As Double, y As Double) As Double Area = x * y End Function Dim z As Double z = Area(3, 5) + 2 MsgBox z
  • 51. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.51 © NCC Education LimitedV1.0 Sub Procedure Sub Area(x As Double, y As Double) MsgBox x * y End Sub Area 3, 5
  • 52. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.52 © NCC Education LimitedV1.0 User form Form Controls are objects which you can place onto an Excel Worksheet or User Forms, which give you the functionality to interact with your data. For example, drop-down boxes, list boxes, spinners, and scroll bars are useful for selecting items from a list Option Buttons and Check Boxes allow selection of various options. Buttons allow execution of VBA code.
  • 53. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.53 © NCC Education LimitedV1.0 User form
  • 54. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.54 © NCC Education LimitedV1.0 User form By adding form controls to user forms, we can tell Excel how the value entered in that should be treated. This is done a special type of macros called as Events. By adding a control to a worksheet and linking it to a cell, you can return a numeric value for the current position of the control. You can use that numeric value in conjunction with the Offset, Index or other worksheet functions to return values from lists.
  • 55. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.55 © NCC Education LimitedV1.0 Current Position Dim emptyRow As Long 'Make Sheet1 active Sheet1.Activate 'Determine emptyRow emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
  • 56. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.56 © NCC Education LimitedV1.0 Inserting on Current Position Sub insert_Date() Cells(emptyRow,1).value=tebox1.value Cells(emptyRow,2).value=tebox2.value Cells(emptyRow,3).value=combo1.value Cells(emptyRow,4).value=listbox1.value End sub
  • 57. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.57 © NCC Education LimitedV1.0 Current Position
  • 58. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.58 © NCC Education LimitedV1.0 Add the Controls To add the controls to the Userform, execute the following steps. 1. Open the Visual Basic Editor. 2. Click Insert, Userform. If the Toolbox does not appear automatically, click View, Toolbox. Your screen should be set up as below.
  • 59. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.59 © NCC Education LimitedV1.0 Add the Controls
  • 60. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.60 © NCC Education LimitedV1.0 Show the Userform Private Sub CommandButton1_Click() DinnerPlannerUserForm.Show End Sub
  • 61. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.61 © NCC Education LimitedV1.0 References - 1 ‱ Gonzalez, J., Meister, C., Ozgur, S., Dilworth, B., Troy, A. and Brandt, T. (2006). Office VBA Macros You Can Use Today: Over 100 Amazing Ways to Automate Word, Excel, PowerPoint, Outlook & Access, Holy Macroi Press. ‱ Microsoft Office: Macros and Security. (2011). http://www.msoffice-tutorial.com/macro-security.php
  • 62. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.62 © NCC Education LimitedV1.0 References - 2 ‱ Visual Basic/Getting Started. (2010). [Available Online] http://en.wikibooks.org/wiki/Visual_Basic/Getting_Started ‱ VBScript Glossary. (1997). [Available Online] http://tinyurl.com/69kxbe5
  • 63. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.63 © NCC Education LimitedV1.0 Topic 8 – Using Macros in Microsoft Excel Part 1 Any Questions?

Hinweis der Redaktion

  1. NCC Education - End Slide Master