Operations on ranges

Copy

The copy action is done by using the copy command:

Range("a3").Copy

Paste

The paste action is done by using the ActiveSheet.Paste command:

Range("a3").Copy

Range("a4").Select

ActiveSheet.Paste

Application.CutCopyMode = False

The above code copies the content of cell A3 and pastes it into cell A4.

The command: Application.CutCopyMode = False cancels the copying state (the surrounding lineal) of cell A3.

Tip:

You can use the following shortcut to paste the value into the desired range (in this case, into cell A4):

Range("a3").Copy Range("a4")

Alternatively, you can use the code:

Range("a4").Value = Range("a3").Value

Note:

Although both of the codes above yield the same result, the way they perform the action is completely different:

In the first code, we copied the value from cell A3 and pasted it into cell A4.

In the second code, we entered the value of cell A3 into cell A4 by using the “=” operation.

Naming a range

You can give names to cells or ranges from within the workbook itself, but you can also do it by using the command Name.

For example:

Range("a1:a5").Name = "FirstRange"

Range("FirstRange").Select

In this code, we gave the name “FirstRange” to the range A1:B5, and then we selected it according to its name.

Making calculations on ranges

You can perform mathematical calculations on cells, for example:

Range("a3").Value = Range("a3").Value - 3
Range("a4").Value = Range("a3").Value + 10

In the example above, cell A3 received a new value after a calculation that subtracted the number 3 from its current value.

Cell A4 received a value equal to the value in cell A3, plus 10.

Note:

In Microsoft Excel you cannot enter a value into a cell that is based on a calculation that includes the cell itself (“circular reference”). However, it is possible when writing a VBA code, because the result of the calculation will appear in the cell as a value and not as a formula.

Do you think it was helpful? Share it with your friends