And if you want to automate your work in Excel using VBA, you need to know how to work with cells and ranges using VBA. There are a lot of different things you can do with ranges in VBA (such as select, copy, move, edit, etc.). So to cover this topic, I will break this tutorial into sections and show you how to work with cells and ranges in Excel VBA using examples. Let’s get started. If you’re interested in learning VBA the easy way, check out my Online Excel VBA Training.

Selecting a Cell / Range in Excel using VBA

To work with cells and ranges in Excel using VBA, you don’t need to select it. In most of the cases, you are better off not selecting cells or ranges (as we will see). Despite that, it’s important you go through this section and understand how it works. This will be crucial in your VBA learning and a lot of concepts covered here will be used throughout this tutorial. So let’s start with a very simple example.

Selecting a Single Cell Using VBA

If you want to select a single cell in the active sheet (say A1), then you can use the below code: The above code has the mandatory ‘Sub’ and ‘End Sub’ part, and a line of code that selects cell A1. Range(“A1”) tells VBA the address of the cell that we want to refer to. Select is a method of the Range object and selects the cells/range specified in the Range object. The cell references need to be enclosed in double quotes. This code would show an error in case a chart sheet is an active sheet. A chart sheet contains charts and is not widely used. Since it doesn’t have cells/ranges in it, the above code can’t select it and would end up showing an error. Note that since you want to select the cell in the active sheet, you just need to specify the cell address. But if you want to select the cell in another sheet (let’s say Sheet2), you need to first activate Sheet2 and then select the cell in it. Similarly, you can also activate a workbook, then activate a specific worksheet in it, and then select a cell. Note that when you refer to workbooks, you need to use the full name along with the file extension (.xlsx in the above code). In case the workbook has never been saved, you don’t need to use the file extension. Now, these examples are not very useful, but you will see later in this tutorial how we can use the same concepts to copy and paste cells in Excel (using VBA). Just as we select a cell, we can also select a range. In case of a range, it could be a fixed size range or a variable size range. In a fixed size range, you would know how big the range is and you can use the exact size in your VBA code. But with a variable-sized range, you have no idea how big the range is and you need to use a little bit of VBA magic. Let’s see how to do this.

Selecting a Fix Sized Range

Here is the code that will select the range A1:D20. Another way of doing this is using the below code: The above code takes the top-left cell address (A1) and the bottom-right cell address (D20) and selects the entire range. This technique becomes useful when you’re working with variably sized ranges (as we will see when the End property is covered later in this tutorial). If you want the selection to happen in a different workbook or a different worksheet, then you need to tell VBA the exact names of these objects. For example, the below code would select the range A1:D20 in Sheet2 worksheet in the Book2 workbook. Now, what if you don’t know how many rows are there. What if you want to select all the cells that have a value in it. In these cases, you need to use the methods shown in the next section (on selecting variably sized range).

Selecting a Variably Sized Range

There are different ways you can select a range of cells. The method you choose would depend on how the data is structured. In this section, I will cover some useful techniques that are really useful when you work with ranges in VBA.  

Select Using CurrentRange Property

In cases where you don’t know how many rows/columns have the data, you can use the CurrentRange property of the Range object. The CurrentRange property covers all the contiguous filled cells in a data range. Below is the code that will select the current region that holds cell A1. The above method is good when you have all data as a table without any blank rows/columns in it.

But in case you have blank rows/columns in your data, it will not select the ones after the blank rows/columns. In the image below, the CurrentRegion code selects data till row 10 as row 11 is blank.

In such cases, you may want to use the UsedRange property of the Worksheet Object.

Select Using UsedRange Property

UsedRange allows you to refer to any cells that have been changed. So the below code would select all the used cells in the active sheet. Note that in case you have a far-off cell that has been used, it would be considered by the above code and all the cells till that used cell would be selected.

Select Using the End Property

Now, this part is really useful. The End property allows you to select the last filled cell. This allows you to mimic the effect of Control Down/Up arrow key or Control Right/Left keys. Let’s try and understand this using an example. Suppose you have a dataset as shown below and you want to quickly select the last filled cells in column A. The problem here is that data can change and you don’t know how many cells are filled. If you have to do this using keyboard, you can select cell A1, and then use Control + Down arrow key, and it will select the last filled cell in the column. Now let’s see how to do this using VBA. This technique comes in handy when you want to quickly jump to the last filled cell in a variably-sized column The above code would jump to the last filled cell in column A. Similarly, you can use the End(xlToRight) to jump to the last filled cell in a row. Now, what if you want to select the entire column instead of jumping to the last filled cell. You can do that using the code below: In the above code, we have used the first and the last reference of the cell that we need to select. No matter how many filled cells are there, the above code will select all. Remember the example above where we selected the range A1:D20 by using the following line of code: Range(“A1″,”D20”) Here A1 was the top-left cell and D20 was the bottom-right cell in the range. We can use the same logic in selecting variably sized ranges. But since we don’t know the exact address of the bottom-right cell, we used the End property to get it. In Range(“A1”, Range(“A1”).End(xlDown)), “A1” refers to the first cell and Range(“A1”).End(xlDown) refers to the last cell. Since we have provided both the references, the Select method selects all the cells between these two references. Similarly, you can also select an entire data set that has multiple rows and columns. The below code would select all the filled rows/columns starting from cell A1. In the above code, we have used Range(“A1”).End(xlDown).End(xlToRight) to get the reference of the bottom-right filled cell of the dataset.

Difference between Using CurrentRegion and End

If you’re wondering why use the End property to select the filled range when we have the CurrentRegion property, let me tell you the difference. With End property, you can specify the start cell. For example, if you have your data in A1:D20, but the first row are headers, you can use the End property to select the data without the headers (using the code below). But the CurrentRegion would automatically select the entire dataset, including the headers. So far in this tutorial, we have seen how to refer to a range of cells using different ways. Now let’s see some ways where we can actually use these techniques to get some work done.

Copy Cells / Ranges Using VBA

As I mentioned at the beginning of this tutorial, selecting a cell is not necessary to perform actions on it. You will see in this section how to copy cells and ranges without even selecting these. Let’s start with a simple example.

Copying Single Cell

If you want to copy cell A1 and paste it into cell D1, the below code would do it. Note that the copy method of the range object copies the cell (just like Control +C) and pastes it in the specified destination. In the above example code, the destination is specified in the same line where you use the Copy method. If you want to make your code even more readable, you can use the below code: The above codes will copy and paste the value as well as formatting/formulas in it. As you might have already noticed, the above code copies the cell without selecting it. No matter where you’re on the worksheet, the code will copy cell A1 and paste it on D1. Also, note that the above code would overwrite any existing code in cell D2. If you want Excel to let you know if there is already something in cell D1 without overwriting it, you can use the code below.

Copying a Fix Sized Range

If you want to copy A1:D20 in J1:M20, you can use the below code: In the destination cell, you just need to specify the address of the top-left cell. The code would automatically copy the exact copied range into the destination. You can use the same construct to copy data from one sheet to the other. The below code would copy A1:D20 from the active sheet to Sheet2. The above copies the data from the active sheet. So make sure the sheet that has the data is the active sheet before running the code. To be safe, you can also specify the worksheet’s name while copying the data. The good thing about the above code is that no matter which sheet is active, it will always copy the data from Sheet1 and paste it in Sheet2. You can also copy a named range by using its name instead of the reference. For example, if you have a named range called ‘SalesData’, you can use the below code to copy this data to Sheet2. If the scope of the named range is the entire workbook, you don’t need to be on the sheet that has the named range to run this code. Since the named range is scoped for the workbook, you can access it from any sheet using this code. If you have a table with the name Table1, you can use the below code to copy it to Sheet2. You can also copy a range to another Workbook. In the following example, I copy the Excel table (Table1), into the Book2 workbook. This code would work only if the Workbook is already open.

Copying a Variable Sized Range

One way to copy variable sized ranges is to convert these into named ranges or Excel Table and the use the codes as shown in the previous section. But if you can’t do that, you can use the CurrentRegion or the End property of the range object. The below code would copy the current region in the active sheet and paste it in Sheet2. If you want to copy the first column of your data set till the last filled cell and paste it in Sheet2, you can use the below code: If you want to copy the rows as well as columns, you can use the below code: Note that all these codes don’t select the cells while getting executed. In general, you will find only a handful of cases where you actually need to select a cell/range before working on it.

Assigning Ranges to Object Variables

So far, we have been using the full address of the cells (such as Workbooks(“Book2.xlsx”).Worksheets(“Sheet1”).Range(“A1”)). To make your code more manageable, you can assign these ranges to object variables and then use those variables. For example, in the below code, I have assigned the source and destination range to object variables and then used these variables to copy data from one range to the other. We start by declaring the variables as Range objects. Then we assign the range to these variables using the Set statement. Once the range has been assigned to the variable, you can simply use the variable.

Enter Data in the Next Empty Cell (Using Input Box)

You can use the Input boxes to allow the user to enter the data. For example, suppose you have the data set below and you want to enter the sales record, you can use the input box in VBA. Using a code, we can make sure that it fills the data in the next blank row. The above code uses the VBA Input box to get the inputs from the user, and then enters the inputs into the specified cells. Note that we didn’t use exact cell references. Instead, we have used the End and Offset property to find the last empty cell and fill the data in it. This code is far from usable. For example, if you enter a text string when the input box asks for quantity or amount, you will notice that Excel allows it. You can use an If condition to check whether the value is numeric or not and then allow it accordingly.

Looping Through Cells / Ranges

So far we can have seen how to select, copy, and enter the data in cells and ranges. In this section, we will see how to loop through a set of cells/rows/columns in a range. This could be useful when you want to analyze each cell and perform some action based on it. For example, if you want to highlight every third row in the selection, then you need to loop through and check for the row number. Similarly, if you want to highlight all the negative cells by changing the font color to red, you need to loop through and analyze each cell’s value. Here is the code that will loop through the rows in the selected cells and highlight alternate rows. The above code uses the MOD function to check the row number in the selection. If the row number is even, it gets highlighted in cyan color. Here is another example where the code goes through each cell and highlights the cells that have a negative value in it. Note that you can do the same thing using Conditional Formatting (which is dynamic and a better way to do this). This example is only for the purpose of showing you how looping works with cells and ranges in VBA.

Where to Put the VBA Code

Wondering where the VBA code goes in your Excel workbook? Excel has a VBA backend called the VBA editor. You need to copy and paste the code in the VB Editor module code window. Here are the steps to do this:

Working with Worksheets using VBA. Working with Workbooks using VBA. Creating User-Defined Functions in Excel. For Next Loop in Excel VBA – A Beginner’s Guide with Examples. How to Use Excel VBA InStr Function (with practical EXAMPLES). Excel VBA Msgbox. How to Record a Macro in Excel. How to Run a Macro in Excel. How to Create an Add-in in Excel. Excel Personal Macro Workbook | Save & Use Macros in All Workbooks. Excel VBA Events – An Easy (and Complete) Guide. Excel VBA Error Handling. How to Sort Data in Excel using VBA (A Step-by-Step Guide). 24 Useful Excel Macro Examples for VBA Beginners (Ready-to-use).

If Worksheets(“WO_SendM”).Cells(i, 9).Value = Me.Label164.Caption Then Worksheets(“WO_SendM”).Cells(i, 4).Copy Worksheets(“WO_SendM”).Cells(i, 5).Copy erow1 = Worksheets(“WO_Ledger”).Cells(Rows.Count, 18).End(xlUp).Row Worksheets(“WO_SendM”).Paste Destination:=Worksheets(“WO_Ledger”).Cells(erow1 + 1, 18) End If Next i Is it possible to get two cell value 4 and 5 in another cell? ‘Dim wdExcelApp As Application ‘Excel is the default library (optional) Dim wdWordApp As Word.Application ‘Word app Application.ScreenUpdating = False ‘ Creating a new instance of Word Set wdWordApp = New Word.Application ‘instantiate a new instance of Word 2010 With wdWordApp ‘ Making Word Visible on the screen .Visible = True ‘iff false, document is invisible. .Activate ‘ make it the top pane, bring it to the front. ””””””””””””””””””””””””””””””””””””””””””””””’ ‘ create a new Word Document based on the specified template ””””””””””””””””””””””””””””””””””””””””””””””’ .Documents.Add “C:Usersstefan.georgescuDesktopTemplate fisa de esantionare – ver. 5-2019.dotm” ‘as before, copy the whole table from sheet to clipboard. Worksheets(“GestiuneSSC”).Activate Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy .Selection.GoTo what:=-1, Name:=”bookmark1” ‘ -1 means “wdgotobookmark” .Selection.Paste ‘paste from the clipboard to the Word Doc. ‘**************** ‘as before, copy the whole table from sheet to clipboard. Worksheets(“AlimATM”).Activate Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy .Selection.GoTo what:=-1, Name:=”bookmark2” ‘ -1 means “wdgotobookmark” .Selection.Paste ‘paste from the clipboard to the Word Doc. ‘**************** ‘as before, copy the whole table from sheet to clipboard. Worksheets(“DepRidAngajati”).Activate Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy .Selection.GoTo what:=-1, Name:=”bookmark3” ‘ -1 means “wdgotobookmark” .Selection.Paste ‘paste from the clipboard to the Word Doc. ”””””””””””””””””””””””””””’ ‘ Save WORD Document ”””””””””””””””””””””””””””’ Dim TheFileName As String TheFileName = “C:Usersstefan.georgescuDesktopFisa.docx” ‘(SaveAs is for Office 2003 and earlier – deprecated) .ActiveDocument.SaveAs2 TheFileName ‘replaces existing .doc iff exists ‘ Close Documents and Quit Word .ActiveDocument.Close ‘close .DOCx .Quit ‘exit Word End With Application.ScreenUpdating = True ‘MEMORY CLEANUP Set wdWordApp = Nothing ‘garbage collection ‘Set wdExcelApp = Nothing ‘OPTIONAL End Sub Hope to hear from you soon. Thanks in advance.