Week 6
Week 6
Week 6
We can use a macro to help automate the process of generating timesheets for us. And what we're
going to focus on is looking at how we can record, run, and edit macros.
The first thing we need to do is show our Developer tab in the ribbon. So, we're going to come to the
File tab, and we're going to come down to Options. Select Customize Ribbon in the left-hand panel and
then, on the right-hand side, you'll see all the ribbon tabs. Most of them are selected by default, but the
Developer tab is not. So, we're going to just make sure that is ticked and then say, OK. You should now
see the Developer tab in the ribbon. And if you click on it, the first group is called the Code group, and
that has all the tools we're going to need to work with macros. We're not ready to start recording our
macro. The way it works when we record our macro is, we hit the Record button, we give it a name and
specify where we want to store it. We then go through all the steps that we want Excel to remember us
doing, and then press Stop. Excel will record each step in a language called VBA or Visual Basic for
Applications, and we can then play those steps back as often as we like. Let's see how it's done.
So, we're going to click the Record Macro button, and we're going to give it a sensible name. This can be
anything, provided it doesn't include spaces or unusual punctuation, so, underscores, numbers, and
letters. We're also going to give it a shortcut key, although this is not required. Now, Control A through Z
are pretty much accounted for, so hold your Shift key down, and then select an appropriate letter.
Last thing I need to do before recording is decide where to store the macro. Put simply, you have two
options. You can either store it in this workbook, which means the macro will travel with the workbook.
Alternatively, if you want to keep a macro that you're going to use with lots of workbooks and it's not
going to travel with any one of them particularly, you can store it in your personal macro workbook. This
will be stored in your Excel startup folder and will open up but remain hidden every time you work with
Excel. So, any macros stored there will always be available to you on your computer but will not be
available to anybody else. Now, from here on, everything I do is going to be recorded. So, I want to try
and not make any mistakes if possible. First thing I need to do is to create a new worksheet. So, I'm
going to click on the plus at the bottom, and then I'm going to give this worksheet a heading and then
click Enter. Give proper heading to the worksheet. Create the desired tables and formulas inside the
macro run workbook. Now to save it, go to the Developer Tab and Click on Stop Macro. It is a very
important step and then press on save as and save the file as xlsm format.
Running a Macro
When you reopen a MACRO file, you will notice a yellow bar with a warning that macros have been
disabled. This an Excel security feature and a very important one. Macros contain code which has the
potential to be harmful to a computer. So if you download a workbook from the Internet, or you're sent
a workbook from an external source, if it contains macros, you probably want to disable them.
If we don't want to have to turn this on every time we want to work with this workbook then what we're
going to do first is look at how we change our security settings. So I'm going to start by clicking Enable
Content, so I'll be able to run the macros. But then I'm going to come to my Developer tab and in the
code group you'll see an option Macro Security. I'm going to click on that. This will take us straight to our
macro security settings which you will see are set to Disable all macros with notification. That is perfect
and you don't want to change it. But what we do want to do, is create a trusted location. In other words,
by default, you will not be able to run a macro without clicking on that yellow bar. But you can specify a
location where the macros will automatically be enabled. And then you only put trusted workbooks in
that location, so we're going to do that. We're going to click Trusted Locations and then we're going to
come down to the button Add new location and click on that. We're then going to click the Browse to
locate that folder. And once you've selected your folder, click OK. And then before you click away, if
there are some folders that you also need to be trusted make sure you tick that so you can trust the
entire directory. And then click OK.
We are now ready to run the MACRO and there's several ways you can do this. We're going to start by
coming to our Developer tab and coming to the big macros button and clicking on that. That will show
you all macros that are currently available. It may be in this worksheet you currently have open, or in
other workbooks you have open. Click on the MACRO sheet and press Run. The macro will be added.
You can also use your created shortcut.
Maybe for our average user who's not that familiar with macros and doesn't have a Developer tab, and
maybe they don't know the shortcut, a button would be an even easier way. Well, that's what we're
going to do next. So this time I'm going to click back onto my Sheet1 and I'm going to add a button, and
I'm just going to put it over there. Now there are a few ways you can do this as well. In your controls
group on your Developer tab, if you come to the Insert, there's a Form Control, which is a button, this
would work very nicely, but you can't edit these buttons. So we're going to go for more graphical
approach, we're going to go to our Insert tab on our ribbon and we're going to find the Shapes. And I'm
going to click on the Shapes and you can really choose any shape you like and place it in Sheet 1. What
you need to do is now right click on the shape and select Assign Macros. Assign the desired Macro and
the next time you click over the shape, you will find a hand symbol suggesting that there will be some
action, once you click on to that shape which has now taken the form of a button.
Edit MACRO
For simply editing headings, we should simply tweak the MACRO. We also will find it hard to record the
step of popping up a message asking the user to enter the date, because there is no facility to do that in
Excel. So again, there's an example where we will need to edit the macro rather than simply record it.
To edit a macro, we're going to come back to our Developer tab, and again, we're going to click on that
big Macro's button. And when we do, we're going to make sure we've got desired worksheet selected,
but this time we're going to click Edit. When you edit a macro, it will open up the VBA editor.
The important things that you can see though should be the project explorer, which allows you to look
at different macros in different open workbooks, and the code window on the right. When you record a
macro in Excel, it will automatically create a module. And all the recorded macros are stored in modules.
Usually there's one, but there may be two or even more.
Right at the top though, you will notice that some of the code is green and prefixed with an apostrophe.
These are what we call comments. Comments are entirely ignored by the compiler, but they're very
useful for documenting our code. So I could put in a comment here with my name and the day I wrote
the code or the day I changed the macro. And I could write absolutely anything as it would be ignored by
the compiler. If there is a line that is in error but you're nervous about deleting it, you can also simply
put an apostrophe in front of that line, and that will comment out the line.
Now, coming down into our code where it actually begins, the first line is Sheets.Add After:=ActiveSheet.
Now, at first glance, that can be a little daunting. But actually the code is quite easy to understand, and
all this is saying is add a new worksheet after the sheet I'm currently clicked on. Let's have a look at the
third line. Range("A2").Select, again, strangely worded, but all that's saying is select cell A2.
Now the thing we want to change first is the size of the font. And if you scroll down a little, you'll see
With Selection. Font, size = 16. So that's where we specified, we wanted our size to be size = 16. Well,
we can just change that now to size 20. Now what I've done here is I have changed the code. This will
have no impact on my existing worksheets. You will only see this change take effect when we actually
run the code again.
We actually want to ask the user to enter the week's commencing date. So I'm going to come up to the
line Week Commencing. Just click in the code at the end and press Enter, so I can get a new line.
Remember, each new instruction must start on a new line.
Then I'm going to type range ("b2 because I want to change the value in b2, ").value, because I'm
changing the value rather than the font, or the border, or the color, and then a space is equal to, and
here comes the interesting bit. We're going to use an input box. So I'm just going to type inputbox and
open my bracket. There are two arguments I need to give input box. The first is a prompt. That's what
the instruction I'm going to give the user. So I'm going to open my quotes, and I'm going to say, Please
enter week commencing date, and close my quotes. Then type a comma, and this is the title of the box
which is what's going to be at the top and that's going to be, New Timesheet Date, close quotes, close
your brackets. And then if you want to just click away, you have now put in your second change.
Before we can now test this, we're going to need to save. You will note a Save icon at the top here. You
can click that Save or you can go back into your workbook and press Save. Both will save the entire
workbook complete with the macro. So click whichever one you prefer. And now a nice little shortcut to
get back to our Excel workbook, we're going to press Alt+F11. And that shortcut toggles you between
the code window and the worksheet. And now to test our macro. So we're going to click back on to
Sheet1 and I'm going to click the New Timesheet button.
You'll see it's run the first two lines of code, and now it's asking for us to type in the week commencing
date.
Suppose we have been asked to change the short cut key to open the MACRO. We're going to come to
our Developer tab, and we're going to come to our Macros button. We can use this dialog to run and
edit macros. The “Step Into” allows us to run the macro, but one step at a time. This is ideal if you're
trying to solve problems with your macro. The Delete button, we'll look at very shortly, that allows us to
delete a macro. And finally, the Options button allows us to change the shortcut key. So we're going to
click the Options button, and instead of Ctrl+Shift+T, we want Ctrl+Shift+N. So hold the Shift button,
press N, and then say OK. And we've now changed our shortcut key, so we can close that dialog.
Suppose when we put the data in date column, the column wasn't actually wide enough. And a lot of
users don't know how to widen the column. So we need to update our macro to do this automatically.
The only problem is we don’t know the VBA code to widen the column. He could look it up on the
Internet, but an even easier way is to record himself widening the column, and then just copy, paste
that code into his original macro.
So let's start by making our column a little too narrow again, so that we can record ourselves widening
it. We're going to come back up to our Record Macro. We're going to call it temp because it's just a
temporary macro, and we're going to click OK. We're now going to come up between the B and the C
and double-click on that line to auto-widen our column. And then we're going to click Stop Recording.
Let's go and have a look at that new macro. So back to our Macros button, make sure you select temp
and click Edit. If you have a look in the Project Explorer, you will notice Excel has added a new module,
so our new macro is in Module2. The new macro only really contains one line of code because we only
performed one operation.
What we want to do now, is copy this line of code into our original macro, and we can do that just by
copying and pasting. So we're going to select the code, and then right-click, Copy, or Ctrl+C, and come
back to Module1, which contains our create time sheet module. And to open it, we're going to need to
double-click, and there is our original macro. We're going to scroll right to the bottom, but remember
everything must go before the End Sub. So click at the end of the second last line and click Enter. And
then paste, Ctrl + V, or right-click > paste. So we've now taken that new line of code and popped it into
our original macro, let's test that that works. So don't forget, Alt+F11, come back to Sheet1 and click
New Timesheet. Adding the date, you will notice that once again, it's created the timesheet beautifully,
but this time it has widened the column as well. So that change is working perfectly.
We no longer need that temp macro, however. So the next step we need to perform is to delete it.
We're going to come up to our Macros button. We're going to make sure we've selected temp, and then
we're going to press the Delete button. And this will delete all the code associated with that macro. So
click Delete, click Yes, and that macro's been removed.
Finally, if I wanted to copy my new timesheet macro into a different workbook, that is also very easy to
do. The first thing you need to do is open the workbook you want to copy it to. And to make our lives
easy, we're just going to create a brand new workbook. So Ctrl+N to get your new workbook up, and
then ALT+F11 to get back into your code. If you have a look in your Project Explorer window, here is our
workbook we've been working with. And just above it, you'll see our brand new Book1. We want to take
Module1 and copy it to Book1. To do that, all you need to do is click and drag the module, and drop it
onto the book where you want to copy it to. And you'll see a little Modules folder has appeared below
Book1. And if you click on the + to expand it, there's our Module1. So we now have the macro in both
workbooks, our original one and our new one.
Relative reference macros record an offset from the active cell. Such macros will be useful if you have to
repeat the steps at various places in the worksheet. With a relative reference macro, if I were in D9 and I
clicked onto A9, it would record me clicking three cells to the left, rather than clicking into A9,
specifically. And this makes relative reference macros far more versatile.