Simple clear advice in plain English

Hands On: Make your first macro

Automating tasks in Excel is easier than you might think

In any version of Excel you can always open the VBA editor, choose Module on the Insert menu and enter the listing for your macro in the right-hand panel.

A macro is either a subroutine which starts with Sub macro name () and finishes with EndSub or a custom function which begins with Function function name() and ends with End Function.

If a macro includes variables they will be of different types. By default they are called Variants but it is more efficient to classify or ‘dimension’ them at the start. An Integer variable only takes two bytes of memory.

A String variable only takes one byte per character. If you don’t dimension your variables then both types will become Variant variables which take 16 bytes plus one byte for each character, if the variable holds a string. That consumes a lot more memory and can reduce performance.

This particular macro needs two Integer variables for sorting the worksheets and one String variable for the names of the months. So, if you have deleted the notes, after Sub MonthTabs() enter:

Dim x As Integer
Dim y As Integer
Dim mth(12) As String

Now you can enter the names of the months which are to appear on the tabs. Type:

mth(1) = “Jan”
mth(2) = “Feb”
and so on down to:
mth(12) = “Dec”

This macro assumes that you are running it on a new workbook with worksheet tabs called Sheet1, Sheet2, Sheet3, though the number of sheets in the workbook doesn’t matter.

The next section of the macro uses a loop that says, if the first tab name starts with Sheets then change it to a month name, adding sheets if necessary. Enter:

For x = 1 To 12
If x <= Sheets.Count Then
If Left(Sheets(x).Name, 5) = “Sheet” Then
Sheets(x).Name = mth(x)
Else
Sheets.Add.Move after:=Sheets(Sheets.Count)
ActiveSheet.Name = mth(x)
End If
Else
Sheets.Add.Move after:=Sheets(Sheets.Count)
ActiveSheet.Name = mth(x)
End If
Next x

All the macro has to do then is move any other sheets to the end of the series, so enter:
For x = 1 To 12
If Sheets(x).Name <> mth(x) Then
For y = x + 1 To Sheets.Count
If Sheets(y).Name = mth(x) Then
Sheets(y).Move Before:=Sheets(x)
End If
Next y
End If
Next x

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Using Wizard to create a form

Create a good Base for your data

Databases may sound very dreary but they are behind almost everything we do. We explain how to go about starting one of your own using Libre Office Base

Extensions for Open Office Writer

Use Open Office Writer as a free alternative to Microsoft Word

Open Office Writer is a great piece of software that has hundreds of features. Here we show you 10 lesser-known features that you might not have discovered yet

Excel error screenshot

Why do some of my Excel cells show a '#VALUE!' error?

The error message a reader is getting is because he may have typed a space in the cell, which is confusing Excel when doing the spreadsheet calculations

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive