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:=xlUp

Generate accurate VBA code in seconds with AutoMacro

AutoMacroExample

AutoMacro is a powerful VBA code generator that comes loaded with an extensive code library and many other time-saving tools and utilities.

Whether you’re an experienced coder looking to save time, or a newbie just trying to get things to work, AutoMacro is the tool for you.

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

Don’t forget:

If you’ve found this post useful, or if you have a better approach, then please leave a comment below.

Do you need help adapting this to your needs?

I’m guessing the examples in this post didn’t exactly meet 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:

3 thoughts on “Cell and Range VBA properties and actions

  1. Yasser says:

    Thank you very much for great snippets. I like these a lot

    Please review the following lines:
    ——————————————-
    Rows(“B:B”).Insert (should be “2:3” for exampel)

    Range(“H7:K10”).Values = Range(“A1:D4”).Values (Notice extra s)

    Range(“H7:K10”).Format = Range(“A1:D4”).Format (Not correct)

    in the section of find the last cell : .Rows.Count (extra dot) and the same extra dot in .Column.Count

    Please let me know if you replied via my email
    Regards

    • Excel Off The Grid says:

      Hi Yasser,

      You are right on all accounts. Thanks for spotting it.

      The time stamp says I initially posted this one at 11:30 at night, clearly my brain had already gone to sleep. I should know better than to type or edit code directly in a web browser, it’s hard enough getting right in the VB Editor.

  2. Yasser says:

    Never mind my friend. I just wanted you to correct those lines so as to make the site clean reference for learners
    Thank you very much for your great contributions

Leave a Reply

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