Day 7 VBA Hero – Selecting a range of cells on a specific sheet


There are two distinct ways to select a cell or a range (a group of cells) in Excel.

Sub SelectingCells()

Sheets("Sheet1").Range("A2:B5").Select

End Sub

or if you only want to choose one cell at a time

Sub SelectingCells()

Sheets("Sheet1").Cells(2, 1).Select 'Where the first number is the row, and the second number is the column

End Sub

The first way is really the way that we are used to seeing when we use formulas in Excel.

The second format is not nearly as intuitive when you are used to Excel, but it will be useful later on.

When selecting a sheet, there are also two ways to select a sheet.

By name and by position.

By name, is the exact thing that I did in the code above.

I used the unoriginal name of Sheet1, but whatever is the name of your sheet, you can put between quotation marks.

As for the positional reference, I rarely use it unless I am sure that the order of the tabs will never change.

If you are sure of that, you can use

Sub SelectingCells()

Sheets(1).Range("A2:B5").Select

Sheets(1).Cells(2, 1).Select

End Sub

The number 1 is not in quotation marks and refers to the sheet in the 1 position of the Excel file.

If there are any hidden sheets, the 1 will take into account the hidden sheets.

So it can be kinda confusing.

Today’s homework:

Create a subroutine, and select cells “A2:C7” in the second sheet of the Excel, which has a name of “February”.

Verified by MonsterInsights