Excel - Basic Training in Names Functions Macro's: Sjabbo Van Timmeren Gerard de Weerd

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Excel – basic training in

Names
Functions
Macro’s

Sjabbo van Timmeren

Gerard de Weerd

Copyright of NAM June 2010


 Goal

 Able to make use of names


 Able to create own functions
 Able to use macro recorder
 Able to edit macro / replace parts

Copyright of NAM Excel VBA introduction 2010 June 2010


Programme
 Navigate, shortcuts in Excel
 Names

 Functions

 Relative vs absolute in macro


 Macro recorder
 Visual Basic Editor
 (Digital Signature)

Copyright of NAM Excel VBA introduction 2010 June 2010


 Navigate in Excel using your keyboard

Find “navigate”
in Help

Copyright of NAM Excel VBA introduction 2010 June 2010


 Shortcuts

 Ctrl Enter fill select area (same value)


 Ctrl Home first cell (can be different from A1 when using
Freeze panes)
 Ctrl End last cell from ‘filled area’
 End go to last filled cell in column (before
empty)
 Ctrl-Shift End select total filled area – last cell
 Ctrl * select filled region

Copyright of NAM Excel VBA introduction 2010 June 2010


 Code
 Ctrl *
Selection.CurrentRegion.Select

 Ctrl-Shift End
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Add comment by using single quote


‘ Select from current cell to last cell

Copyright of NAM Excel VBA introduction 2010 June 2010 6


NAMES & FUNCTIONS

Sjabbo

Copyright of NAM Excel VBA introduction 2010 June 2010 7


 Macro Recorder

 Macro in Workbook
 Can only be used (in other workbooks) when workbook is open

 Macro in PERSONAL.xls
 Saved in XLSTART
 Is automatically opened as hidden
 Macro’s always available

Copyright of NAM Excel VBA introduction 2010 June 2010 8


 Record File Save As:

ActiveWorkbook.SaveAs Filename:= _
"\\amsdc1vfc008\Gerard.DeWeerd$\cached\my documents\Input.xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Copyright of NAM Excel VBA introduction 2010 June 2010


 Replace filename by variable

Dim MyName As String

MyName = “Input.xls"

ActiveWorkbook.SaveAs Filename:= _
MyName, _
FileFormat:= xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False , CreateBackup:=False

Copyright of NAM Excel VBA introduction 2010 June 2010


Dim MyName As String

MyName = InputBox("Enter Filename", "This is my box",


"TL_datasheet")

ActiveWorkbook.SaveAs Filename:= _
MyName, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

MsgBox ("File saved as: " & MyName)

Copyright of NAM Excel VBA introduction 2010 June 2010


Another way to save a file by asking input is:
ActiveSheet.Copy ' creates a one sheet workbook
fileSaveName = Application.GetSaveAsFilename("TL_" & ActiveSheet.Name, _
fileFilter:="Comma delimited (*.csv), *.csv")
If fileSaveName <> False Then ‘ fileSaveName is a variable
ActiveSheet.SaveAs Filename:= _
fileSaveName, FileFormat:=xlCSV _
, CreateBackup:=False ‘ means if file exists, it will be overwritten automatically
MsgBox "Saved as " & fileSaveName
End If
Application.GetSaveAsFilename will prompt for a filename and give you the option to change the pathname (ie directory)
ActiveSheet.Name is name of sheet at the bottom in tab; so if sheet is called Perfs your filename in this case will be
TL_Perfs; filter is set on Comma delimited, so other csv-files in that directory will be displayed;
fileSaveName is here a variable; file format is here set to CSV instead of normal XLS.

Copyright of NAM Excel VBA introduction 2010 June 2010 12


 Exercise
 Open editor (Macro – Edit or Alt - F11)
 Type:
Sub AskInput()
Dim MyName as String
MyName = inputbox(
MsgBox(

Copyright of NAM Excel VBA introduction 2010 June 2010 13


 Exercises

 Empty cells:
=COUNTIF(range,””)

Percentage
=1-(‘empty cells’ / ’nr of rows’)

Copyright of NAM Excel VBA introduction 2010 June 2010 14


 Tip
 To find VB codes you can make use of Google

MsgBox “The active cell row is “& ActiveCell.Row

Copyright of NAM Excel VBA introduction 2010 June 2010


 How to make it fit for more than one sheet

Range("E5").Select
Selection.Copy
Range("E6:E24").Select
ActiveSheet.Paste

 Make use of variable  find number of rows

NrOfRows = ActiveSheet.UsedRange.Rows.Count

Range("E6:E“ & NrOfRows).Select


Copyright of NAM Excel VBA introduction 2010 June 2010 16
BACKUP SLIDES

Copyright of NAM Excel VBA introduction 2010 June 2010 17


 Digital Signiture
In case you do not want to change the security level, every time you
open a workbook which contains a macro, Excel will ask you whether
or not you want to enable the macro. In other words “do you trust the
person have created the macro”. To avoid this you can add a digital
signature to the workbook.
How to create and add a signiture is explained in next few slides.

Copyright of NAM Excel VBA introduction 2010 June 2010 18


 Add/create Certificate
 Click Start
 In Start Search type: Cert
 Select Digital Certificate for VBA Projects
 Type a (your) name
at bottom of window
and click OK
 Certificate has been
added

Copyright of NAM Excel VBA introduction 2010 June 2010


 Add Digital Signature to workbook
 Open Workbook containing macro’s
 Open VBA editor (Alt-F11)
 Tools – Digital Signatures..
 Choose

 Select created certificate


 Save workbook

Copyright of NAM Excel VBA introduction 2010 June 2010


Open Workbook and change Options

Copyright of NAM Excel VBA introduction 2010 June 2010

You might also like