Introduction to VBA: Leveraging the Power of MS Excel

14
OSUINFORMS Introduction to VBA: Leveraging the Power of MS Excel OSUINFORMS 2015 Tutorial Series Sayak Roychowdhury PhD Student, ISE 1/17

Transcript of Introduction to VBA: Leveraging the Power of MS Excel

Page 1: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS 2015 Tutorial Series

Sayak Roychowdhury

PhD Student, ISE

1/17

Page 2: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

• Why use VBA?

• VBA Environment

• Declaration, Read, Write

• Data Types

• Fuctions

• GUI

• Record Macros • https://www.informs.org/Community/OSU-INFORMS

Outline

2/17

Page 3: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

• Comes with MS Excel, used almost everywhere

• Easy on syntax

• Read and Write data on excel sheets

• Can use built-in functions made for Excel, e.g. MAX,MIN, MINVERSE

Why use VBA

3/17

Page 4: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

VBA Environment

4/17

Page 5: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

VBA Environment

5/17

Page 6: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

VBA Environment

6/17

Project Pane

Property Pane

Editor

Run Buttons

Page 7: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

VBA Environment

7/17

WorkSheet Names

WorkSheet Properties

Module Names

Page 8: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

8/17

Declaration, Read, Write Begin Subroutine

Declare Variables

Read from WorkSheet

Page 9: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

9/17

Declaration, Read, Write Select/Clear

Write to Worksheet

Write to Worksheet (with loop)

Page 10: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

• Short,Integer, Long • Single, Double • Char • Date • Boolean • Object • Variant (can contain any type, except string)

https://msdn.microsoft.com/en-us/library/47zceaw7.aspx

10/17

Data Types

Page 11: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

11/17

Functions and Function Call

User-defined Function

Function Call

Excel Function Call (Generate normal random number)

Page 12: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

12/17

GUI Developer Tab

Assign Macro to command button

Page 13: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

• Developer Tab -> Click Record Macro ->Give Name of the Macro-> Do the task-> Stop Recording

• Macro is saved in the module

13/17

Record Macro

Page 14: Introduction to VBA: Leveraging the Power of MS Excel

OSUINFORMS

Hope it was useful! 14/17