How do you use selection in VBA?

Home / VBA / How to SELECT ALL the Cells in a Worksheet using a VBA Code

In VBA, there is a property called CELLS that you can use to select all the cells that you have in a worksheet.

Cells.Select
  1. First, type the CELLS property to refer to all the cells in the worksheet.
    How do you use selection in VBA?
  2. After that, enter a (.) dot.
  3. At this point, you’ll have a list of methods and properties.
    How do you use selection in VBA?
  4. From that list select “Select” or type “Select”.
    How do you use selection in VBA?

Once you select the entire worksheet you can change the font, clear contents from it, or do other things.

Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook

Notes

  • The CELLS property works just like the way you use the keyboard shortcut Control + A to select all the cells.
  • When you run this VBA code, it will select all the cells even if the sheet is protected and some of the cells are locked.
  • It will select cells that are hidden as well.

The sheet Must Be Activated

Now you need to understand one thing here when you select all the cells from a sheet that sheet needs to be activated. In short, you can’t select cells from a sheet that is not activated.

Let’s say you want to select all the cells from “Sheet1”. If you use the type below code, you’ll get an error. You need to activate the “Sheet1” first and then use the “Cells” property to select all the cells.

Worksheets("Sheet1").Activate
Cells.Select

Now when you run this it will first activate the “Sheet1” and then select all the cells. This thing gives you a little limitation that you can’t select the entire sheet if that sheet is not activated.

Here’s another thing that you can do: You can add a new sheet and then select all the cells.

Sheets.Add.Name = "mySheet"
Cells.Select

  • How to Set (Get and Change) Cell Value using a VBA Code
  • How to Sort a Range using VBA in Excel
  • How to Create a Named Range using VBA (Static + Dynamic) in Excel
  • How to Merge and Unmerge Cells in Excel using a VBA Code
  • How to Check IF a Cell is Empty using VBA in Excel
  • VBA ClearContents (from a Cell, Range, or Entire Worksheet)
  • Excel VBA Font (Color, Size, Type, and Bold)
  • How to AutoFit (Rows, Column, or the Entire Worksheet) using VBA
  • How to use OFFSET Property with the Range Object or a Cell in VBA
  • VBA Wrap Text (Cell, Range, and Entire Worksheet)
  • How to Copy a Cell\Range to Another Sheet using VBA
  • How to use Range/Cell as a Variable in VBA in Excel
  • How to Find Last Rows, Column, and Cell using VBA in Excel
  • How to use ActiveCell in VBA in Excel
  • How to use Special Cell Method in VBA in Excel
  • How to Apply Borders on a Cell using VBA in Excel
  • How to Refer to the UsedRange using VBA in Excel
  • How to Change Row Height/Column Width using VBA in Excel
  • How to Insert a Row using VBA in Excel
  • How to Insert a Column using VBA in Excel
  • How to Select a Range/Cell using VBA in Excel

Bottom line: Learn the difference between these two commonly used methods in VBA.

Skill level: Intermediate

One question I hear often from new members of our VBA Pro Course is, “What's the difference between Select and Activate? It seems like they do the same thing?”

How do you use selection in VBA?

The short answer is that Select and Activate can perform the same action, but the differences are:

  • Select can be used to select multiple objects (sheets, ranges, shapes, etc.) at the same time.
  • Activate can be used to active one object within the selection.

Let's take a look at an example to see how this works.

Which Method Should I Use?

First, it's important to note that both methods can be used on many different types of objects in Excel. This includes sheets, ranges, cells, shapes, charts, slicers, etc. We'll use worksheets in this example.

The use case for each method depends on the scenario. And as I explain below, it's usually best to use neither. 😲

When to use the Select Method

The Select method is typically the one we're most familiar with because the macro recorder uses it when generating code.

Select allows us to select a single object OR multiple objects.

The following line of VBA code selects a single worksheet.

  Worksheets("Sheet2").Select

We can also use Select to select multiple objects. Here is an example of selecting multiple ranges.

Range("A1:B10,D1:F10,J1:K10").Select

Here is an example of selecting multiple sheets. The Array function is used to reference the sheets. This same technique can be used for shapes.

  Worksheets(Array("Sheet2", "Sheet3", "Sheet5")).Select

When that line of code is run, the first item in the array is activated. That means that Sheet2 will be the active sheet that the user sees.

How do you use selection in VBA?

What if we want to keep the three sheets selected, but have the user view Sheet3 instead? This is where the Activate method comes into play.

When to use the Activate Method

The Activate method allows us to select a single object. This can be a single object within a selection, if multiple objects are already selected.

The following lines would select the three sheets, then make Sheet3 the active sheet that the user sees.

Worksheets(Array("Sheet2", "Sheet3", "Sheet5")).Select

Worksheets("Sheet3").Activate

How do you use selection in VBA?

If you do not have a group of objects selected, then Activate will just select a single object. It does NOT add to the current selection and will work the same way that Select works.

Be Careful with Activate

You might not always know which objects are selected in the workbook. Therefore, you can't always use Activate to select a single object.

For example, let's say you have a line of code in your macro to activate Sheet1.

If the user has Sheet1 to Sheet3 selected before the macro is runs, then all three sheets will remain selected. You might not want this if your next line of code modifies a cell on Sheet1. Depending on how the code is written, all three sheets could be modified, causing unwanted results. 😬

If you activate an object that is NOT in the selection, then that single object will be selected and the previously selected objects will no longer be selected. For example, if the user has Sheet1 to Sheet3 selected and your code activates Sheet4, then only Sheet4 will be selected.

The main takeaway here is that there are a lot of potential unwanted outcomes when using Activate to select a single object. So, it's best to use Select for selecting single objects.

Activate for Workbooks

The Activate method is also used when activating Workbooks and Windows. There is no Select method for these objects.

ThisWorkbook.Activate

Workbooks("Book3.xlsx").Activate

Windows("Book3.xlsx").Activate

Avoid Select and Activate Whenever Possible

It's great to know the difference between these two methods, but you might not need them as often as you think. Especially if you are just starting out with VBA and using the macro recorder a lot.

The macro recorder code contains a lot of lines with the Select method because it is generating code for every action we take. However, we do NOT need to select an object before we take actions on it. This can slow down our code and make it more prone to errors.

How do you use selection in VBA?

Here is an article on How to Avoid the Select Method in VBA & Why that explains more.

Conclusion

It's best to use Select when you want to select a single object or multiple objects. Activate should be used when you want to activate (view/select) an object within an existing selection. Activate is also used to select Workbooks, as there is not Select method for that object.

Did I miss anything? Please leave a comment below with questions or suggestions. Thank you! 🙂

How do I use selection in Excel VBA?

Excel VBA Selection – Example #2 Step 1: Write the subcategory of VBA Selection as shown below. Step 2: Select the range of cell as per your need or else we can keep off using the same range of cells which we had seen in the above example. Step 3: Now choose Offset function along with Selection as shown below.

How do you define a selection in VBA?

VBA Selection. In VBA, we can select any range of cells or a group of cells and perform different operations on them. For example, the selection is a range object, so we use the Range method to select the cells as it identifies the cells and the code to select the cells is the “Select” command.

Why should you not use select in VBA?

The problem with the Select method is that it can really slow down your macro. When we use the Select method, VBA has to force the Excel application to update the screen with the selection change (display a new worksheet, scroll to a range/cell, etc.). This screen update takes extra time and is usually unnecessary.

How do I create a selection box in Excel VBA?

To create a combo box in Excel VBA, execute the following steps..
On the Developer tab, click Insert..
In the ActiveX Controls group, click Combo Box..
Drag a combo box on your worksheet. ... .
Open the Visual Basic Editor..
Double click on This Workbook in the Project Explorer..