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

Lesson

  n Transcrip
pt  Chandoo.org 

Right Click Con
ntext Me
enu exam
mple 
 

[00:00:001 minutes] 

Hello  evveryone,  welcome  to htttp://vbaclassses.com.   Th his  is  Vijay  Sh


harma.  In  thhis  lesson  we  will  be 
learningg how to add d a right‐clickk context meenu option to o a data tablle. We will bbe doing this by using 
a user fo orm on to w which we will put the Miicrosoft mon nth‐view cale endar controol. We will a also learn 
how to iinstall this caalendar control if it is nott available o on your syste em and how  to register itt. A word 
of  advicce  ‐  this  opttion  is  currently  only  avvailable  for  Excel 
E 32‐bit  versions  an d  you  need  to  have 
administtrator rights on the syste em on whichh you wish to o install the ccalendar conttrol. 

[00:00:553 minutes] 

Before wwe go ahead d and take a look at the ccode and ho ow all this is done, let us  see a quick demo of 


what exactly we aree trying to accomplish heere. Here is aan Excel data table and w we need the ability to 
choose aa right click  context men nu option thhat would allow us to select an optioon to bring u up a pop‐
up calenndar and we could easilyy select a datte from the ccalendar insttead of typinng one over h here. We 
will just  put anotherr record here. I will rightt click on thiis cell and th
his Insert Datte option is  what we 
are talkiing about in  this lesson. How do we  add this opttion into thiss menu? Whhat is the cod de that is 
requiredd and what is the functio onality that tthis would ddo. The functtionality is ppretty simplee. When I 
click on  this button,  a calendar w
will come onn the screen, and when I click on a daate on that ccalendar, 
that date will appear in the cell. Let's see thaat. 

[00:02:004 minutes] 

I am goiing to selectt 21st Novem mber and yoou can see that the mom ment I do thhat, it appears in this 


cell.  Pleease  note  thhat  this  option  is  not  li mited  only  to  a  cell  that  has  a  heeading  that  we  have 
specified d as  a date  heading.  This option is aavailable to  this entire ttable or any  y other table e that we 
would b be having in o our workboo ok. Even if I r ight click on this cell thatt contains thhe heading as name, I 
would sttill get the In nsert Date op ption and if I  click on it by mistake, I can either cl ose this user form or 
I  can  prress  the  escaape  key  to  safely 
s come  out  of  this.  That  is  the  demo  of  w hat  we  are  trying  to 
achieve  in this lesso on to add a rright click coontext menu u option to a a data table.  As I said ea arlier, we 
need to  have an Acttive‐X contro ol available w with us so th hat we can d s control is called the 
do this. This 
Microso oft  month‐vieew  control.  Let's  see  it  in  action.  Let's  bring  up p  the  Visual   Basic  Edito
or.  I  have 
already  designed the form calle ed frmCalenddar. You can n see this callendar contrrol on my form here. 
Anotherr  thing  that  i  have  designed  on  this  form  is  a  co ommand  buttton  which  iis  hidden  be ehind  the 
calendarr  control  because  we  do on't  want  thhe  user  to  cllick  on  this  control. 
c The 
e  functionalitty  of  this 
comman nd button is  only to captture the Esc  key. We do that by speccifying the caancel properrty of the 
button. 

[00:03:554 minutes] 

Let's seee what all wee have done
e here. We hhave put in a calendar co
ontrol which  is here and we have 
put in a command b button. Do we have any ccode? The co dMe, i.e. pleease remove this user 
ode is Unload

  Page 1 off 4  
Lesson
  n Transcrip
pt  Chandoo.org 

form  froom  the  screeen.  When  we w press  Escc  from  the  keyboard,  th he  way  this  event  triggers  is  by 
specifyinng the Canceel property o of this commmand button n. If we bring g up the prooperties box,, you can 
see thatt the Cancel  property forr this commaand button h has been spe ecified as truue which me eans that 
whenever we press  the Esc key,, the code thhat we have  written in C ClickEvent wiill be triggered. Right 
now, thee only line th hat we have is UnloadM e and so the e form gets u unloaded froom the scree en. How I 
get  this  month‐view w  control  is  pretty 
p simplee.  I  can  clickk  on  this  buttton  called  ttoolbox.  First  of  all,  I 
need  to  bring  up  th
he  toolbox  control  bar  oon  the  scree en.  So,  we  ca an  go  to  Vieew  and  then n  click  on 
toolbox  or we can click on the toolbox buttoon here. Now w, once the toolbox is onn the screen n, we can 
then either go to thee Tools menu and click oon Additional Controls or we can righht click on the e toolbar 
and  chooose  Additional  Controlss.  They  both   will  bring  up  u the  additional  controols  dialog  bo ox  on  the 
screen u using which wwe can select any of the  options thatt are availablle on our sysstem to use. 

[00:05:444 minutes] 

If we scrroll down in this list, we can see thatt we have the e Microsoft D Date‐Time ppicker control and the 


Forms 22.0 library and if we scrolll down furthher, you can see that we e have the MMicrosoft month‐view 
control 66.0. This is p
pointing to th his file calledd MS Commo on Control 2.ocx. Again,  if you do not see the 
Microso oft month‐vieew control in n this Additioonal Controlls list, there are two posssibilities ‐ yo ou either 
don't haave this control installed on your systtem or you aare running E Excel 64‐bit eedition and ssince this 
Active‐XX is only a 32‐bit version, you will nott see it in you ur Excel 64‐bbit. So, pleasee make sure that you 
are runn ning this lessson only on  a 32‐bit verrsion of Exce el. If you don n't have this  common co ontrol on 
your sysstem, you will need to do ownload it. O Once you have download ded the cabi net file that contains 
this  called  MS  Com  CD  2.cab  ‐  it  is  basicallly  a  zip  file  with  the  ca
ab  extensionn  which  stan
nds  for  a 
cabinet  file. It contaains these twwo files ‐ thee INF file and d the OCX fille. We are innterested in  the OCX 
file. We  need to cop nto the relevvant directorries depending on the opperating system that 
py this file in
you are using. Right now, I am w working on a  Windows 8..1 Operating System andd so I need to o put this 
file into  the folder ccalled c:\windows\Syswoow64\mscom mct2.ocx. On nce I have coopied this file, I need 
to run  the command prompt ve ersion  of thee administraator version  where I  cann register an  Active‐X 
control.  Again, theree are two ways using whhich you would be gettin ng the comm mand promp pt. One is 
when yo ou click on thhe simple command prom mpt and the e other is the e admin com mmand prompt. Make 
sure thaat you choosse the admin n command  prompt. If yyou are  working on  Winndows 7 or W Windows 
Vista, yoou can right  click on the command pprompt optio on and then choose Runn as Adminisstrator. If 
you  are  running  Wiindows  8  or  Windows  88.1,  when  yo ou  press  thee  WindowsX  key  to  bring  up  the 
shortcutt menu, you will see two options the re ‐ comman nd prompt an nd commandd prompt admin. 

[00:09:001 minutes] 

Right noow, I will sho ow you comm mand promppt admin on  my screen. Once you hitt yes on the warning 


messagee, this is whaat you get on n the screen . How do you come to know that thiis is an admiinistrator 
comman nd prompt?  You can quickly look at  the title. It cclearly says a administratoor command prompt. 
Now, I ccan use RegSSvr32 to regiister the conntrol. I just p pressed Enter to show yoou that when we are 
using this utility, wee need to pro ovide variouus switches h here or we can also just  provide the  name of 
the  dlll  file  orr  the  occx  file  thhat  we  wish  w to  register.  IIn  our  case,  c it 
was c:\wwindows\Sysswow64\msccomct2.ocx.  Now when II press Enterr, I will get aa message bo ox saying 
that  thee  registration
n  for  this  occx  was  succeessful.  Once
e  we  have  done  this,  wee  can  now  close 
c the 

  Page 2 off 4  
Lesson
  n Transcrip
pt  Chandoo.org 

comman nd prompt w window and tthis option oor Active‐X control will now be availaable to us to


o be used 
in our usser form. 

[00:10:337 minutes] 

Once  wee  have  donee  that,  you  can 


c right  clicck  on  the  to ool  box  and  get  the  monnth‐view  con ntrol  and 
place  it  on  your  useer  form.  Wee  talked  abo ut  the  code  for  the  Closse  button.  W We  also  need  to  talk 
about th he code for  the calendarr control, i.ee. a date is returned oncce we click. IIf we double e click on 
the conttrol, it takes  us to the Da ateClick evennt of the mo onth view control where  we have wrritten the 
code. Inn case you have just one e cell selecteed, it will givve you the d date that youu have clicke ed on. In 
case  you  have  multtiple  cells  se elected,  all  tthose  cells  will  be  filled
d  with  the  ddate  that  yoou  chose 
becausee of this simp ple For Each loop. This iss the code that allows us to enter thhe date into the cell. 
And, wee are also ch hecking ‐ eve ery time this  user form is loaded in tthe memoryy and then shown on 
the screen ‐ if you riight clicked o on a cell thatt already con ntained a datte. If it contaained a date then we 
are settiing that datee as the activve date on thhe user form m. If there is n no date and  it is a blank cell then 
we are jjust showingg today's date.  This is th e code and tthe design o of the user foorm that allo ows us to 
get  the  user  form  on 
o the  scree en  and  allowws  us  to  remmove  it  from m  the  screenn  and  it  allows  us  to 
choose aa date and p put it in the  cell on whicch we right cclick. Let's co orrect the deesign form again. We 
just wan nt it to be a ccompact one e. We will jusst resize it annd we are done!  

[00:12:445 minutes] 

In Moduule 1, we have written a a very simplee sub routine associated d with bringi ng this user  form on 


the screen. If you go
o there, it is just a simple  sub routine called Open
nCalendar(). A de is: 
And, the cod
frmCCalendar.Show 

This is to o bring up the user form m on the screeen. So, whe ere exactly is the code tthat allows u us to add 


the option to the rigght click menu. Well, th at code is in n ThisWorkbook. If we ddouble click o on it, we 
will see two procedu ures listed he ere that allow w us to add tthe right clicck context meenu option. We have 
written  the  code  to o  add  the  rigght  click  con text  menu  option 
o t WorkboookOpen  event  which 
into  the 
means tthat every tim me we open this workboook, the code e will run, annd will add t he InsertDatte option 
to the right click menu for a data a table only.  How do we do that? Lett's study this  code. 
Priva ate Sub Workkbook_Open(() 
    On n Error Resum me Next 
    Dim m NewContrrol As Comma andBarContrrol 
    Ap pplication.On nKey "+^{C}", "Module1.O OpenCalenda ar" 
    Ap pplication.Com mmandBars("List Range  Popup").Con ntrols("Insert Date").Deleete 
    Sett NewContro ol = Application.CommanndBars("List R Range Popup p").Controls.A
Add(Before:= =1) 
    Wiith NewConttrol 
        ..Caption = "Insert Date" 
        ..OnAction = "Module1.Op penCalendarr" 
        ..BeginGroup = True 
    End With 
End SSub 

We startt with On Error Resume Next so thatt if there is an error, it go
oes to the neext line. Then
n, we are 
using th
he Command dBarControl o
object and wwe are creatting a new variable calleed NewContrrol. Next, 

  Page 3 off 4  
Lesson
  n Transcrip
pt  Chandoo.org 

we  are  assigning  the  Ctrl‐Shift  and 


a C  key  shhortcut  using  these  charracters  ‐‐  "+ ^{C}  ‐‐  to  thhe  option 
that we  want to seee. We can briing up the InnsertDate callendar control on the scrreen also by pressing 
by  Ctrl+Shift+C  from m  the  keyboa ard.  Whenevver  the  userr  presses  the e Ctrl+Shift+CC  key,  the  code 
c that 
needs to o run is in module 1 and it is called O OpenCalendaar(). Then, we are addingg the right cliick menu 
option tto the data ttable and this is specifiedd by this nam me ‐ a data ttable is calleed List Range e and the 
right clicck menu that we are add ding is the Lisst Range Pop pup. In case it already exxists, we are deleting 
it.  Then,,  we  are  adding  it  again
n  and  we  arre  specifyingg  the  position  where  wee  want  to  ad dd  it,  i.e. 
before n number 1. An nd, we are a adding a capttion and spe ecifying that the OnActioon, i.e. when the user 
clicks onn this option, we need to o run the OppenCalendar(() sub‐routin ne from moddule 1. Once we have 
done this, wheneverr we right cliick on the daata table and d choose InsertDate, we  will get the calendar 
on screeen. 

[00:16:331 minutes] 

Similarlyy, we need tto make sure e that when  we are clossing our workbook, this rright click In nsertDate 


option  iis  also  remo
oved  from  the  ntext  menu  and  that  is  done  in 
t List  Ran ge  or  a  datta  table  con
the Worrkbook_Befo oreClose even
nt with this ccode: 
Privaate Sub Workkbook_BeforeeClose(Canceel As Boolean) 
    On
n Error Resum me Next 
    Ap
pplication.On nKey "+^{C}" 
    Ap
pplication.Com mmandBars("List Range  Popup").Con ntrols("Insert Date").Deleete 
End SSub 

We  are  specifying  that  the  Ctrl+Shift+C  keyy  should  nott  run  anythin ng  and  we  aare  also  dele
eting  the 
InsertDaate option th hat we have  added. Withh all these liines of code, we are now w able to addd a right 
click meenu option to Date and wheenever we choose date, it will appeaar in the cell. If I right 
o this InsertD
click  in  any  other  reegion  which  is  not  a  parrt  of  the  datta  table,  I  do  not  see  thhe  InsertDatte  option 
becausee  this  contexxt  menu  is  applicable 
a too  a  cell  and  when  we  click  on  a  datta  table,  thee  context 
menu is applicable to the List Range pop‐up.. 

[00:17:447 minutes]  

I  have  aalso included d a  link  whicch  will  show


w  you  the  vaarious  options  that  are  available  fo or  you  to 
understaand  if  you  want 
w to  integrate  the  rigght  click  me ape  or  any  oother  things  that  are 
enu  to  a  sha
availablee,  i.e.  the  naames  and  va arious  thingss  that  you  need  to  undeerstand  and  use.  I  hope  you  will 
enjoy thhis lesson an nd be able to o add the rigght click men nu option into your dataa tables or a any other 
objects tthat you aree using in you ur Excel workkbooks. Pleaase do let me e know if youu have any q questions 
by  addin ng  them  to  the  comments  area  andd  I  will  be  happy 
h to  ansswer  them.  This  is  Vijayy  Sharma 
signing o off. Thank yo ou so much. 

  Page 4 off 4  

You might also like