Finance

Charts

Statistics

Macros

Search

Example: Moving Cells in Excel VBA

In the very first macro presented in this book, a recorded macro was used to move the contents of one cell to another:

Sub Macro1()
    Range("A1").Select
    Selection.Cut
    Range("C1").Select
    ActiveSheet.Paste
End Sub

Disadvantages of this approach:

  • Multiple steps are required, increasing the chance of errors.
  • It is unclear which workbook and worksheet the move operation applies to.
  • The process runs more slowly.

Using direct referencing, you can perform this task more clearly and efficiently:

Sub MoveCells()
    ThisWorkbook.Worksheets("Sheet1").Range("A7:A9").Cut _
        Destination:=ThisWorkbook.Worksheets("Sheet1").Range("B7")
End Sub

This method clearly specifies the source and destination ranges within the same workbook and worksheet. It avoids unnecessary selection and pasting steps, resulting in faster and more reliable code.

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx