Get our FREE VBA eBook of the 30 most useful Excel VBA macros.

Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Claim your free eBook


Cell and Range VBA properties and actions

VBA Code Snippets

Manipulating ranges and cells is one of the most common actions in VBA.   You can use the following to either learn from or just to copy and paste into your own code.

Referencing ranges & cells from the worksheet

'Reference range by address
Workbooks("WorkbookName.xlsx").Worksheets("SheetName").Range("A1:D4").[Other properties and actions]

'Reference cells by row and column - Cells(Row,Column)
Workbooks("WorkbookName.xlsx").Worksheets("SheetName").Cells(1, 1).[Other properties and actions]

'Reference ranges by a defined name
Workbooks("WorkbookName.xlsx").Worksheets("SheetName").Range("RangeName").[Other properties and actions]

Assigning a range to a variable

'Assigning a range to a variable
Dim Rng As Range
Set Rng = Workbooks("WorkbookName.xlsx").Worksheets("SheetName").Range("A1:D4")

'Assigning a cell to a variable
Dim Rng As Range
Set Rng = Workbooks("WorkbookName.xlsx").Worksheets("SheetName").Cells(1, 1)

Selecting ranges and cells

'Select range by address
Range("A1:D4").Select

'Select cells by row and column - Cells(Row,Column)
Cells(1, 1).Select

'Select ranges by a defined name
Range("RangeName").Select

Changing the value of a range or cell

'Change value of a range by address
Range("A1:D4").Value = "Text here"

'Change value cells by row and column - Cells(Row,Column)
Cells(1, 1).Value = 30

Setting the value of a variable based on the value of a cell

'Setting a variable based on a cell value
Dim CellValue As Integer
CellValue = Cells(1, 1).Value

Counting cells, rows and columns

'Count cells in a range
Dim CellsInRange As Long
CellsInRange = Range("A1:D4").Cells.Count

'Count rows in a range
Dim RowsInRange As Long
RowsInRange = Range("A1:D4").Rows.Count
 
'Count columns in a range
Dim ColumnsInRange As Long
ColumnsInRange = Range("A1:D4").Columns.Count

Looping through cells, rows and columns in a range

'Loop through action for each cell in a range
Dim Rng As Range
Dim CellsInRng As Range

Set Rng = Range("A1:D4")

For Each CellsInRange In Rng
    'Carry out an action
Next CellsInRange

'Loop through action for each row in a range
Dim Rng As Range
Dim CellsInRng As Range

Set Rng = Range("A1:D4")

For Each CellsInRange In Rng.Rows
    'Carry out an action
Next CellsInRange

'Loop through action for each column in a range
Dim Rng As Range
Dim CellsInRng As Range

Set Rng = Range("A1:D4")

For Each CellsInRange In Rng.Columns
    'Carry out an action
Next CellsInRange

Inserting rows, columns, ranges and cells

'Inserting Columns
Columns("B:B").Insert

'Inserting Rows
Rows("2:3").Insert

'Inserting a range of cells (shift cells to the right)
Range("A1:D4").Insert Shift:=xlToRight

'Inserting a range of cells (shift cells to down)
Range("A1:D4").Insert Shift:=xlDown

Deleting rows, columns, ranges and cells

'Deleting columns
Columns("B:B").Delete

'Deleting rows
Rows("3:4").Delete

 'Delete a range of cells (shift cells to the right)
Range("A1:D4").Delete Shift:=xlToLeft

'Delete a range of cells (shift cells to the down)
Range("A1:D4").Delete Shift:=xlU

Copy and pasting

'Copy and paste everyting
Range("A1:D4").Copy
Range("H7").Paste

'Copy and paste values only
Range("A1:D4").Copy
Range("H7").PasteSpecial Paste:=xlPasteValues

'Copy and paste formats only
Range("A1:D4").Copy
Range("H7").PasteSpecial Paste:=xlPasteFormats

Copying and paste without using the clipboard

'Copy everyting
Range("A1:D4").Copy Destination:=Range("H7")

'Copy values only
Range("H7:K10").Value = Range("A1:D4").Value

Finding the last cell in a row or column

'Last used cell in one row
Dim LastRow As Long
LastRow = Workbooks("WorkbookName.xlsx").Worksheets("SheetName").Cells(Rows.Count, "A").End(xlUp).Row

'Last used cell in one column
Dim LastCol As Integer
LastCol = Workbooks("WorkbookName.xlsx").Worksheets("SheetName").Cells(1, Columns.Count).End(xlToLeft).Column

Finding the first cell in a range

'Find the row of first cell of a range
Dim FirstRow As Long
Dim Rng As Range
Set Rng = Range("A1:D4")
FirstRow = Rng.Row

'Find the column of first cell of a range
Dim FirstColumn As Long
Dim Rng As Range
Set Rng = Range("A1:D4")
FirstColumn = Rng.Column

Finding the row and column of the active cell

'Find the row of the active cell
 ActiveCell.Row

'Find the columns of the active cell
 ActiveCell.Column

'Find the address of the active cells
ActiveCell.Address

Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).


Do you need help adapting this post to your needs?

I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you're still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise.  List all the things you've tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don't go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

Leave a Reply

Your email address will not be published. Required fields are marked *