Guide
Cell Navigation
Basic navigation is one of the most important parts of programming and is usually the starting point when learning to code VBA instead of just recording it. Below you will see some useful examples of how to navigate the cells in your spreadsheets.
One of the most useful tips I can give is that the currently selected cell is the 'ActiveCell'.
To simply select cell A1.
Range("A1").Select
To select cell A1 and D2.
Range("A1,D2").Select
To select cell A1 and D1.
Range("A1:D1").Select
The next two lines select cell A1 as the 'Cells' function required the row then the column.
Cells(1, 1).Select
Cells(1, "A").Select
Both of the next two lines will stay in the current row but move to column B.
Cells(ActiveCell.Row, "B").Select
Cells(ActiveCell.Row, 2).Select
The below code will move the cursor to row one but stay in the same column.
Cells(1, ActiveCell.Column).Select
To select column D.
Columns("D").Select
To select Columns E and G.
Range("E:E,G:G").Select
To select columns E to G.
Columns("E:G").Select
To select row 1.
Rows(1).Select
To select rows 4 and 9.
Range("4:4,9:9").Select
To select rows 4 to 9.
Rows("4:9").Select
From cell A1, to get to the end of a range use...
Selection.End(xlDown).Select
You can also use the following codes to navigate in different directions.
Selection.End(xlUp).Select
Selection.End(xlToRight).Select
Selection.End(xlToLeft).Select
If you want to highlight as you go the code can be used within a selection.
Range _
(Selection, Selection.End(xlDown)).Select
Range _
(Selection,Selection.End(xlToRight)).Select
Cell Navigation
Basic navigation is one of the most important parts of programming and is usually the starting point when learning to code VBA instead of just recording it. Below you will see some useful examples of how to navigate the cells in your spreadsheets.
One of the most useful tips I can give is that the currently selected cell is the 'ActiveCell'.
To simply select cell A1.
Range("A1").Select
To select cell A1 and D2.
Range("A1,D2").Select
To select cell A1 and D1.
Range("A1:D1").Select
The next two lines select cell A1 as the 'Cells' function required the row then the column.
Cells(1, 1).Select
Cells(1, "A").Select
Both of the next two lines will stay in the current row but move to column B.
Cells(ActiveCell.Row, "B").Select
Cells(ActiveCell.Row, 2).Select
The below code will move the cursor to row one but stay in the same column.
Cells(1, ActiveCell.Column).Select
To select column D.
Columns("D").Select
To select Columns E and G.
Range("E:E,G:G").Select
To select columns E to G.
Columns("E:G").Select
To select row 1.
Rows(1).Select
To select rows 4 and 9.
Range("4:4,9:9").Select
To select rows 4 to 9.
Rows("4:9").Select
From cell A1, to get to the end of a range use...
Selection.End(xlDown).Select
You can also use the following codes to navigate in different directions.
Selection.End(xlUp).Select
Selection.End(xlToRight).Select
Selection.End(xlToLeft).Select
If you want to highlight as you go the code can be used within a selection.
Range _
(Selection, Selection.End(xlDown)).Select
Range _
(Selection,Selection.End(xlToRight)).Select