• Skip to main content
  • Skip to primary sidebar
  • Home
  • Blog
  • VBA Programming
  • Open-Libre Office
  • Web Stuff
  • Videos
    • VBA Articles
    • LibreOffice And Open Office
  • Excel VBA Glossary
  • About
  • Contact

My Best Stuff Is In The Newsletter

No charge. No spam. Unsubscribe anytime.

Submitting...

How To Copy A Range Of Cells In Excel Using VBA

There are many ways to achieve the same objective in Excel, in this instance copying a range of cells to a new spreadsheet location.

Copy cells from one range to another using vba Copy cells from one range to another using vba

The spreadsheet above represents the situation after the code has been executed.

Before execution the range "F1 through "H6" would not contain any data.

In this example I will copy the cells in vba by creating a range object "rngSource" to refer to the cells in range "A1" through "C6".

I will then create another range object "rngTarget" to refer to cell "F1".

To eliminate any confusion the object "cdeData" on line 4 in the code listing below (which you can also see in the properties window to the right of this paragraph) refers to the "code sheet" rather that the Tab name in this instance "The Data".

Line 6 below executes the copy command on the range object and passes it the destination parameter which in this instance is "rngTarget" or cell "F1".

That’s it we have copied the source range to the target range, job done!

Line 7 then cleans things up by destroying the range objects as they are no longer required.

01  Sub copyCells()
02    Dim rngSource As Range, rngTarget As Range
03    ' Using Clipboard
04    Set rngSource = cdeData.Range("A1").currentRegion
05    Set rngTarget = cdeData.Range("F1")
06    rngSource.Copy rngTarget
07    Set rngSource = Nothing: Set rngTarget = Nothing:
08  End Sub 

Get the spreadsheet used in the tutorial above.

Also get updates when new blog or video tutorials are released.

Submitting…

How To Copy An Excel Range Without Using Copy

Another way of achieving the same result and the way I do it in most, but not all cases is to write the contents of the source range to the target range without using copy.

This is done by makeing the target range the same dimensions as the source range and then simply setting the value property of the source range to the target range.

Because both ranges are objects you can query their dimensions and then use the resize method to change the size of the target range in this instance.

01  Sub copyCells()
02    Dim rngSource As Range, rngTarget As Range
03    
04    Set rngSource = cdeData.Range("A1").currentRegion
05    Set rngTarget = cdeData.Range("F1")
06    ' Direct Writing
07    rngTarget.Resize(rngSource.Rows.Count, rngSource.Columns.Count).Value = rngSource.Value
08
09    Set rngSource = Nothing: Set rngTarget = Nothing:
10  End Sub 

Line 7 above resizes the target range and writes the range to cells "F1" through "H6" in one clean readable line.

Don’t forget to sign up in the yellow box above to get notified about future articles and videos on VBA and other businessprogrammer topics.

Filed Under: VBA Tagged With: Excel VBA Macros, macro, vba

Reader Interactions

Comments

  1. Scott Gaines says

    05/10/2017 at 12:30

    Another great article Sean!

    I’m just beginning to learn about VBA and love your videos they are very informative! You’ve made me look at solving problems differently than having VBA record my keystrokes. 🙂

    I hope you post more in the future,

    Regards,

    Scott

    Reply
    • Sean Johnson says

      05/10/2017 at 12:50

      Hi Scott

      Thanks for the comment, Yes I will be publishing more in the future, so watch this space, also as part of my research feel free to tell me your biggest challenges with learning vba and business programming in general.

      Reply
  2. Hassan Jatta says

    21/12/2019 at 23:52

    Many thanks for your lovely videos which are greatly helping me learn VBA. I am a beginner but learning slowly with the goal of writing simple payroll, fixed assets register, staff loans programmes etc. I will be very grateful if you can share any codes, tips and tricks to prepare me for my VBA challenges in 2020.

    Reply
    • Sean Johnson says

      20/01/2020 at 14:28

      Hi Hassan

      Check out my youtube channel I publish there regularly.
      This Playlist covers most of my VBA stuff, I’ve ordered it by experience level.

      Reply

Leave a Reply to Hassan Jatta Cancel reply

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

Primary Sidebar

Free Training Videos

Make VBA Listbox Work as Database Grid

Image

Ulitmate Excel VBA Advanced Filter Tutorial

Click video and press f for full screen or watch on YouTube

Dynamically Link Images to LibreOffice Base Form

Click video and press f for full screen or watch on YouTube

Create Database with LibreOffice Base

Click video and press f for full screen or watch on YouTube

Create VBA Form In Excel

Click video and press f for full screen or watch on YouTube

Affiliate Disclamer

We hope you love the products we recommend! Just so you know, we may collect a share of sales or other compensation from the links on this page.
Thank you if you use our links, we really appreciate it!

Terms Of Service | Privacy Statement | Copyright © 2024 BusinessProgrammer.com.