Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman...

44
Microsoft Excel Macros & Excel Solver (IENG490 ) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi

Transcript of Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman...

Page 1: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Microsoft Excel Macros&

Excel Solver

(IENG490)

Spring:(2013-2014)Research Assistants:

Arman Nedjati & Mahmoud Golabi

Page 2: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Excel Solver

Page 3: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example1

Page 4: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example2

Page 5: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example2

Page 6: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Contents of Macros

• Introduction to Macro• Introduction to Visual Basic for Application• Recording a Macro• Looking at the code of Recorded Macro

Page 7: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Introduction to Visual Basic for Application

• Microsoft Excel in itself a tool which helps users to work with worksheets & workbooks with ease.

• This tool is one of the ultimate timesaver, but there are numerous events when you have to do repetitive tasks which are of a very little value but consume your precious time; yet engage you in a less productive activity, which Excel may do automatically.

• Microsoft Excel macro is the feature which eliminates or at-least minimize these events which take your precious time in repetitive tasks, and you will be able to capitalize this time in more productive & strategic activities.

Page 8: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Introduction to Visual Basic for Application - cont…

• Apart from automating repetitive tasks Microsoft Excel Macros may also be used to create user interfaces

• User interfaces are screens where user can give inputs with ease also it enables input validations

Page 9: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Introduction to Visual Basic for Application - cont…

• What is Visual Basic?Visual Basic is a language which is inherited from a very popular language BASIC. BASIC stands for Beginners All-purpose Symbolic Instruction Code. Visual Basic is called visual as you can do most of the program by click and go i.e. visually. It’s an event driven & object based language.

• What is Event Driven Language?When we say event driven it means that you can write triggers which will initiate on specific events for example when user opens any document or saves any document

• What is Object Based Language?Object based language is language in which we can use objects & their properties i.e. we can use the Worksheet object and its property SheetName when some one clicks on a button

Page 10: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Introduction to Visual Basic for Application - cont…

• Visual Basic for ApplicationVisual Basic for Application is called VBA as it uses Visual Basic language and is capable of using application specific objects i.e. if we talk about VBA for Excel it can use Cell Object, Range Object, Worksheet Object, Workbook Object etc.

Page 11: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Recording your first Macro

• Microsoft Excel provides you a very helpful option which enables you to record a macro and also modify and run afterwards

• Microsoft Excel don’t provide you the macro recording option by default for enabling macro recording at first you need to enable the Developer Tab, next slide defines step by step procedure to enable Developer Tab

Page 12: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Enabling the Developer Tab

• Click on File• Click on Excel Options• On Customize Ribbon

check Developer tab

1

2

3

Page 13: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Enabling the Developer Tab

Page 14: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Recording your first Macro• When you record macro Microsoft Excel

provides you with 2 options either you can record a macro using Absolute References or Relative References

• If you go for Absolute References Excel’s behavior will be:– You record a macro in Cell A1 type “Excel is Great”

format Bold Cell B1– Whenever you play / run this macro Excel will

always type “Excel is Great” in Cell A1 and format Bold Cell B1 no matter wherever your cursor is

Page 15: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Recording your first Macro

• You can refer to following tab that you can click on Use Relative References which will enable you to record a macro in relative cells, as discussed whenever a relative reference macro is executed it will run in-place

Page 16: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Recording your first Macro

1. Click Developer Tab2. Click on Use Relative Reference3. Click on Record Macro

1

2

3

Page 17: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Recording your first Macro• As soon as you press record macro button the

following screen will appear• In the Macro name box

you have to type the nameof macro

• In Shortcut key you can typeshort cut key which invokethe Macro

• Store macro (will discussedlater)

• You can also provide description of the macro

Page 18: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Recording your first Macro

• As soon as you press OK in the previous dialog box two things happens first record macro button changesto Stop Recording

• Secondly Excel starts lookingfor your action and will recordthe same

• Please follow steps on the following slide to complete recording

Page 19: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Recording your first Macro

1. Click on Cell A1 type “Excel is Great”2. Select cells from A1:C1 click on Home Tab

and click on Merge & Center 3. Press Bold & Italics4. Now click on Developer Tab and click on Stop

recording

Page 20: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Playing your Macro

• You can execute the macro you just recorded by either clicking on the Macro button on the Developer Tab or by pressing Alt F8 , you can see the list of Recorded macros in the dialog box you can press Run buttonto execute the macro as soonas you press run you can seethat your recorded macro willexecute

Page 21: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Behind the scenes

• To see what Excel did behind the scenes you can either click on Macros button on Developer Tab and select Edit or you can press Alt F11

Page 22: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

What it looks like

• This is what Excel did behind the scenes, it written the code for you. You can change the code if you want. You can either closethe window or pressAlt F11 again to goback to your Excelnormal screen.

Page 23: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example1

Page 24: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example1

Page 25: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example1

Working with Excel while recording the macro

Page 26: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example1

Page 27: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example1

Page 28: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example1

Page 29: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Looking inside the VB code of our Macro

Page 30: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Using Form Controls

Page 31: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Using Form ControlsUsing the Form Controls select a GUI element and by mouse-click place it on the frame

Page 32: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Using Form Controls

Page 33: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Using Form Controls

SelectThe Cell and

click on button

Page 34: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example2:

Develop a macro to calculate the average of five cells to the left of a selected cell.

Initialize a spreadsheet and fill five vertical cells with numbers. Select the cell to the right

Page 35: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example2

Start macro-recording and modifymacro properties

Page 36: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example2

Input formula in cell C2 and then stop macro-recording.

Page 37: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example2

Test the macro by filling other five cells with numbers, then select the cell to the rightand then click shortcut-key. (we had chosen [Ctrl-a] )

Page 38: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example2

Click [Alt-F11] to access VBA module

Page 39: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example3

Writing a subroutine to solve a set of linear equations while allowing the user to input cell-range.

Step 1. Click [Alt-F11] to open the Visual Basic Editor.Step 2. Select [Insert][Module].

Page 40: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example3

Step 3. Type in the following code:

Page 41: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example3

Step 4. Go back to the Excel workbook by clicking [Alt-F11] once more. Then click [Alt-F8] to change the macro options and select shortcut key (plus add description if desired).

Page 42: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

Example3

Step 5. Test the macro. First prepare the spreadsheet with a matrix which will be treated as matrix A and column of cells to be treated as vector b. Then run the macro.

Page 43: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

43

Conclusion• Macros are used to automate tasks or you can use macros to

develop user interfaces• Excel Macro use a language called VBA (Visual Basic for

Applications), which is both Event Driven and Object Based• The easiest way to look at macro is by using the Excel’s record

Macro Feature• You can even edit Excel’s recorded macro by yourself• Soon we will publish a presentation which will guide you on

how to write your own macro

Page 44: Microsoft Excel Macros & Excel Solver (IENG490) Spring:(2013-2014) Research Assistants: Arman Nedjati & Mahmoud Golabi.

FINISH