Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Introduction

 to  VBA  in  Excel  


Week  1  
Hello  Students  and  welcome  to  the  VBA  programming  module  of  the  course.    

Study  items  for  this  week  


These  are  the  topics  you  should  familiarize  yourself  for  this  week:    
1. Introduction  to  VBA  –  What  is  it  and  why  do  we  need  it?    
2. Getting  started  with  VBA  –  Access  the  VBA  editor  
3. Record  a  Macro  
4. Recording  a  Macro  for  Automation  of  ‘Repetitive’  Tasks  
5. Running  a  Macro  in  another  Workbook  
6. Recap    
For  each  of  these  points  we  have  mentioned  some  text,  links  to  videos,  links  to  articles,  book  
chapters   etc.   that   you   can   use   to   understand   and   practice   with.   You   are   welcome   to   look   at  
other  sources  too  for  better  understanding.  You  are  also  encouraged  to  discuss  these  with  your  
group  members  and  learn  from  each  other.    
By   the   time   we   get   to   the   lab   for   this   week   you   should   develop   a   basic   understanding   of   the  
items  listed  above.    
 

   
1.  Introduction  to  VBA  –  What  is  it  and  why  do  we  need  it?  
1.1  What  is  VBA?  
• VBA  stands  for  Visual  Basic  for  Applications.  
• Applications  in  the  word  VBA  refers  to  the  different  Microsoft  programs  such  as  Excel,  
Word,  PowerPoint.  We  will  focus  on  Excel.  
• VBA   is   a   tool   for   programming,   editing   and   running   application   code   of   the   host  
application  (excel).  We  will  run  its  code  within  the  host  application.    
• We  can  work  on  VBA  using  the  VBA  editor  that  we  can  open  from  the  ‘Developer’  tab  in  
excel  by  clicking  on  first  item  in  the  ribbon  called  ‘VBA’  or  can  press  Alt+F11.    

1.2  Why  (computer)  programming?  


• Useful  in  developing  problem  solving  skills  

1.3  Why  VBA?  


• Useful  “add-­‐on”  to  basic  model  development  skills  on  top  of  Excel:  You  have  seen  how  
powerful   Excel   can   be   to   carryout   different   tasks.   With   VBA,   you   can   do   many   more  
customizable  things  for  the  different  problems  you  may  be  working  on.    
• Automate   repetitive   tasks:   If   you   have   to   repeat   the   same   task   again   and   again,   like  
adding  the  same  headings  to  multiple  sheets  or  to  multiple  cells,  instead  of  you  typing  it  
over  and  over  again,  you  can  just  ‘record  it  as  a  macro’  or  ‘write  a  macro’  to  do  the  task  
in  fewer  iterations.    
• Add/build   additional   capabilities   into   your   model:   It   gives   you   the   ability   to   create  
custom   design   functions   in   excel.   You   have   already   used   built-­‐in   functions   such   as   SUM,  
COUNTIF,   VLOOKUP   etc   do   perform   different   actions.   VBA   and   Macros   give   you   the  
ability  to  add  more  functions  to  Excel  to  work  with.  e.g  SUM  is  already  programmed  in  
excel.  If  you  wanted  to  add  say  a  ‘add  my  formatting’  function,  with  a  formatting  that  
you  have  defined  that  is  say  needed  by  LUMS  every  time  you  submit  your  excel,  instead  
of   you   having   to   type   it   again   and   again,   you   can   add   it   to   excel   as   a   function.   You   can’t  
add  it  directly  in  excel  but  VBA  lets  you  do  that.  You  will  create  the  function  using  VBA  
and  it  will  become  accessible  in  your  excel  files  for  future  use.    
• Facilitate  integration  across  Office  platforms:  You  can  also  create  functions  etc  that  can  
link  your  word  file  with  your  excel  file  or  with  your  PowerPoint  etc  if  you  learn  how  to  
program  in  VBA,  giving  you  even  more  power  in  how  to  use  these  tools  for  work.    

1.4  Object  Oriented  Programming  


• Programming  paradigm  that  builds  programming  logic  by  conceptualizing  objects  (with  
properties  and  methods)  interacting  with  each  other    
• Everything  in  Excel  is  represented  and  defined  as  an  object.  E.g.  Workbook,  worksheet,  
cell,  are  all  objects.  
• Each   object   has   certain   ‘properties’   e.g.   A   cell   has   different   properties   such   as   for  
example  you  can  change  its  color,  font,  etc.  You  can  add  values  to  it  etc.  
• Each  object  has  certain  ‘methods’  associated  with  it  too.    
 

   
2.  Getting  started  with  VBA  –  Access  the  VBA  editor  
1. Open  Excel  
2. Enable  Developer  tab:  
File  à  Options  à  Customize  Ribbons  à  Developer  

 
 
The   Developer   tab   will   now   be   appearing   in   you   excel.     Click   on   ‘Visual   Basic’   option   in   the  
ribbon  to  open  up  the  VBA  editor.  
 

 
   
 

The  VBA  editor  will  look  similar  to  the  screen  shot  below.  This  has  multiple  windows  within  it  as  
shown  below.    

This   is   just   to   show   you   what   it   looks   like   for   now.   We   will   get   back   to   it   later.   You   can   read  
details   about   the   different   parts   at   this   link:   https://www.thespreadsheetguru.com/vba-­‐
intro/the-­‐visual-­‐basic-­‐editor  
 

   
3.  Record  a  Macro  
The   first   thing   you   will   do   is   to   use   the   Macro   recording   option   without   having   to   write   any  
code.    
Macro  –  is  a  piece  of  code  that  you  can  either  write  yourself  in  VBA  editor  or  in  some  cases  can  
be  create  using  the  Record  Macro  option  in  the  Developer  ribbon.    
See   the   video   on   this   link   :   https://www.youtube.com/watch?v=v4Uo5gMSqAg.   Don’t   worry  
about  the  coding  part  just  yet.  Here  you  are  practicing  it  without  writing  the  code,  where  VBA  
will  automatically  create  it  and  you  can  see  the  code  for  it  in  the  editor.    
Also  check  this  link:  https://www.excel-­‐pratique.com/en/vba/first_macro  
Another  video  you  can  view:    
https://www.youtube.com/watch?v=ltcpaHdXUrU&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU
3&index=5  
Important  when  saving  excel  file  with  Macros  in  them.  Else  you  will  lose  your  macro  info:    
https://www.youtube.com/watch?v=IuzssYXKgzY&list=PLWPirh4EWFpEFSYTbKaST6hSlgIFCJjU3
&index=6  
 

4.  Recording  a  Macro  for  Automation  of  ‘Repetitive’  Tasks  


The   following   video   can   help   you   understand   the   purpose   of   recoding   a   Macro   i.e.   to   automate  
repetitive  tasks:  
https://www.youtube.com/watch?v=MUeL0nHEqkI  

5.  Running  a  Macro  in  another  Workbook  


If   you   have   already   recorded   a   macro,   you   can   reuse   it   in   another   workbook.   In   order   to  
understand  this,  please  see  the  following  link:  
https://www.youtube.com/watch?v=RT_MiyPYNGo      
It’s  important  to  understand  that  when  you  are  working  with  multiple  workbooks,  you  need  to  
open  the  workbook  in  order  to  use  its  stored  macros.  

   
6.  Recap  
An  ebook  by  Mike  McGrath  with  a  title  ‘Excel  VBA  in  easy  steps’  (2nd  Edition)  has  been  uploaded  
on   LMS   along   with   ‘Week   1   Guide’.   To   recap   the   topic   ‘recording   a   Macro’   please   read   the   first  
two  topics  of  the  book.  These  two  topics  are:  
1. Introducing  Excel  VBA  
2. Recording  a  macro  
If  you  need  further  practice,  you  can  follow  the  video  on  the  following  link:  
https://www.youtube.com/watch?v=G05TrN7nt6k  
This  is  a  great  video  and  the  person  explains  things  well  but  its  about  2hours  long.  However,  for  
now  you  can  watch  the  initial  28  minutes  of  it.  That  initial  part  is  where  she  introduces  you  to  
VBA  and  also  how  record  a  macro.  In  the  later  part  she  also  talks  about  write  Macros  but  you  
can  come  view  that  at  a  later  time.    
 

You might also like