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.
Email Address * First Name *
By entering your email address you agree to receive emails from Excel Off The Grid. We'll respect your privacy and you can unsubscribe at any time.

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
100 Excel VBA Macros

Do you know the fastest way to learn foreign languages?  It is to read, write, speak, and think in that language as often as possible.  Apart from speaking, programming languages are no different.  The more you immerse yourself in that language, the faster you will pick it up.

100 Excel Macros Book

Therefore, what most people like you need is lots of examples that you can practice.  That is why the 100 Excel VBA Macros eBook exists.  It’s the book for all Excel users who want to learn how to read and write Excel macros, save time, and stand out from their peers.  The book contains:

  • 100 example codes to practice reading and writing macros that will embed the language into your thinking.
  • An introduction to macros in Excel to ensure you can implement the VBA code in the book even if you have no prior knowledge.
  • Consistent code layout between examples to enable you to understand the structure and easily customize the code to meet your needs.
  • Downloadable workbook containing all the source code, so the examples can be added to your project to give you the benefit of VBA straight away.

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


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.

Email Address * First Name *

By entering your email address you agree to receive emails from Excel Off The Grid. We’ll respect your privacy and you can unsubscribe at any time.


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:

Leave a Reply

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