Vba excel entirerow select

In this Article

  • Select Entire Rows or Columns
    • Select Single Row
    • Select Single Column
    • Select Multiple Rows or Columns
    • Select ActiveCell Row or Column
    • Select Rows and Columns on Other Worksheets
    • Is Selecting Rows and Columns Necessary?
  • Methods and Properties of Rows & Columns
    • Delete Entire Rows or Columns
    • Insert Rows or Columns
    • Copy & Paste Entire Rows or Columns
    • Hide / Unhide Rows and Columns
    • Group / UnGroup Rows and Columns
    • Set Row Height or Column Width
    • Autofit Row Height / Column Width
  • Rows and Columns on Other Worksheets or Workbooks
  • Get Active Row or Column

This tutorial will demonstrate how to select and work with entire rows or columns in VBA.

First we will cover how to select entire rows and columns, then we will demonstrate how to manipulate rows and columns.

Select Entire Rows or Columns

Select Single Row

You can select an entire row with the Rows Object like this:

Rows(5).Select

Or you can use EntireRow along with the Range or Cells Objects:

Range("B5").EntireRow.Select

or

Cells(5,1).EntireRow.Select

You can also use the Range Object to refer specifically to a Row:

Range("5:5").Select

Select Single Column

Instead of the Rows Object, use the Columns Object to select columns. Here you can reference the column number 3:

Columns(3).Select

or letter “C”, surrounded by quotations:

Columns("C").Select

Instead of EntireRow, use EntireColumn along with the Range or Cells Objects to select entire columns:

Range("C5").EntireColumn.Select

or

Cells(5,3).EntireColumn.Select

You can also use the Range Object to refer specifically to a column:

Range("B:B").Select

Select Multiple Rows or Columns

Selecting multiple rows or columns works exactly the same when using EntireRow or EntireColumn:

Range("B5:D10").EntireRow.Select

or

Range("B5:B10").EntireColumn.Select

However, when you use the Rows or Columns Objects, you must enter the row numbers or column letters in quotations:

Rows("1:3").Select

or

Columns("B:C").Select

Select ActiveCell Row or Column

To select the ActiveCell Row or Column, you can use one of these lines of code:

ActiveCell.EntireRow.Select

or

ActiveCell.EntireColumn.Select

Select Rows and Columns on Other Worksheets

In order to select Rows or Columns on other worksheets, you must first select the worksheet.

Sheets("Sheet2").Select
Rows(3).Select

The same goes for when selecting rows or columns in other workbooks.

Workbooks("Book6.xlsm").Activate
Sheets("Sheet2").Select
Rows(3).Select

Note: You must Activate the desired workbook. Unlike the Sheets Object, the Workbook Object does not have a Select Method.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

automacro

Learn More

Is Selecting Rows and Columns Necessary?

However, it’s (almost?) never necessary to actually select Rows or Columns. You don’t need to select a Row or Column in order to interact with them. Instead, you can apply Methods or Properties directly to the Rows or Columns. The next several sections will demonstrate different Methods and Properties that can be applied.

You can use any method listed above to refer to Rows or Columns.

Methods and Properties of Rows & Columns

Delete Entire Rows or Columns

To delete rows or columns, use the Delete Method:

Rows("1:4").Delete

or:

Columns("A:D").Delete

VBA Programming | Code Generator does work for you!

Insert Rows or Columns

Use the Insert Method to insert rows or columns:

Rows("1:4").Insert

or:

Columns("A:D").Insert

Copy & Paste Entire Rows or Columns

Paste Into Existing Row or Column

When copying and pasting entire rows or columns you need to decide if you want to paste over an existing row / column or if you want to insert a new row / column to paste your data.

These first examples will copy and paste over an existing row or column:

Range("1:1").Copy Range("5:5")

or

Range("C:C").Copy Range("E:E")

Insert & Paste

These next examples will paste into a newly inserted row or column.

This will copy row 1 and insert it into row 5, shifting the existing rows down:

Range("1:1").Copy
Range("5:5").Insert

This will copy column C and insert it into column E, shifting the existing columns to the right:

Range("C:C").Copy
Range("E:E").Insert

Hide / Unhide Rows and Columns

To hide rows or columns set their Hidden Properties to True. Use False to hide the rows or columns:

'Hide Rows
Rows("2:3").EntireRow.Hidden = True

'Unhide Rows
Rows("2:3").EntireRow.Hidden = False

or

'Hide Columns
Columns("B:C").EntireColumn.Hidden = True

'Unhide Columns
Columns("B:C").EntireColumn.Hidden = False

Group / UnGroup Rows and Columns

If you want to Group rows (or columns) use code like this:

'Group Rows
Rows("3:5").Group

'Group Columns
Columns("C:D").Group

To remove the grouping use this code:

'Ungroup Rows
Rows("3:5").Ungroup

'Ungroup Columns
Columns("C:D").Ungroup

This will expand all “grouped” outline levels:

ActiveSheet.Outline.ShowLevels RowLevels:=8, ColumnLevels:=8

and this will collapse all outline levels:

ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1

Set Row Height or Column Width

To set the column width use this line of code:

Columns("A:E").ColumnWidth = 30

To set the row height use this line of code:

Rows("1:1").RowHeight = 30

AutoMacro | Ultimate VBA Add-in | Click for Free Trial!

Autofit Row Height / Column Width

To Autofit a column:

Columns("A:B").Autofit

To Autofit a row:

Rows("1:2").Autofit

Rows and Columns on Other Worksheets or Workbooks

To interact with rows and columns on other worksheets, you must define the Sheets Object:

Sheets("Sheet2").Rows(3).Insert

Similarly, to interact with rows and columns in other workbooks, you must also define the Workbook Object:

Workbooks("book1.xlsm").Sheets("Sheet2").Rows(3).Insert

Get Active Row or Column

To get the active row or column, you can use the Row and Column Properties of the ActiveCell Object.

MsgBox ActiveCell.Row

or

MsgBox ActiveCell.Column

This also works with the Range Object:

MsgBox Range("B3").Column

VBA Select

It is very common to find the .Select methods in saved macro recorder code, next to a Range object.

.Select is used to select one or more elements of Excel (as can be done by using the mouse) allowing further manipulation of them.

Selecting cells with the mouse:

Select with the Mouse

Selecting cells with VBA:

    'Range([cell1],[cell2])
    Range(Cells(1, 1), Cells(9, 5)).Select
    Range("A1", "E9").Select
    Range("A1:E9").Select

Each of the above lines select the range from «A1» to «E9».

Select with VBA


VBA Select CurrentRegion

If a region is populated by data with no empty cells, an option for an automatic selection is the CurrentRegion property alongside the .Select method.

CurrentRegion.Select will select, starting from a Range, all the area populated with data.

    Range("A1").CurrentRegion.Select

CurrentRegion

Make sure there are no gaps between values, as CurrentRegion will map the region through adjoining cells (horizontal, vertical and diagonal).

  Range("A1").CurrentRegion.Select

With all the adjacent data

CurrentRegion Data

Not all adjacent data

CurrentRegion Missing Data

«C4» is not selected because it is not immediately adjacent to any filled cells.


VBA ActiveCell

The ActiveCell property brings up the active cell of the worksheet.

In the case of a selection, it is the only cell that stays white.

A worksheet has only one active cell.

    Range("B2:C4").Select
    ActiveCell.Value = "Active"

ActiveCell

Usually the ActiveCell property is assigned to the first cell (top left) of a Range, although it can be different when the selection is made manually by the user (without macros).

ActiveCell Under

The AtiveCell property can be used with other commands, such as Resize.


VBA Selection

After selecting the desired cells, we can use Selection to refer to it and thus make changes:

    Range("A1:D7").Select
    Selection = 7

Example Selection

Selection also accepts methods and properties (which vary according to what was selected).

    Selection.ClearContents 'Deletes only the contents of the selection
    Selection.Interior.Color = RGB(255, 255, 0) 'Adds background color to the selection

Clear Selection

As in this case a cell range has been selected, the Selection will behave similarly to a Range. Therefore, Selection should also accept the .Interior.Color property.

RGB (Red Green Blue) is a color system used in a number of applications and languages. The input values for each color, in the example case, ranges from 0 to 255.


Selection FillDown

If there is a need to replicate a formula to an entire selection, you can use the .FillDown method

    Selection.FillDown

Before the FillDown

Before Filldown

After the FillDown

After FillDown

.FillDown is a method applicable to Range. Since the Selection was done in a range of cells (equivalent to a Range), the method will be accepted.

.FillDown replicates the Range/Selection formula of the first line, regardless of which ActiveCell is selected.

.FillDown can be used at intervals greater than one column (E.g. Range(«B1:C2»).FillDown will replicate the formulas of B1 and C1 to B2 and C2 respectively).


VBA EntireRow and EntireColumn

You can select one or multiple rows or columns with VBA.

    Range("B2").EntireRow.Select
    Range("C3:D3").EntireColumn.Select

Select EntireColumn

The selection will always refer to the last command executed with Select.

To insert a row use the Insert method.

    Range("A7").EntireRow.Insert
    'In this case, the content of the seventh row will be shifted downward

To delete a row use the Delete method.

    Range("A7").EntireRow.Delete
    'In this case, the content of the eighth row will be moved to the seventh

VBA Rows and Columns

Just like with the EntireRow and EntireColumn property, you can use Rows and Columns to select a row or column.

    Columns(5).Select
    Rows(3).Select

Select Rows

To hide rows:

    Range("A1:C3").Rows.Hidden = True

Hidden Cells

In the above example, rows 1 to 3 of the worksheet were hidden.


VBA Row and Column

Row and Column are properties that are often used to obtain the numerical address of the first row or first column of a selection or a specific cell.

    Range("A3:H30").Row 'Referring to the row; returns 3
    Range("B3").Column  'Referring to the column; returns 2

The results of Row and Column are often used in loops or resizing.



Consolidating Your Learning

Suggested Exercise



SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.

Excel ® is a registered trademark of the Microsoft Corporation.

© 2023 SuperExcelVBA | ABOUT

Protected by Copyscape

I have a row of data (A3 to A11) that I would like to select (there are no blanks in this range). I am using the following code:

Range(ws.Range("A3"), ws.Range("A3").End(xlToRight)).Select

However, this code is only selecting cell A3 and not A3 to A11. I have also tried xlToLeft and that still only selects A3 as well…How can I correct this? Thanks.

Community's user avatar

asked Oct 25, 2011 at 6:04

user1012091's user avatar

0

Just because of your title, here are a couple ways to select a row

ws.Rows(3).Select
ws.Range("a3").EntireRow.Select

to select all the data on a row,

ws.Range("A3",ws.Cells(3,ws.Columns.Count).End(xlToLeft)).Select

Also in your example, you’re missing the «ws.» prefix from the outermost range object — without specifying the worksheet, Excel will try to refer to the ActiveSheet and you’ve just written a potential bug.

answered Oct 25, 2011 at 7:20

Nick's user avatar

NickNick

3,57312 gold badges37 silver badges43 bronze badges

I think this overlaps with Populating a list box with data from worksheet. I suggest you continue any issues related to this thread back in the original posting

As per prior question to select vertically you use xlDown and xlUp (not xltoRight or xltoLeft)

ws.Range(ws.[a3], ws.Cells(Rows.Count, "A").End(xlUp))

Community's user avatar

answered Oct 25, 2011 at 6:12

brettdj's user avatar

brettdjbrettdj

54.6k16 gold badges113 silver badges176 bronze badges

3

To select entire row Dynamically(current active cell), Try Below VBA code snippet:

ActiveSheet.Range(Selection, Selection).EntireRow.Select

answered Nov 7, 2017 at 9:32

Deepak Kushwaha's user avatar

You need to select range from A3 to A11. This would do the trick

  activesheet.range("A3:A11").select  or
  activesheet.range(cells(3,"A"),cells(11,"A")).select  or
  activesheet.range(cells(3,1),cells(11,1)).select 

answered Oct 25, 2011 at 7:06

niko's user avatar

nikoniko

9,22127 gold badges81 silver badges131 bronze badges

How about:

a = Range("A11").End(xlToRight).Address
Range("A3:" & a).Select

answered Oct 25, 2011 at 8:52

Fionnuala's user avatar

FionnualaFionnuala

90.1k7 gold badges110 silver badges148 bronze badges

RANGE("A1", Cells(RANGE("A23").row, Columns.Count).End(xlToLeft)).Select    'YES

answered May 2, 2016 at 10:00

dave's user avatar

1

Try This…

ActiveSheet.UsedRange.EntireRow.Select

answered Feb 13, 2017 at 17:47

Nhubs's user avatar

1

Need to Select Data Range in Excel

In Microsoft Excel, several operations like delete, edit, type, and other formatting can be done only if we select the range of cells we need to change. There are several ways in which we select the range of cells. In this article, we will discuss selecting a specific row or row using VBA to perform any action or format them.

Select a Row if You Know its Number

Here is the code that helps select a specific row if we know the row number. Let us select row number 15 here.

Sub row_select()

'Select the row no 15
Rows(15).Select

End Sub

Output:

Here I already have a “Simple Interest” sheet with some data. Row number 15 of the active sheet is selected.

Output of a code selecting row number 15

Using the EntireRow Property

This is a property of a cell reference. We can use it in two ways, as explained below.

Using a Range Reference

We select the entire row to which a specified cell belongs.

Sub row_select()
'Select the rows that belongs to Cell G6
Range("G6").EntireRow.Select
End Sub

Output:

Since G6 belongs to row number 6, that row is selected here.

Output of code that selects and entire row to which a specified cell belongs.

Using a Cell Reference

Sub row_select()
'Select the rows that belongs to Cell (5,3) i.e.  5th row
Cells(5, 3).EntireRow.Select
End Sub

Output:

Output of code selecting a range of cells

Select 1 or More Rows:

The range object can be used to select 1 or more rows too.

The code below selects all rows from 5 to 8.

 
Sub row_select()

'Select the rows from 5 to 8
Range("5:8").EntireRow.Select

End Sub

Output of code selecting all rows from 5 to 8.

Select All Rows That Belong to a Specific Range

Let us assume a specific range B3 to E5. Now, if we use the entire row property on this range and select, we can notice that rows 3 to 5 have been selected because the specified range belongs to these three rows.

Sub row_select()

'Select the rows that belong to a range
Range("B3:E5").EntireRow.Select

End Sub

Output:

Output of the entirerow property on a range

Selection of the Row to Which the Active Cell Belongs

The code below is run after selecting cells (10, 3) or C10.

Sub row_select()

'Select the rows to which the active cell belongs
ActiveCell.EntireRow.Select

End Sub

Output of entirerow code being run after selecting cells

Selecting All Rows of the Active Sheet

All rows of the active sheet (Simple interest sheet in this example) is selected.

Sub row_select()

'Select the rows of the active sheet
ActiveSheet.Rows.Select

End Sub

Output:

Output of selecting all rows in an active sheet

Select a Specific Row in a Range

In this example, the rows are not selected from the sheet. Instead, a specified range is assumed as a sheet, and the row number specified within the code is assumed as the row number within the specified range and selected. For example, we want to select the 2nd row of the range D4 to H8. The second row of this range is row number 5 of the spreadsheet.

Sub row_select()

'Select the row within the range
Range("D4:H8").Rows(2).Select

End Sub

Output:

Output when a specified range is assumed as a sheet, and the row number specified within the code is assumed as the row number within the specified range and selected

Now, we see that the 2nd row of the range D4 to H8 is selected within the 5th row of the spreadsheet.

Color Rows That Have Data in the First Column

In this example, we will consider the first 10 rows and go through the content of the first column in each row. If the cell is not null, then we will color it green.

Sub row_select()

' Loop through the first 10 rows
For i = 1 To 10

' validate if the cell is blank
If Cells(i, 1).Value <> "" Then

'select the row
Rows(i).EntireRow.Select

' colour the selected row
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    ' end the condition
End If
' end the loop
Next

End Sub

Output ofcode that fills the first columns that aren't null green

The rows that do not have values in column A (only until row number 10) are colored green.

Conclusion

Row selection can be used with loops and conditions in the program logic. However, unlike a manual task, most of the tasks in VBA can also be done directly without selecting cells/rows/columns/range. So, the selection operation is not always necessary.

This example teaches you how to select entire rows and columns in Excel VBA. Are you ready?

Place a command button on your worksheet and add the following code lines:

1. The following code line selects the entire sheet.

Cells.Select

Entire Sheet in Excel VBA

Note: because we placed our command button on the first worksheet, this code line selects the entire first sheet. To select cells on another worksheet, you have to activate this sheet first. For example, the following code lines select the entire second worksheet.

Worksheets(2).Activate
Worksheets(2).Cells.Select

2. The following code line selects the second column.

Columns(2).Select

Column

3. The following code line selects the seventh row.

Rows(7).Select

Row

4. To select multiple rows, add a code line like this:

Rows(«5:7»).Select

Multiple Rows

5. To select multiple columns, add a code line like this:

Columns(«B:E»).Select

Multiple Columns

6. Be careful not to mix up the Rows and Columns properties with the Row and Column properties. The Rows and Columns properties return a Range object. The Row and Column properties return a single value.

Code line:

MsgBox Cells(5, 2).Row

Result:

Row Property

7. Select cell D6. The following code line selects the entire row of the active cell.

ActiveCell.EntireRow.Select

EntireRow

Note: border for illustration only.

8. Select cell D6. The following code line enters the value 2 into the first cell of the column that contains the active cell.

ActiveCell.EntireColumn.Cells(1).Value = 2

EntireColumn

Note: border for illustration only.

9. Select cell D6. The following code line enters the value 3 into the first cell of the row below the row that contains the active cell.

ActiveCell.EntireRow.Offset(1, 0).Cells(1).Value = 3

EntireRow + Offset

Note: border for illustration only.

Like this post? Please share to your friends:
  • Vba excel doevents примеры
  • Vba excel doevents описание
  • Vba excel do while not eof
  • Vba excel do not save changes
  • Vba excel dir file