Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 210,034 times

Contents

Related Categories

VBA in Excel - Selection

Selection

One of the first things you will want to know how to do is select cells and apply formatting/text to them. Fortunately, this is very simple.

If you want to select a cell on a spreadsheet, you can use the Range object. This object is very useful. It 'points' to whatever cell or cells you specify, and then lets you perform an action (ie selecting it, or applying formatting). You pass the same cell references as you do in the rest Excel (ie C3). So, to select B4, you use this code:

Range("B4").Select

If you want to select more than one cell, you pass a range of cells. To do this, you specify the top left hand corner cell, and the bottom right hand corner cell, with a : in-between. For example, to select B6 to D8 (ie from B6, 3 cells down, 3 cells across), you simply use this code:

Range("B6:D8").Select

Obviously, if you are using Loops to add or select lots of cells, you won't want to have to generate the column letters. Instead, Excel also provides a Cells object, which instead of accepting "B6", it accepts 6,2, ie Row,ColumnNumber. So,

Cells(4,2).Select

does the same as

Range("B4").Select

Once you have selected cells, you will probably want to refer to them, and to do this, you use the ActiveCell (to refer to one cell... in a selection, it is the cell that is white), or Selection to refer to a group of cells.

Single cell selection

Multiple selection

So,

Msgbox ActiveCell.Value

displays a message box containing the value of the active cell, and

Selection.Font.Bold = True

makes all the text in the selection bold. More on formatting in the next section...

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.