Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 210,030 times

Contents

Related Categories

VBA in Excel - Editing Macros

Editing Macros

As we saw in the last section, you don't actually need to know any VB to create a Macro in Excel! However, even if you know VB, it can be very useful to record macros just so you can get to grips with the commands needed to perform an operation (such as setting the background colour of a cell). This means that if you don't know how to do something, simply record the action, and see what code Excel generates for you.

As VBA is so similar to VB, I am not going to cover standard VB syntax, procedures etc. In fact, all you really need to know is:

1) You can put code in a module. Any public procedures can be called from any sheet in the workbook (ie ExampleMacro)
2) You can put code in a worksheet. Any public procedures can be access from another sheet, so long as you reference it (ie Sheet1.ExampleMacro). Any private procedures can only be accessed from that worksheet
3) Any procedure (Sub) or function is a Macro.

Now you know that, we can take a look at the macro that Excel recorded for us, and see what it does. Then, in the next few sections, I will show you the main commands for selecting, formatting, inserting formulae etc.

So that you can see exactly what I mean, I have simply added comments explaining what each line of code does above it:

'Standard Procedure/Macro called ExampleMacro
Sub ExampleMacro()
    'Select B2
    Range("B2").Select
    'Set the active cells' value to 'Cust ID'
    ActiveCell.FormulaR1C1 = "Cust ID"
    'Select C2
    Range("C2").Select
    'Set the active cells' value to 'Order ID'
    ActiveCell.FormulaR1C1 = "Order ID"
    'you get the idea....
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "ItemID"
    Range("E2").Select
    ActiveCell.FormulaR1C1 = "Quantity"
    Range("F2").Select
    ActiveCell.FormulaR1C1 = "Unit Price"
    Range("G2").Select
    ActiveCell.FormulaR1C1 = "Total"
    'Select cells B2 across to G2
    Range("B2:G2").Select
    'Make the selected cells bold
    Selection.Font.Bold = True
    'Set the background colour of the selected cells
    With Selection.Interior
        .ColorIndex = 15
        .Pattern = xlSolid
    End With
    'Select columns B to G
    Columns("B:G").Select
    'Set the selected columns' column width to 8.9
    Selection.ColumnWidth = 8.9
End Sub

As you can see, you can select a group of cells using the Range object. Excel then calls the ActiveCell object, and sets its Formula/Value to something (ie ActiveCell.FormulaR1C1 = "ItemID"). You could also use ActiveCell.Value = "ItemID".

When there is more than one cell selected, and you want to apply formatting or text to the whole lot, you use the Selection object instead. It is essentially the same as the ActiveCell object, and has the same methods/properties etc, except that it refers to more than one cell!

In this code Excel recorded, it seems to be doing a lot of selecting, and then applying formatting to the current cell. This is how Excel works when recording a Macro, but you can simply refer to it directly, without changing the selection. For example, instead of using

Range("D2").Select
ActiveCell.FormulaR1C1 = "ItemID"

you could simply use

Range("D2").FormulaR1C1 = "ItemID"

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [35] VBA in Excel

    Posted by MartinMudge on 28 Aug 2007

    Hi


    What i am trying to do is use a formula to grap cells a specific distance apart, and then use autofill to copy these cells.  Currently, I am ...

  • Re: [35] VBA in Excel

    Posted by erniede on 10 Aug 2007

    This might not be the place to ask this question, but here goes...


    I'd like to put a macro in an excel worksheet that limits the time someone can use the sheet. that is, I have a "demo" of ...

  • deleting unwanted rows

    Posted by charuambekar on 08 Feb 2006


    You need to first write a code to select the entire range, then


    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete



    Regards,

    Charu

  • .Borders

    Posted by djlysuc on 26 Nov 2005

    I am trying to use VB in excel 2003 to check if a cell has a border. I tried the following but it does not work. Can anyone explain how I should be doing this?

    [code]
    ActiveCell.Borders(xlEdgeLeft...

  • Try something different

    Posted by richardbarnwell on 14 Nov 2005

    Why dont you try using a For loop instead. Something like:

    x = o

    For x to 100

    // What ever you want to do

    Next x

    Makes it very easy.