Sei sulla pagina 1di 34

C H A P T E R 3.

Working With
Spreadsheets
CHAPTER OUTLINE
SPREADSHEET CONCEPTS
SPREADSHEET PROGRAMMING
DEVELOPING MACROS IN MS EXCEL
INTRODUCTION

Chapter Objectives
At the end of the chapter, the
student should be able to:
explain the theories behind
spreadsheet programs;
interface MS Excel with Visual
BASIC; and
write macros in MS Excel.

2
INTRODUCTION
Spreadsheets

A spreadsheet is an
interactive computer
application program for
organization, calculation
and analysis of data in
tabular form.
Spreadsheets developed
as computerized
simulations of paper
accounting worksheets.
INTRODUCTION
History of Spreadsheets

In 1978, Dan Bricklin noticed that


he was doing he was doing the
same paper spreadsheet in his
business class.
Working with his friend Bob
Frankston, they invented VisiCalc
for Apple IIe computer.
Over the years, many companies
sold spreadsheet programs, in
which today, MS Excel leads the
market.
SPREADSHEET CONCEPTS
MS Excel

Microsoft Excel is a spreadsheet


application developed by Microsoft
for Microsoft Windows and Mac OS
as part of the MS Office Suite.
It features calculation, graphing
tools, pivot tables, and a macro
programming language called
Visual Basic for Applications.
It has replaced Lotus 1-2-3 as the
industry standard for spreadsheets.
SPREADSHEET CONCEPTS
Excel Terminologies

Microsoft Excel documents are known as workbooks.


A workbook can consist of any number of worksheets
which are accessed thru a tab that appears at the
bottom of the workbook window.
The table below summarizes the file extension of
workbooks depending on the version and features it has.
SPREADSHEET CONCEPTS
Excel Terminologies (cont.)

As a very large table, a worksheet is composed several


rows (represented as numbers) and columns (represented
as letter).
In Excel 2010, the maximum worksheet size is 1,048,576
rows by 16,384 columns.
The intersection of a row and column is called a cell which
can hold string, numeric, or boolean data, as well as
formula for manipulating these data.
A formula is a special type of cell entry that returns a
result (to type a formula, start the entry with either = or
+ symbol.
SPREADSHEET PROGRAMMING
Excel Object Model

Visual BASIC can interact with Excel through the Excel


Object Model.
It is a vocabulary identifying spreadsheet objects, and
a set of supplied functions or methods that enable
reading and writing to the spreadsheet and interaction
with its users.
In order to call MS Excel in VB, the Microsoft Office
Object Library should be first added thru the Project
menu.
SPREADSHEET PROGRAMMING
Using Excel Object Model

In order to call MS Excel in VB, the Microsoft Office Object


Library should be first added thru the Project menu.
Project -> Add Reference -> COM Tab -> Microsoft Office
<Version> Object Library*

*Sometimes, you need to select Microsoft Office <Version> Excel Object Library
SPREADSHEET PROGRAMMING
Using Excel Object Model (cont.)

To start using Excel, type the following code at the beginning of the
code (before the form class statement):
Imports Excel = Microsoft.Office.Interop.Excel

Excel can be invoked in a Windows Form Application in Visual Basic


using the following Codes in a Module:
Public oXL As New Excel.Application
Public oWB As Excel.Workbook
Public oSheet As Excel.Worksheet

You may replace the keyword Public with Dim if declaration would
be made inside the form class.
SPREADSHEET PROGRAMMING
Using Excel Object Model (cont.)

To Start the Excel Application write the following code at the


start of an event handler such as a button:
oXL.Visible = True

To create a new workbook and worksheet, write:


oWB = oXL.Workbooks.Add
oSheet = oWB.Sheets ("Sheet1")

To enable user control (shift the focus of the cursor), write:


oXL.UserControl = True
SPREADSHEET PROGRAMMING
Using Excel Object Model (cont.)

To Add an entry to a cell (Note that Excels rows and


columns starts with 1):
oSheet.Cells(<Row>, <Column>).Value = <Value>
Or
oSheet.Range(<Cell Address>).Value = <Value>

Example:
oSheet.Cells(8, 2).Value = Sample
Or
oSheet.Range(B8).Value = Sample
SPREADSHEET PROGRAMMING
Using Excel Object Model (cont.)

To Add a formula to a cell:


oSheet.Cells(<Row>, <Column>).Formula = <formula>
Or
oSheet.Range(<Cell Address>).Formula = <formula>

Example:

oSheet.Cells(1, 1).Value = =AVERAGE(A1:B4)


Or
oSheet.Range("A1").Value = =AVERAGE(A1:B4)
SPREADSHEET PROGRAMMING
Example Program No. 1

Create a program that will log and calculate daily


expenses in an Excel workbook.
SPREADSHEET PROGRAMMING
Example Program No.1 (cont.)
Dim oXL As New Excel.Application 'Creates an instance of Excel Application
Dim oWB As Excel.Workbook 'Creates an instance of Workbook Object
Dim oSheet As Excel.Worksheet 'Creates an instance of a Worsksheet Object
Dim oChart As Excel.Chart 'Creates an instance of a Chart Object

Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click


'Adds a New Workbook
oWB = oXL.Workbooks.Add

'Display Excel
oXL.Visible = True

'Sets the relevant sheet that we want to work with


oSheet = oWB.Sheets("Sheet1")

With oSheet
'Directly type the values that we want
.Range("A1").Value = "Food"
.Range("A2").Value = "Transportation"
.Range("A3").Value = "Recreation"
.Range("A4").Value = "Total"

.Range("B1").Value = txtFood.Text
.Range("B2").Value = txtTransportation.Text
.Range("B3").Value = txtRecreation.Text
.Range("B4").Formula = "=Sum(B1:B3)"

.Range("A1:A4, B4").Font.Bold = True 'sets font to bold


End With
End Sub
SPREADSHEET PROGRAMMING
Exporting Excel Charts to VB Forms

To create a chart in excel, use the following syntax:


Dim <chartname> As Excel.Chart
<chartname> = <workbookname>.Charts.Add()

To select a chart-type and data range use the following:


<chartname>.ApplyCustomType(<chart type>)
<chartname>.SetSourceData(Source:=oSheet.Range(<range>"))
SPREADSHEET PROGRAMMING
Exporting Excel Charts to VB Forms (cont.)

Upon creating a chart in a separate sheet, export the


chart as an image object as shown in the example:
<chartname>.Export(Filename:=<address>", FilterName:="GIF")

Simply call an image object or use the picture as a


background of a form:
Me.BackgroundImage = System.Drawing.Image.FromFile(<address>")
SPREADSHEET PROGRAMMING
Example Program No.2
SPREADSHEET PROGRAMMING
Closing Excel Instances

Instances of Excel can be closed automatically using the


following syntax:
Tells Excel that the workbook was saved so that no
confirmation messagebox will show (although workbook
wasnt actually saved).
oWB.Saved = True
oXL.Quit() Closes Excel

Note that the code above is added before releasing of the


Excel Objects
SPREADSHEET PROGRAMMING
Closing Excel Instances (cont.)

Make sure you release each object at the end of the


operation
oRng = Nothing
oSheet = Nothing
oWB = Nothing
OXL = Nothing
DEVELOPING MACROS IN MS EXCEL
Macro
A macro (short for macroinstruction)
is a single instruction that expands
automatically into a set of
instructions to perform a particular
task.
A tool for writing macros is
sometimes included in a software
application or as a part of a
programming language.
Macros are used to make tasks using
the application less repetitive or a
tool.
DEVELOPING MACROS IN MS EXCEL
Macro (cont.)
Enable Macro in Excel
To enable the developer tab in the Ribbon Interface (MS Excel
2010 and up), go to:
Options (or right-click on the ribbon interface) Customize
Ribbon Check Developer under the Main Tab Section.
DEVELOPING MACROS IN MS EXCEL
Macro (cont.)
Creating Macros
1. By recording actions. When you record a macro, Excel
stores information about each step you take as you
perform a series of commands. You then run the macro
to repeat, or "play back," the commands.
2. By programming codes in Visual BASIC for
Applications (VBA). VBA can write codes, edit macros,
copy macros from one module to another, copy macros
between different workbooks, rename the modules that
store the macros, or rename the macros. Recorded
macros can also be edited in VBA.
DEVELOPING MACROS IN MS EXCEL
Recording a Macro

1. In the developer tab, click Record


Macro.
You may select Use Relative References so
that actions will be recorded with respect to the
selected cell and not on the actual cell address.

2. Name your macro (without space).


You may assign a shortcut key to your macro.
Choose a storage for your macro. In this case,
just save it within the workbook.
If you want your macro to be available in any
open workbook, you can place it in your
Personal Macro Workbook (physically located
in AppData of Windows).
A description maybe added for reference.
DEVELOPING MACROS IN MS EXCEL
Recording a Macro (cont.)
3. Click Ok and go to a worksheet. Anything you do will be
recorded.
4. Go back to developer tab and click Stop Recording and all
your actions were recorded into the macro.
DEVELOPING MACROS IN MS EXCEL

VBA
The Windows version of Excel supports programming
through VBA which is an implementation of VB6 and its
associated IDE.
Programming with VBA allows spreadsheet
manipulation that is difficult or impossible with standard
spreadsheet functions and techniques.
Codes are written directly using the Visual Basic Editor
(VBE), which includes a window for writing code,
debugging code, and code module organization
environment.
DEVELOPING MACROS IN MS EXCEL
VBA (cont.)
Running VBA in Excel
To access VBA in Excel, go to:
Developer Tab Click Visual BASIC Button.
DEVELOPING MACROS IN MS EXCEL
VBA (cont.)
The VBA IDE

1. Project Window
2. Code Window
Displays the linked Excel Objects and VBA Objects
Allows viewing and editing of codes of a
Allows navigation among various components of
particular selected VBA project component
the VBA project

Use to adjust properties of the


selected objects in the VBA Project.
3. Properties Window
DEVELOPING MACROS IN MS EXCEL
VBA (cont.)
Writing Macros
1. To create a macro in VBA, go to
the project explorer window
and add a new module. Note
that all recorded macros are
available under a workbooks
particular project tree.
2. Select the module and start
programming. Start each macro
as a new sub procedure in a
module or you may choose to
write on separate module.
DEVELOPING MACROS IN MS EXCEL
Running Macros
To run a macro, you may choose from the following
options:
1. You can run a macro by choosing it from a list in
the Macro dialog box.
2. To make a macro run whenever you click a
particular button or press a particular key
combination, you can assign the macro to a
toolbar button, a keyboard shortcut, or a graphic
object on a worksheetSelect the module and start
programming.
DEVELOPING MACROS IN MS EXCEL
Programming Tips:
1. If you are planning to link Excel with a particular
project that was written in VB (the full
programming language) and are unaware of the
syntax to be used (such as chart type, object
properties and keywords), you may record a
macro first and study the codes in VBA, and then
write the particular code in VB.
2. To save a macro-enabled workbook in Excel,
choose *.xlsm format. Otherwise, the workbook will
not contain the macro.
DEVELOPING MACROS IN MS EXCEL
Programming Tips (cont.):
3. After you record a macro, you can view the macro
code with the Visual Basic for Application to
correct errors or change what the macro does. For
example, if you wanted the text-wrapping macro
to also make the text bold, you could record
another macro to make a cell bold and then copy
the instructions from that macro to the text-
wrapping macro.
SUMMARY

Spreadsheet programs have evolved


from simple organized calculation
software into a powerful software
computing package.
Thru the COM framework of MS, VB are
able to access and use Excel objects and
thus, extending the capability of VB.
Thru VBA, macros can be created and ran
within the Excel environment, extending
the features of the already-powerful
spreadsheet program.
Thank you very much!
Next Chapter:
WORKING WITH MATHCAD
MathCAD Overview
Writing MathCAD Documents

Potrebbero piacerti anche