Vba excel копирование значения ячейки

I’m trying to copy values from a table to a Range, in Excel using vba Macros, but I dont want the table format, only its values. How can I achieve this?

Here is part of the code:

    'Source range
    Set r = Sheets("Sheet1").Range("Table1")

    'Destination range
    Set dest = Range(.Cells(linhaAtual, 1), .Cells(linhaAtual + r.Rows.Count - 1, 5))

    r.Copy Destination:= dest

asked Jun 18, 2014 at 20:59

pablo.vix's user avatar

You can skip the copy command altogether by assigning the values of the source range to your destination range:

'Source range
Set r = Sheets("Sheet1").Range("Table1")
'Destination range
Set dest = Range(.Cells(linhaAtual, 1), .Cells(linhaAtual + r.Rows.Count - 1, 5))

dest.Value = r.Value

answered Jun 19, 2014 at 12:47

MP24's user avatar

MP24MP24

3,07721 silver badges23 bronze badges

1

I believe you are looking for the functionality of pasting values. You can record it, or use what I have done below. (from recording so selecting is in there, which will make it run slower, but you aren’t looping so it is only constant time being added).

Selection.Copy
        'Select your destination like range("destination").select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

answered Jun 18, 2014 at 21:11

Kory's user avatar

KoryKory

3081 silver badge7 bronze badges

1

I assume you want to copy from «Sheet1» to «Sheet1» — of course you can make this a parameter and loop through all your sheets

Dim rSource as range 'Source Range
Dim rDest as range   'Target Range - It should be the same dimension
Dim wbSource as workbook 'Source Workbook
Dim wbTarget as workbook 'Target Workbook
Dim myRange as string

myRange = "A:G" ' It is an example, you can make it dynamic

'add new workbook
    Workbooks.Add
    Set wbTarget = ActiveWorkbook

'Set the Source Range
    Set rSource = wbSource.Sheets("Sheet1").Range(myRange)

'Destination Range
    Set rDest = wbTarget.Sheets("Sheet1").Range(myRange)

'Copy values only
    rSource.Copy
    rDest.PasteSpecial xlPasteValues

answered Oct 8, 2018 at 17:20

pbou's user avatar

pboupbou

2782 silver badges13 bronze badges

You need to use the pastespecial command as shown below.

'Source range
Set r = Sheets("Sheet1").Range("Table1")

'Destination range
Set dest = Range(.Cells(linhaAtual, 1), .Cells(linhaAtual + r.Rows.Count - 1, 5))

r.Copy 
dest.pastespecial paste:=xlPasteValues

answered Jun 18, 2014 at 21:15

gtwebb's user avatar

gtwebbgtwebb

2,9713 gold badges13 silver badges22 bronze badges

r.Copy
dest.pastespecial xlPastevalues

answered Jun 18, 2014 at 21:19

user2649602's user avatar

1

I achieve a solution that works.

There follows the code:

    Set r = Sheets("Criteria").Range("CriteriaTable")

    Set dest = Range(.Cells(linhaAtual, 1), .Cells(linhaAtual + r.Rows.Count - 1, 5))
    Sheets("Criteria").Activate
    r.Select
    Selection.Copy
    Sheets("Load").Activate
    dest.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

answered Jun 20, 2014 at 15:28

pablo.vix's user avatar

pablo.vixpablo.vix

2,0432 gold badges15 silver badges12 bronze badges

Use the Range.Value method. Its like setting a variable a = 1. Where you think of it as copying 1 to a. So…

Range2.value = Range1.value

or

Worksheets("historical").Range("A1:F15").Value =  Worksheets("actuals").Range("A1:F15").Value

Here I’m copying some data in worksheet actual to some historical worksheet. Or if you prefer setting the value of one range to another range.

Saurabh Bhandari's user avatar

answered Jan 30, 2018 at 4:08

RHH1095's user avatar

RHH1095RHH1095

791 silver badge4 bronze badges

You can skip the copy command altogether as MP24 said .. his suggestion worked for me after modifyibg the last line from «value» to «formula» as follows

Set r = Sheets(«Sheet1»).Range(«Table1»)
Set dest = Range(.Cells(linhaAtual, 1), .Cells(linhaAtual +
r.Rows.Count — 1, 5))

dest.formula = r.formula

answered Aug 1, 2022 at 6:42

Jawali M's user avatar

Pekin

1

Копировать только значения

11.02.2013, 12:00. Показов 32753. Ответов 4

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

Добрый день.

Пишу макрос и на данный момент столкнулся со следующей проблемой: есть готовый код,с помощью которого я собираю информацию со всех листов на отдельном листе и не знаю что следует поправить в строке, чтобы вставлялись только значения. Выдержка из кода:

PureBasic
1
Sheets("Sheets"1).Range(sCopyAddress).Copy wsDataSheet.Cells(lLastRowMyBook, 1).Offset(, lCol)

Что надо изменить в этой строке, чтобы вставлялись только значения?

Programming

Эксперт

94731 / 64177 / 26122

Регистрация: 12.04.2006

Сообщений: 116,782

11.02.2013, 12:00

4

Скрипт

5468 / 1148 / 50

Регистрация: 15.09.2012

Сообщений: 3,514

11.02.2013, 12:14

2

Вариант № 1:

Visual Basic
1
2
3
4
5
6
Sub Procedure_1()
 
    Range("A1").Copy
    Range("B1").PasteSpecial Paste:=xlPasteValues
 
End Sub

Вариант № 2:

Visual Basic
1
2
3
4
5
Sub Procedure_2()
 
    Range("B1").Value = Range("A1").Value
 
End Sub

Примечание

С большими диапазонами ячеек быстрее работает PasteSpecial, чем второй вариант.



1



Pekin

11.02.2013, 12:26

3

@Скрипт

Спасибо за предлагаемые решения.

Моя проблема в том, что мне необходимо минимально изменить приведенный код, поскольку весь код громоздкий и используется множество команд (в приведенном коде, например, используется Offset) и вероятность проблем с новым кодом высока

Хотел бы увидеть предложение к конкретному коду (именно по нему не могу найти как выполнить данную задачу)

Watcher_1

356 / 162 / 27

Регистрация: 21.06.2011

Сообщений: 350

11.02.2013, 12:40

4

Visual Basic
1
2
Sheets("Sheets1").Range(sCopyAddress).Copy
wsDataSheet.Cells(lLastRowMyBook, 1).Offset(, lCol).PasteSpecial Paste:=xlPasteValues



1



2 / 2 / 0

Регистрация: 15.10.2019

Сообщений: 46

16.10.2019, 09:20

5

не работает…

пишет ошибку expected end of statement



0



This article covers everything you need to know about Excel VBA Copy methods.

By “Excel VBA Copy Methods”, I mean the different methods you can use to copy data from one range or worksheet to another.

In this article, I’m going to show you the fastest and most efficient way to copy and filter data no matter what your task is.

I say “Copy and Filter” because in most cases you will want to filter the data before you copy it. If you just want to copy data from one place to another then I cover that too.

The article is broken down as follows:

    1. Which Excel VBA Copy and Filter method is the fastest(section 6) – What the professionals know and how you can use it to your advantage.
    2. How to pick the best Excel VBA copy method for each task(section 7) – miss this and you’re wasting countless hours of your time.
    3. How to ensure your application runs at its optimal speed(section 8) – It’s not hard to do once you know the secret.
    4. The Copy and Filter methods explained with full code examples(sections 9 and 10) – Learn these methods and watch your macros run like magic.

Useful Links

The following are some related articles with more information about certain topics:

Ranges and Cells
Arrays
Dictionary
For Loop
For Each Loop
The Workbook object
The Worksheet object

Glossary

Range.Value2 – returns the underlying value in the call. Value is similar but slower and may not return the correct value if there is currency or data formatting. See this article for more information.

ADO – stands for ActiveX Database Objects. It is a library that allows us to run queries on databases. We can also use it with Excel spreadsheets.

The Webinar

Members of the Webinar Archives can access the webinar for this article by clicking on the image below.

(Note: Website members have access to the full webinar archive.)

vba copy autofilter

Download the Source Code and Data

In this article I will be using the following dataset:

Excel VBA Copy Dataset

Unless specified, most examples will use the 2 worksheets:

        • Transactions – contains the above data.
        • Report – the data will be copied to this worksheet.

How to use this article

In the next section you can see how the different VBA Copy Methods compare in terms of speed. This is vital when you are determining which one to use.

You can use section 4 to help you determine which method you should use based on your requirements.

Section 5 shows you the basic ground work you should have in your application.

The rest of the article provides a description and code example of each method.

Which Excel VBA Copy Method is the Fastest?

In this section you will see how the different methods compare in terms of speed.

Using the fastest method is vital when dealing with large amounts of data. As you can see from the results, some of the methods are incredibly slow.

I have run speed tests on the different methods to see which methods are the fastest for different types of tasks.

I ran the tests on 20000 , 100000 and 200000 records. You can see the average results for each method in milliseconds in the tables below:

Copying Data By Rows

VBA Copy Row Speed

Average time taken by each method in milliseconds

In this test, I filtered by rows that contain a given first name in the first column. About 1% of the records will match the criteria and will be copied. So for 20000 records, there will be 200 records copied.

You can see that using Autofilter and Advanced filter are the fastest methods for copying and filtering rows.

At the other end you can see that a For Loop with Range.Copy takes almost 215 times longer than using AutoFilter. I didn’t even run the 200,000 test for this as I expected it would take 15 minutes plus!

Copying Data By Individual Cells

VBA Copy Columns Speed

Average time taken by each method in milliseconds

In this tests we want to copy some of the cells but not the entire row. In most cases we need to copy the cells individually so this makes it more complicated than copying an entire row. This may seem counter intuitive at first that copying serveral cells requires more work than the copying the entire row.

Using the Advanced Filter is the clear winner here. Autofilter doesn’t have it’s own copy method so after filtering we have to copy the data using some other method.

Advanced Filter automatically copies the data and we can specify the columns we want to be copied. Advanced filter then takes care of the rest.

I have created two different methods of copying using AutoFilter for individual rows. The methods may be faster or slower depending on the number of columns:

        • The Copy Columns method would be slower with more columns to copy.
        • The Delete Columns method would be slower with more columns to delete.

Copying Data and Summing Totals

VBA Copy Summing Speeds

Average time taken by each method in milliseconds

In these tests we are summing data for a particular item. Summing means to get the total amount of a given column for a particular item. For example, getting the total volume for “Laptop Model A”.

There are 3 VBA copy methods that we can use to do this. These are:

        1. Pivot Tables
        2. For Loop with a Dictionary
        3. ADO(ActiveX Database Objects)

Using a Pivot Table is the fastest method for summing data. There is not much difference between the other two methods.

The downside of the Pivot Table is that the code may be slightly complex for a new user. ADO is very flexible but it does require knowledge of SQL(Structured Query Language).

Copying and Transposing Data

VBA Copy Transpose speeds

Average time taken by each method in milliseconds

Both methods here are pretty fast. If you are doing a large volume of transpose copies then Application.Transpose tends to be quicker than PasteSpecial Transpose.

Which Excel VBA Copy Method Should I Use?

With so many different methods, you may be feeling overwhelmed. Don’t worry, in this section I will provide a complete guide to selecting the correct Excel VBA copy method to use.

Note that you can download the source code for this post from the start or end of this post. This is an invaluable with of practicing the method shown here.

Straight Copy with no Filter

To copy without any filter use the copy by assignment method like this:

shWrite.Range("F1:G4").Value2 = shRead.Range("A1:B4").Value2

Excel VBA Copy

Filter columns(AND Logic) and Copy Rows

Advanced Filter is the fastest and easiest method to use if you want to filter by column values using AND logic:

e.g Item is “Laptop Model A” And Volume is greater than 20

Excel VBA Copy

Filter columns(OR Logic) and Copy Rows

Advanced Filter is the fastest method to do an OR filter. It’s not possible to do this with AutoFilter.

e.g Item is “Laptop Model A” Or Volume > 20″

Excel VBA Copy

Filter and Copy Individual Columns

Sometimes you will not want to copy the entire row. Instead you may want to copy individual columns. Advanced Filter is fastest VBA Copy method for doing this.

e.g. return the columns Item, Volume and Sales where Item is “Laptop Model A” And Volume > 20″
Excel VBA Copy

Sum totals for individual items

If you want to get the total amount for each item then using a Pivot Table is faster than the other two methods. ADO(ActiveX Database Objects) is slightly faster than using the For Loop with the Dictionary.

Using a Pivot Table is very flexible. Once you create the table it pretty easy to display the data in many different ways. The downside is that the code may be a bit complex for a VBA beginner.

ADO is much more flexible but requires some knowledge of SQL(a database query language). It also requires using an external library which may be a bit advanced for a VBA beginner.

Using the For Loop and a Dictionary requires more code and is less flexible. But it is still pretty fast for up to 200,000 records and doesn’t require any SQL or external libraries.

Transpose

Transposing means to copy data so that the rows become columns and the columns become rows

Excel VBA Copy

There are two methods of transposing data:

  1. Range.Copy and Transpose using PasteSpecial.
  2. Assignment and Transpose using Application.Transpose.

There is not much difference between these in terms of speed. However, if your application is doing multiple transpose operations then Range.Copy tends to be much slower.

Before You Start Copying and Filtering

No matter which Excel VBA Copy method we use there are some tasks we must perform first.

These include getting the range of data, turning off certain Excel functionality etc.

We will look at these tasks in this section.

The first thing we will look at is the mistake that most VBA beginners make – using Select.

Never Use Select

When copying data in Excel VBA, don’t use Select – ever!

A big mistake that new VBA users make is thinking that they need to select the cell or range before they copy it.

For example

shRead.Activate
shRead.Range("A1").Select
shWrite.Activate
Selection.Copy ActiveSheet.Range("H1")

Keep these two important things in mind before you use VBA to copy data:

  1. You don’t need to select the cell or range of cells.
  2. You don’t need to select or activate the worksheet.

You will see Select used in many places online. But you don’t need to use it for copying cells – ever!

Speed Up Your Code

If you want your code to run fast then it is important to turn off certain VBA functionality at the start of our code. We then turn it back on, at the end of our code.

We can use the following subs to do this:

' Procedure : TurnOffFunctionality
' Source    : www.ExcelMacroMastery.com
' Author    : Paul Kelly
' Purpose   : Turn off automatic calculations, events and screen updating
' https://excelmacromastery.com/
Public Sub TurnOffFunctionality()
    Application.Calculation = xlCalculationManual
    Application.DisplayStatusBar = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
End Sub

' Procedure : TurnOnFunctionality
' Source    : www.ExcelMacroMastery.com
' Author    : Paul Kelly
' Purpose   : turn on automatic calculations, events and screen updating
' https://excelmacromastery.com/
Public Sub TurnOnFunctionality()
    Application.Calculation = xlCalculationAutomatic
    Application.DisplayStatusBar = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

We can use them like this

Sub Main()

    ' Turn off at the start
    TurnOffFunctionality
    
    ' Your code here
    
    ' Turn back on at the end
    TurnOnFunctionality

End Sub

Sometimes when you run your code, it won’t reach the TurnOnFunctionality code.

This could because of an error or because you stop the code at a certain point and don’t restart.

If this happens you can turn everything on again by clicking in the TurnOnFunctionality sub and pressing F5.

Get the correct worksheet

When copying data, we need to specify the range which we will copy from. When using VBA we need to select the worksheet before we can select the range. There are many ways of selecting the worksheet which can be confusing.

I have broken it down into three scenarios:

  1. The worksheet is in the same workbook as the code.
  2. The worksheet is in a different workbook but we only want to read from it.
  3. The worksheet is in a different workbook but we want to write to it.

You can see the code for each of these scenarios in the next subsections:

The worksheet is in the current workbook

The worksheet is in the current workbook so we can use either:

  1. The code name of the worksheet.
  2. The worksheet name: ThisWorkbook.Worksheets(“worksheet name”).

VBA Code name

In the screenshot below we have changed the codename and worksheet name:
Worksheet Code name
We would use the following code to reference the sheet

' https://excelmacromastery.com/
Sub WorksheetCodeName()

    Dim shRead As Worksheet
    
    ' Use the code name
    Set shRead = CodeName
    
    ' OR
    
    ' Use the worksheet name
    Set shRead = ThisWorkbook.Worksheets("SheetName")
    
End Sub

The worksheet is in a different workbook – reading only

If the worksheet is in a different workbook than the code, then we need to open the workbook. In this case we will only be reading from the worksheet so we can simply make it read-only.

Then it doesn’t matter if it’s already open. Making it read-only also prevents us from accidentally changing the data in the workbook.

' WorksheetRead()
'
' HOW TO RUN:
' 1. Place this code in a new workbook and save it as a .xlsm file.
' 2. Create a workbook called Writedata.xlsx and place it in the same
'    folder as the workbook you just created.
' 3. Place any value in cell A1 on sheet1 from Writedata.xlsx.
' 4. Run the code(Press F5).
'
' RESULT: The value in cell A1 on sheet1 from Writedata.xlsx
'         will be displayed in a message box.
' https://excelmacromastery.com/
Sub WorksheetRead()

    ' Get the full filename and path of "WriteData.xlsx"
    Dim sFilename As String
    sFilename = _
         ThisWorkbook.Path & Application.PathSeparator & "WriteData.xlsx"

    ' Open workbook as read-only and store in variable wk
    Dim wk As Workbook
    Set wk = Workbooks.Open(sFilename, ReadOnly:=True)

    ' Store the worksheet Sheet1 in the variable shRead
    Dim shRead As Worksheet
    Set shRead = wk.Worksheets("Sheet1")
    
    ' Print the value from the worksheet cell A1
    If IsEmpty(shRead.Range("A1").Value2) Then
        MsgBox "No value was found in cell Sheet1:A1 of WriteData.xlsx."
    Else
        MsgBox "The value found in Sheet1:A1 is of WriteData.xlsx is: " _
            & shRead.Range("A1").Value2
    End If
    
    wk.Close SaveChanges:=False
End Sub

The worksheet is in a different workbook – reading and writing

Just like the last example, the worksheet is in a different workbook than the code. In this case we want to write to the worksheet in the workbook.

We first check if the workbook is already open, and if so we display a message informing the user to close it before running the code again:

' WorksheetWrite()
'
' HOW TO RUN:
' 1. Place this code in a new workbook and save it as a .xlsm file.
' 2. Create a workbook called Writedata.xlsx and place it in the same
'    folder as the workbook you just created.
' 3. Click in the WorksheetWrite sub and press F5 to run the code.
' 4. Open the Writedata.xlsx and check if the value was correctly
'    written to cell A1 in sheet1.
'
' RESULT: If the file is already open then and error message will be
'         displayed to the user asking them to close the file.
'         If the file is not already open then the value 99 will be
'         written to cell A1 on sheet1 of Writedata.xlsx.
' https://excelmacromastery.com/
Sub WorksheetWrite()

    Dim sFilename As String
    sFilename = _
         ThisWorkbook.Path & Application.PathSeparator & "WriteData.xlsx"

    ' Ensure that the workbook is not already open
    If IsWorkBookOpen("WriteData.xlsx") Then
        MsgBox "Please close workbook " & sFilename & " and try again."
        Exit Sub
    End If

    ' Open workbook and store in variable wk
    Dim wk As Workbook
    Set wk = Workbooks.Open(sFilename, ReadOnly:=False)

    ' Store the worksheet Sheet1 in the variable shRead
    Dim shRead As Worksheet
    Set shRead = wk.Worksheets("Sheet1")
    
    ' Write a value to the worksheet cell A1
    shRead.Range("A1").Value2 = Now

    ' Close the file when finished with it
    wk.Close saveChanges:=True
    
End Sub

' Helper function to check if workbook is already open
Function IsWorkBookOpen(strBookName As String) As Boolean
    
    Dim oBk As Workbook
    
    On Error Resume Next
    Set oBk = Workbooks(strBookName)
    On Error GoTo 0
    
    If Not oBk Is Nothing Then
        IsWorkBookOpen = True
    End If
    
End Function

Now that we can get any worksheet we require, we can focus on how to copy the data.

Get the Data Range

In many online Excel VBA Copy examples, you will see a fixed range like Range(“A1:D6”).

When dealing with real-world applications it is rare that the data will be of a fixed size.

For example, imagine you stored student marks for each class in a worksheet. Obviously, each class will have a different number of students. Each time you read a worksheet you cannot be sure in advance what the range will be.

Your code should be able to figure out the full range of data, even if the size changes each time it runs.

There are two types of data:

  1. Rectangular data where the data is in a list of rows with no blank rows or columns.
  2. Fragmented data where the data may contain blank rows or columns.

Using CurrentRegion

The CurrentRegion property of Range is very useful for returning the entire range of data. CurrentRegion returns all the adjacent data from a given range.

You can see the CurrentRegion in Excel by selecting a cell and pressing Ctrl + Shift + *.

In the screenshot below you can see two groups of adjacent data. By selecting any cell with data and pressing Ctrl + Shift + *, Excel will select all the data in that group. This is the current region.

VBA CurrentRegion

We can also use the CurrentRegion in VBA:

The Current region will work as long as there are no blank rows or columns between the data.

' CurrentRegion()
'
' HOW TO RUN:
' 1. Create a worksheet called "CurrentRegion".
' 2. Add data to all cells in the range B3:D14.
' 3. Run this sub(F5).
'
' RESULT: The current region for B3, C9 and D14 will
'         be displayed in the Immediate Window(Ctrl + G).
' https://excelmacromastery.com/
Sub CurrentRegion()

     ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("CurrentRegion")
    
    Dim rg1 As Range, rg2 As Range, rg3 As Range
    
    ' Get the current region
    ' All the below examples with return B3:D14
    Set rg1 = shRead.Range("B3").CurrentRegion
    Set rg2 = shRead.Range("C9").CurrentRegion
    Set rg3 = shRead.Range("D14").CurrentRegion
    
    ' Display the addresses to the Immediate Window(Ctrl + G)
    Debug.Print "B3 CurrentRegion is: " & rg1.Address
    Debug.Print "C9 CurrentRegion is: " & rg2.Address
    Debug.Print "D14 CurrentRegion is: " & rg3.Address

End Sub

Using End with xlUp and xlToLeft

If there are blank rows and/or blank columns then CurrentRegion will not return all the data.

For example, in the data shown here there are blank rows and a blank column:

Excel VBA Copy

We need to get the last row with data and/or the last column with data. We can then use these cells to build our Range.

' UseLastRowCol()
'
' HOW TO RUN:
' 1. Create a worksheet called "Fragmented".
' 2. Add any data to the cells A1:E13 but make sure you
'    have the same blank cells as in the above screenshot.
' 3. Run this sub(F5)
'
' RESULT: The range A1:E13 will be displayed in the
'         Immediate Window(Ctrl + G).
' https://excelmacromastery.com/
Sub UseLastRowCol()

     ' Get the worksheet
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Fragmented")
    
    Dim lastRow As Long, lastCol As Long
        
    ' Get the last cell with data in column A
    lastRow = shRead.Cells(shRead.Rows.count, 1).End(xlUp).row
    
    ' Get the last cell with data in row 1
    lastCol = shRead.Cells(1, shRead.Columns.count).End(xlToLeft).column
    
    Dim rg As Range
    With shRead
        ' Get the full range of data from A1 to last row and column
        Set rg = .Range(.Cells(1, 1), .Cells(lastRow, lastCol))
    End With
    
    ' Print the rg address(i.e. A1:E13) to the Immediate Window(Ctrl + G)
    Debug.Print "The range of data is: " & rg.Address

End Sub

Excel VBA Copy Methods

In this section we are going to look at the copying methods.

There are three ways of copying data in VBA:

  1. The Range.Copy function
  2. The assignment operator(i.e. equals sign)
  3. The Range.CopyFromRecordset function(ADO only)

In the following sub sections we will look at these in turn. We will be using these Excel VBA copy methods with the different filter methods.

Range.Copy

We can use the Copy method of the Range to copy data.

' https://excelmacromastery.com/
Sub SimpleCopy()

     ' Get the worksheet
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    ' Copy the data from A1:D1 to H1:K1
    shRead.Range("A1:D1").Copy Destination:=shRead.Range("H1")
    
    ' Using Destination is optional
    shRead.Range("A1:D1").Copy shRead.Range("H1")

End Sub

We can copy between worksheets using this Excel VBA method. The only thing that will change in the code is the worksheets that we use.

' UsingRangeCopy()
'
' HOW TO RUN:
' 1. Create a workbook called Data.xlsx and place it in the same
'    folder as the workbook with the code.
' 2. Rename sheet1 to "CustomerData".
' 3. Add data to the range A1:D19.
' 4. Save the file.
' 5. Have a worksheet called "Report" in the current workbook
' 6. Click in this sub and press F5 to run
'
' RESULT: The data in the range A1:D19 from the CustomerData
'          worksheet in Data.xlsx will have been copied to
'         the Report worksheet in the current workbook.
' https://excelmacromastery.com/
Sub UsingRangeCopy()

    Dim sFilename As String
    sFilename = _
            ThisWorkbook.Path & Application.PathSeparator & "Data.xlsx"

    ' Open a workbook called Data.xlsx from the current folder
    Dim wk As Workbook
    Set wk = Workbooks.Open(sFilename, ReadOnly:=True)
    
    ' shRead stores the worksheet CustomerData
    ' shWrite store the Report sheet
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = wk.Worksheets("CustomerData")
    Set shWrite = ThisWorkbook.Worksheets("Report")

    ' Clear the data from the report worksheet
    shWrite.Cells.Clear

    ' Copy from CustomerData to Report
    shRead.Range("A1:D19").Copy shWrite.Range("A1")
    
    ' Activate the report worksheet
    shWrite.Activate
    
    ' Close the "Data.xlsx" workbook
    wk.Close

End Sub

This Range.Copy method copies everything. In other words it will copy values, formulas, formats etc.

If we only want to copy the data we can use the PasteSpecial property of Range.

Using PasteSpecial

We can use PasteSpecial with Range.Copy to paste the values only

' https://excelmacromastery.com/
Sub UsingPasteSpecial()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    ' Clear any existing data from report
    shWrite.Cells.Clear
    
    ' This will copy the values only
    shRead.Range("A1:E13").Copy
    shWrite.Range("A1").PasteSpecial xlPasteValues
    
End Sub

You can also copy formats, formulas and comments using PasteSpecial. You can read more about it here.

Range.Copy and Transpose

Transposing data means that the rows become columns and the columns become rows:

Excel VBA Copy
We can use the Range.PasteSpecial function to transpose data:

' https://excelmacromastery.com/
Sub RangeCopy_Transpose()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transpose")
    
    ' Clear any existing data
    shRead.Range("F1:I2").Clear
    
    ' This will copy from A1:B4 to F1:I2
    shRead.Range("A1:B4").Copy
    shRead.Range("F1").PasteSpecial Transpose:=True
    
End Sub

Copying by Assignment

We can assign values using the equals sign in VBA.

We can copy values from one range to another using assignment.

The value to the right of the equals is placed in cell to the left of the equals as you can see in these examples:

' https://excelmacromastery.com/
Sub AssignExamples()"
    
    ' Copy a value to a cell
    Range("A1").Value2 = 56
    Range("A2").Value2 = "Mary"
    
    ' Copy cell to cell
    Range("G1").Value2 = Range("F1").Value2
    Range("G2").Value2 = Range("F2").Value2
    
    ' Copy multiple cells - the source and destination range
    ' must be the same size
    Range("A1:C5").Value2 = Range("D1:F5").Value2
    
End Sub

The code below shows an example of copying the data from the data shown in the screenshot at the start of this section.

We will copy the range A1:D11 on the worksheet Transactions to the range A1:D11 on worksheet Report.

' UsingAssignment()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' RESULT: The data will be copied from the Transaction worksheet
'         to the report worksheet
' https://excelmacromastery.com/
Sub UsingAssignment()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "dd/mm/yyyy"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        .Columns(4).NumberFormat = "0"
        .Columns(5).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Copy the data
    shWrite.Range("A1:D11").Value2 = shRead.Range("A1:D11").Value2

End Sub

The key thing to remember about assigning values is that the destination must be the same size as the source.

If it is smaller than the source range then not all the data will be written.

If it is bigger than the source then there will be #N/A errors in the extra cells.

Assignment with Transpose

Excel VBA Copy

We can use the WorksheetFunction.Transpose function to transpose using the assignment operator(i.e. equals sign) to copy:

' https://excelmacromastery.com/
Sub TransposeAssign()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transpose")
    
    ' Clear any existing data
    shRead.Range("F1:I2").Clear
    
    shRead.Range("F1:I2").Value2 = _
            WorksheetFunction.Transpose(shRead.Range("A1:B4").Value2)

End Sub

Using Range.CopyFromRecordset

A Recordset is the collection of data we get when we run a database query.

We can run database queries on worksheet data using ADO (ActiveX Data Objects).

We will look at this Excel VBA Copy method in more detail in the ADO section below.

Excel VBA Filter Methods

Whenever you are copying data in VBA you will almost always apply some type of filter. This could be selection rows based on certain criteria or it could be summing data.

In this section, we are going to look at the various ways of filtering data. If you want working examples using the Filter methods then you can download the source code at the top or bottom of this post.

Filtering using For and If

The most common way of filtering data in VBA is using a For Loop and If Statement.

This is the least flexible and most long-winded method of filtering data in VBA. By long-winded, I mean that it requires lots of code compared to the other methods.

Using For and IF is the method you will see used the most in examples or in legacy VBA code.

We use the For Loop to read through data one row at a time. For each row, we will use the If Statement to check for certain criteria.

Here is a simple outline:

' https://excelmacromastery.com/
Sub ForIf_Basic()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    Dim i As Long
    For i = 1 To 13
        If shRead.Range("A" & i).Value2 = "Laptop Model A" Then
            ' Code for copying here
        End If
    Next i
    
End Sub

We can use both And and Or operators in the if statement.

Here are some examples:

If Item = "Laptop Model A" And Volume > 20 Then

If Item = "Laptop Model A" Or Volume > 20 Then

If Item = "Laptop Model A" Or Item = "Laptop Model B" Then

How to Copy Rows

The following code reads through the data. It checks each line to see if the first cell contains “Laptop Model A” .

If the row with these criteria is found, the row will be copied to the output worksheet.

The first version of the code below will use Range.Copy and the second will use the assign copy method:

' For_RangeCopy()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' https://excelmacromastery.com/
Sub For_RangeCopy()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    Dim shWrite As Worksheet
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    With shWrite

        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "dd/mm/yyyy"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        .Columns(4).NumberFormat = "0"
        .Columns(5).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Read through the data
    Dim i As Long, row As Long
    row = 1
    For i = 1 To rg.Rows.count
        
        If rg.Cells(i, 1).Value2 = "Laptop Model A" Or i = 1 Then
            
            ' Copy using Range.Copy
            rg.Rows(i).Copy
            shWrite.Range("A" & row).PasteSpecial xlPasteValues
            
            ' move to the next output row
            row = row + 1
            
        End If
        
    Next i
    
End Sub
' For_Assign()
'
' Requirements:
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' https://excelmacromastery.com/
Sub For_Assign()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    Dim shWrite As Worksheet
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    With shWrite

        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "dd/mm/yyyy"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        .Columns(4).NumberFormat = "0"
        .Columns(5).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Read through the data
    Dim i As Long, row As Long
    row = 1
    For i = 1 To rg.Rows.count
        
        If rg.Cells(i, 1).Value2 = "Laptop Model A" Or i = 1 Then
           
            ' Get the destination range
            Dim rgDest As Range
            Set rgDest = _
                shWrite.Range("A" & row).Resize(1, rg.Columns.count)
            
            ' Copy using Assignment
            rgDest.Value2 = rg.Rows(i).Value2
            
            ' move to the next output row
            row = row + 1
            
        End If
        
    Next i
    
End Sub

In the above code, we use Range.Resize when copying by assignment. You can read more about this here.

Using an Array to Read Faster

If you are reading through a lot of data then reading directly from the worksheet range is very slow.

It is much quicker to copy the data to an array and then read through that array. Then when we are finished, we write the array back to the worksheet

We cannot use Range.Copy because it obviously can only be used on an actual range.

Let’s update our last example so it will use an array:

' https://excelmacromastery.com/
Sub ForUsingArray_Assign()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    Dim shWrite As Worksheet
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    ' Copy the range to an array
    Dim arr As Variant
    arr = shRead.Range("A1").CurrentRegion.Value2
    
    With shWrite
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "dd/mm/yyyy"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        .Columns(4).NumberFormat = "0"
        .Columns(5).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Read through the data
    Dim i As Long, j As Long
    Dim row As Long, Columns As Long
    row = 1
    For i = LBound(arr) To UBound(arr)
        
        ' If "Laptop Model A" or header row then copy
        If arr(i, 1) = "Laptop Model A" Or i = 1 Then
            
            ' Copy each column
            For j = LBound(arr, 2) To UBound(arr, 2)
                shWrite.Cells(row, j).Value2 = arr(i, j)
            Next j
            
            ' move to the next output row
            row = row + 1
            
        End If
        
    Next i
    
End Sub

We could also use the Worksheet Function Index to copy. However, it is incredibly slow. In the speed tests it was over 4000 times slower than using the AutoFilter.

' Copy using Index
Columns = UBound(arr, 2)
shRepAssign.Range("A" & row).Resize(1, Columns).Value2 = _
        Application.WorksheetFunction.Index(arr, i, 0)

Copying Individual Fields

Copying individual items in a row is actually very straightforward.

We simply copy each cell individually:

' For_Assign_Individual()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' https://excelmacromastery.com
Sub For_Assign_Individual()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    Dim shWrite As Worksheet
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    With shWrite
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Read through the data
    Dim i As Long, rgWrite As Range, row As Long
    row = 1
    For i = 1 To rg.Rows.count
    
        ' If "Laptop Model A" or the header row
        If rg.Cells(i, 1).Value2 = "Laptop Model A" Or i = 1 Then
    
            ' Copy from column A to column A
            shWrite.Cells(row, 1).Value2 = rg.Cells(i, 1).Value2
            
            ' Copy from column E to column B
            shWrite.Cells(row, 2).Value2 = rg.Cells(i, 5).Value2
            
            ' Move to the next row for writing
            row = row + 1
        
        End If
        
    Next i

End Sub

The method below using Range.Copy – it is very slow so it is best to avoid using it.

' WARNING: DON'T USED THIS METHOD - IT IS INCREDIBLY SLOW
' https://excelmacromastery.com/
Sub For_RangeCopy_Individual()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    Dim shWrite As Worksheet
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    ' Read through the data
    Dim i As Long, rgWrite As Range, row As Long
    row = 1
    For i = 1 To rg.Rows.count
    
        ' If "Laptop Model A" or the header row
        If rg.Cells(i, 1).Value2 = "Laptop Model A" Or i = 1 Then
    
            ' Copy using Range.Copy
            rg.Cells(i, 1).Copy
            shWrite.Range("A" & row).PasteSpecial xlPasteValues
            
            rg.Cells(i, 5).Copy
            shWrite.Range("B" & row).PasteSpecial xlPasteValues
            
            ' Move to the next row for writing
            row = row + 1
        
        End If
        
    Next i

In this example we use an array with the For loop to copy. It is the fastest way to copy individual columns using the For loop but it is still way slower than using the AutoFilter.

' ForUsingArray_Assign_Individual()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' https://excelmacromastery.com/
Sub ForUsingArray_Assign_Individual()

    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    Dim shWrite As Worksheet
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Get the range
    Dim arr As Variant
    arr = shRead.Range("A1").CurrentRegion.Value2
    
    ' Read through the data
    Dim i As Long, rgWrite As Range, row As Long
    row = 1
    For i = LBound(arr) To UBound(arr)
    
        ' If "Laptop Model A" or the header row
        If arr(i, 1) = "Laptop Model A" Or i = 1 Then
    
            ' Copy from column A to column A on the report worksheet
            shWrite.Cells(row, 1).Value2 = arr(i, 1)
            
            ' Copy from column E to column B on the report worksheet
            shWrite.Cells(row, 2).Value2 = arr(i, 5)
            
            ' Move to the next row for writing
            row = row + 1
        
        End If
        
    Next i

End Sub

If the original data has a lot of columns and you only require a few of them, then this method may be as fast as AutoFilter but you will need to test both methods.

Summing Totals

In the data shown below we want to get the total sales for each item:
Excel VBA Copy

Our result should look like this:

Excel VBA Copy

Summing data like this is much trickier than simply filtering data.

For each item, we need to store a running total somewhere. Each time we read an item we need to either create a new entry or add the current sales amount to the existing entry.

The easiest way to do this is to use a Dictionary.

You can see the code to do this below. You will notice that it is split into two parts:

  • In part one we read the values from the worksheet to the dictionary.
  • In part two we write the values from the dictionary to the worksheet.
' ForDictionary_Assign_Sum()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' IMPORTANT: Add the Dictionary reference to use the dictionary
'            Tools->Reference and check "Microsoft Scripting Runtime"
' https://excelmacromastery.com/
Sub ForDictionary_Assign_Sum()

    ' PART 1 - Read the data
    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    ' Create the dictionary
    Dim dict As New Dictionary
    
    ' Read through the data
    Dim i As Long, Item As String, Sales As Long
    For i = 2 To rg.Rows.count
        ' Store the values in a variable
        Item = rg.Cells(i, 1).Value2
        Sales = rg.Cells(i, 5).Value2
        
        ' The item will be automatically added if it doesn't exist
        dict(Item) = dict(Item) + Sales
    Next i
    
    ' PART 2 - Write the data
    Dim shWrite As Worksheet
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "$#,##0;[Red]$#,##0"
        
        ' Write header
        .Cells(1, 1).Value2 = "Item"
        .Cells(1, 2).Value2 = "Sales"
        
    End With
    
    Dim key As Variant, row As Long
    row = 2
    ' Read through each item in the Dictionary
    For Each key In dict.Keys
    
        shWrite.Cells(row, 1) = key
        shWrite.Cells(row, 2) = dict(key)
        
        row = row + 1
    Next key
    
End Sub
Summing Multiple Totals

In the previous example we were summing one item. This time we are going to sum the volume and the price fields.

The Dictionary stores a key and a value. If we want to store more than one value then we use a class module.

In the next example, I have split up the code into procedures to make it easier to read and to be more flexible:

' Class Module: clsSales
Public Items As String
Public Volume As Long
Public Sales As Long
' https://excelmacromastery.com/
Sub CreateReport()

    ' Declare a Dictionary variable
    Dim dict As Dictionary

    ' Read the data to the Dictionary
    Set dict = ReadData
    
    ' Write the Data from the Dictionary to the Worksheet
    WriteData dict

End Sub

' Read the data from the worksheet into a Dictionary
'
' IMPORTANT: Add the Dictionary reference to use the dictionary
' Tools->Reference and check "Microsoft Scripting Runtime"
' https://excelmacromastery.com/
Private Function ReadData() As Dictionary
    
    ' Create the dictionary
    Dim dict As New Dictionary
    
    ' Get the worksheets
    Dim shRead As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    ' Read through the data
    Dim i As Long, Item As String, Volume As Long, Sales As Long
    Dim oSales As clsSales, oSalesCurrent As clsSales
    For i = 2 To rg.Rows.count

        Item = rg.Cells(i, 1).Value2
        Volume = rg.Cells(i, 4).Value2
        Sales = rg.Cells(i, 5).Value2
        
        ' Check if the Item has already been added
        If dict.Exists(Item) = False Then
            ' Add Item and new class module object
            Set oSales = New clsSales
            dict.Add Item, oSales
        End If
        
        ' Get the current item
        Set oSalesCurrent = dict(Item)
        
        ' Update the data
        With oSalesCurrent
            .Volume = .Volume + Volume
            .Sales = .Sales + Sales
        End With
        
    Next i
    
    Set ReadData = dict
    
End Function

' Write the summed data to the worksheet
' https://excelmacromastery.com/
Sub WriteData(dict As Dictionary)

    Dim shWrite As Worksheet
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "0"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        
        ' Write header
        .Cells(1, 1).Value2 = "Item"
        .Cells(1, 2).Value2 = "Volume"
        .Cells(1, 3).Value2 = "Sales"
        
    End With
    
    Dim key As Variant, oData As clsSales, row As Long
    row = 2
    
    ' Read through each item in the Dictionary
    For Each key In dict.Keys
    
        Set oData = dict(key)
        
        shWrite.Cells(row, 1) = key
        shWrite.Cells(row, 2) = oData.Volume
        shWrite.Cells(row, 3) = oData.Sales
        
        row = row + 1
        
    Next key
    
End Sub

Using AutoFilter

The methods that we have looked at so far require a lot of code.

There is actually a much simpler way of reading and filtering data. We can use the AutoFilter. This is the filter we use in Excel to filter data. Once the data is filtered we can easily copy and paste it.

VBA AutoFilter Excel

VBA AutoFilter Excel

In terms of speed, AutoFilter came out on top in the speed tests that I performed. It is the best one to use for selecting or copying either rows or individual columns.

These are some examples of using AutoFilter in VBA:

' Field 1 = "Ipsum Corporation" and field 3 >=50
rg.AutoFilter Field:=1, Criteria1:="Ipsum Corporation" _ 
       , Operator:=xlAnd, Field:=3, Criteria1:=">=50"

' Field 1 starts with "I"
rg.AutoFilter Field:=1, Criteria1:="I*"

' Field 1 ends with "Ltd"
rg.AutoFilter Field:=1, Criteria1:="*Ltd"

' Field 1 contains "Su"
rg.AutoFilter Field:=1, Criteria1:="*Su*"

' Field 1 does not contain "Su"
rg.AutoFilter Field:=1, Criteria1:="<>*Su*"

' Get the top 10 items based on the value in Field 3
rg.AutoFilter Field:=3, Criteria1:="10", Operator:=xlTop10Items

' Field 3 is in the value list of 100,17 and 2
rg.AutoFilter Field:=3, Criteria1:=Array("100", "17", "2") _
     , Operator:=xlFilterValues

Now that we understand the basics, let’s look at some practical examples of using AutoFilter.

How to Copy Rows

Using AutoFilter is the fastest way to copy filtered rows of data.

Let’s use AutoFilter to copy all the rows that contain “Laptop Model A”.

The following code shows how to do this:

' AutoFilter_RangeCopy_Row()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' https://excelmacromastery.com/
Sub AutoFilter_RangeCopy_Row()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "dd/mm/yyyy"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        .Columns(4).NumberFormat = "0"
        .Columns(5).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    ' Remove any existing filters
    rg.AutoFilter
    
    ' Apply the Autofilter
    rg.AutoFilter Field:=1, Criteria1:="Laptop Model A"
    
    ' Copy the data using Range Copy
    shRead.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
    shWrite.Range("A1").PasteSpecial xlPasteValues
    
    ' Remove any existing filters
    rg.AutoFilter
    
    ' Active the output sheet so it is visible
    shWrite.Activate

End Sub

We can also use assign to copy the data. It is almost as fast but requires slightly more complex code.

It is actually not necessary to use this code but I am including it for completeness:

' AutoFilter_Assign_Row()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' https://excelmacromastery.com/
Sub AutoFilter_Assign_Row()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "dd/mm/yyyy"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        .Columns(4).NumberFormat = "0"
        .Columns(5).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
        
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    ' Remove any existing filters
    rg.AutoFilter
    
    ' Apply the Autofilter
    rg.AutoFilter Field:=1, Criteria1:="Laptop Model A"
     
    ' Get the read range
    Dim rgRead As Range
    Set rgRead = _
        shRead.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible)

    Dim rgRow As Range, row As Long
    row = 1
    ' Read through each row
    For Each rgRow In rgRead.Rows
        
        ' Copy the row
        shWrite.Cells(row, 1).Resize(1, rgRead.Columns.count).Value2 _
                   = rgRow.Value2
        
        ' move to the next output row
        row = row + 1
        
    Next rgRow
    
    ' Remove any existing filters
    rg.AutoFilter

End Sub

Using AutoFilter code is much easier to write because we don’t need a For Loop and an If Statement. We can filter the data in just one line.

Copying Individual Fields

Copy individual cells is more long winded than copying the row.

It may seem counter intuitive at first but we have to specify the cells we are copying. With a row, we can just copy the entire thing.

There are 2 main ways we can do this:

  1. Copy the individual columns from the filtered data.
  2. Copy filtered data and delete the columns that are not required.
Copy the Columns

The first method is the most basic way to do this. We simple copy each column that we require

' AutoFilter_CopyColumns()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' Details: Use AutoFilter with Range.Copy to filter and copy individual
'          column data.
'          The resulting data should contain the columns Items and Sales.
' https://excelmacromastery.com/
Sub AutoFilter_CopyColumns()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    ' Remove any existing filters
    rg.AutoFilter
    
    ' Apply the Autofilter
    rg.AutoFilter Field:=1, Criteria1:="Laptop Model A"
    
    Dim arr As Variant
    arr = Array(1, 5)
    
    ' Remove unnecessary columns
    Dim i As Long, column As Long
    column = 1
    For i = LBound(arr) To UBound(arr)
        rg.Cells(1, arr(i)).EntireColumn.Copy
        shWrite.Cells(1, column).PasteSpecial xlPasteValues
        column = column + 1
    Next i
    
    ' Remove any existing filters
    rg.AutoFilter
    
End Sub
Copy and Delete Unwanted Columns

The second method is the fastest for data with a reasonable number of columns. We simply copy all the data and delete the columns we don’t need:

' AutoFilter_DeleteColumns()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' Details: Use AutoFilter with Range.Copy to filter and copy individual
'          column data.
'          Copy the entire data and then delete the unnecessary columns.
'          The result should be Item and Sales columns only.
' https://excelmacromastery.com/
Sub AutoFilter_DeleteColumns()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
    
    ' Remove any existing filters
    rg.AutoFilter
    
    ' Apply the Autofilter
    rg.AutoFilter Field:=1, Criteria1:="Laptop Model A"

    ' Copy the filtered data to the output worksheet
    rg.SpecialCells(xlCellTypeVisible).Copy
    shWrite.Range("A1").PasteSpecial xlPasteValues
    
    ' Remove unnecessary columns
    Dim rgOut As Range, i As Long
    Set rgOut = shWrite.Range("A1").CurrentRegion
    For i = rgOut.Columns.count To 1 Step -1
        Select Case i
            Case 1, 5
            Case Else
                rgOut.Columns(i).EntireColumn.Delete
        End Select
    Next i
    
    ' Remove any existing filters
    rg.AutoFilter
    
End Sub

Summing Totals

We cannot use the Filters to sum data. We need to use either a For Loop with a Dictionary or ADO.

Using Advanced Filter

See the main article on Advanced Filter here

Advanced Filter has 4 major advantages over AutoFilter:

  1. It has more advanced filtering.
  2. It can automatically copy the results to another range.
  3. When copying the results it automatically copies the format.
  4. You can select specific columns and the order of columns.

The AutoFilter has a drawback when filtering data. We cannot use the logic:

If Value1 is in Column1 OR Value2 is in Column2

For example, we cannot say we want all the records where the Item is “Laptop Model A” or Volume > 6.

We can use the AdvancedFilter to get around this. AdvancedFilter is very similar to AutoFilter. The major difference is that the criteria must be a range on the worksheet.

The screenshot below shows how to use AND. It means item is “Laptop Model A” and Volume > 16. The Criteria Range for this must be G1:K2.

Excel VBA Copy

The result of using these criteria is:

Excel VBA Copy

The screenshot below shows how to use OR. It means item is “Laptop Model A” OR Volume > 16. The Criteria Range for this must be G1:K3.

Excel VBA Copy

The result of using these criteria is

Excel VBA Copy

The code below shows how we create an advanced filter for the above and criteria.

Range("A1:D12").AdvancedFilter Action:=xlFilterInPlace _
     , CriteriaRange:=Range("G1:K2")

For OR it would be

Range("A1:D12").AdvancedFilter Action:=xlFilterInPlace _
     , CriteriaRange:=Range("G1:K3")

Important: You will notice here that the Criteria range is different for the two examples. Make sure that you don’t have a blank line in the criteria (e.g. G1:K4 instead of G1:K3). This will result in all records being returned i.e. no filter being applied.

How to Copy Rows

Advanced filter will not only automatically copy the results for us. It is also the fastest VBA method for copying and filter in most scenarios.

We simple set the Action parameter to xlFilterCopy and set the CopyToRange parameter to the output range:

' ----------------------------------------------------------------
' Procedure Name: AdvancedFilterExample
' Purpose: An example of using the Advanced Filter to copy rows
'
' Result: The result depends on the values set in the criteria range
'         G to K columns on the "Transaction Filter" worksheet.
' Website: https://excelmacromastery.com/
' ----------------------------------------------------------------
' https://excelmacromastery.com/
Sub AdvancedFilterExample()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions Filter")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    ' Clear any existing data
    shWrite.Cells.Clear

    ' Remove the any existing filters
    If shRead.FilterMode = True Then
        shRead.ShowAllData
    End If
    
    ' Get the source data range
    Dim rgData As Range, rgCriteria As Range
    Set rgData = shRead.Range("A1").CurrentRegion
    
    ' IMPORTANT: Do not have any blank rows in the criteria range
    Set rgCriteria = shRead.Range("G1:K2")
   
    ' Apply the filter
    rgData.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rgCriteria _
                , CopyToRange:=shWrite.Range("A1")

 End Sub

Copying Individual Fields

To copy specific columns is pretty simple. We place the columns in our output range and Advanced Filter will only write data to this columns.

For example, If we only want the Item and Sales columns then we need to make two changes to Advanced filter example above:

  1. Set the Headers before we run advanced filter.
  2. Set the CopyToRange parameter to include the Range of headers.

We can see the updated code here:

' ----------------------------------------------------------------
' Procedure Name: AdvancedFilter_Columns
' Purpose: Uses Advanced Filter to copy individual columns
'
' Result: The result will be the Item and Sales column in the Report
'         worksheet.
' Website: https://excelmacromastery.com/
' ----------------------------------------------------------------
' https://excelmacromastery.com/
Sub AdvancedFilter_Columns()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions Filter")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    ' Clear any existing data
    shWrite.Cells.Clear
    
    ' Specify the output columns
    shWrite.Range("A1").Value2 = "Item"
    shWrite.Range("B1").Value2 = "Sales"

    ' Remove the filter
    If shRead.FilterMode = True Then
        shRead.ShowAllData
    End If
    
    ' Get the source data range
    Dim rgData As Range, rgCriteria As Range
    Set rgData = shRead.Range("A1").CurrentRegion
    
    ' IMPORTANT: Do not have any blank rows in the criteria range
    Set rgCriteria = shRead.Range("G1:K2")
   
    ' Apply the filter
    rgData.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rgCriteria _
                , CopyToRange:=shWrite.Range("A1:B1")

End Sub

Using ADO and SQL

ADO is an external Library that allows us to perform queries on databases. It stands for ActiveX Database Objects.

ADO can also perform queries on data in worksheets. This means we can do very powerful queries that run very fast.

Just like using AutoFilter and Advanced filter, we can use most of the same VBA code each time. The main difference will be the query line. The disadvantage is that it requires some basic knowledge of SQL.

If you want to see working examples using ADO and SQL then you can download the source code at the top or bottom of this post.

The following code returns all the records where the item is “Laptop Model A”:

' ReadFromWorksheetADO()

'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' Details: Returns all the records for item "Laptop Model A".
' https://excelmacromastery.com/
Sub ReadFromWorksheetADO()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    With shWrite
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "dd/mm/yyyy"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        .Columns(4).NumberFormat = "0"
        .Columns(5).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' To add ADO reference select Tools->Reference and
    ' check "Microsoft ActiveX Data Objects Objects 6.1 Library"
    Dim conn As New ADODB.Connection
    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & ThisWorkbook.FullName & ";" & _
            "Extended Properties=""Excel 12.0;HDR=Yes;"";"

    ' Store the query in a string
    Dim query As String
    query = "Select * from [" & shRead.name _
                    & "$] Where Item='Laptop Model A' "
    
    ' Run the query and store in a recordset
    Dim rs As New Recordset
    rs.Open query, conn

    ' Write header
    Dim i As Long
    For i = 0 To rs.Fields.count - 1
        shWrite.Cells(1, i + 1).Value2 = rs.Fields(i).name
    Next i
    
    ' Write data
    shWrite.Range("A2").CopyFromRecordset rs
    
    ' Close the connection
    conn.Close
    
End Sub

Here are some examples of ADO queries

' Item is "Laptop Model A" AND the volume is greater than or equal 20
query = "Select Item,Day,Price,Volume,Sales from [Transactions$] " _
         & " Where Item = 'Laptop Model A' and Volume>=20"

' Item is "Laptop Model A" OR the volume is greater than or equal 20
query = "Select Item,Day,Price,Volume,Sales from [Transactions$] " _
         & " Where Item = 'Laptop Model A' or Volume>=20"

' Item starts with "Laptop"  
query = "Select Item,Day,Price,Volume,Sales from [Transactions$] " _
         & " Where Item like 'Laptop%' "   

' Item Ends with "Model A"
query = "Select Item,Day,Price,Volume,Sales from [Transactions$] " _
         & " Where Item like '%Model A' "

' Item contains "top"
query = "Select Item,Day,Price,Volume,Sales from [Transactions$] " _
         & " Where Item like '%top%' "

 ' Item does not contain "top"
query = "Select Item,Day,Price,Volume,Sales from [Transactions$] " _
         & " Where Item Not like '%top%' "

' Return the total volume for each monitor type
query = "Select Item, Sum(Volume) As [Total Vol] from [Transactions$] " _
         & " Where Item like 'Monitor%' " _
         & " Group by Item"

It is important to note that for multiple queries you don’t need to connect to the workbook each time.

One connection to the workbook is enough. Then you can run all your queries. When your queries are finished you can close the connection.

The following outline shows what I mean:

' Open the connection
Dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=" & ThisWorkbook.FullName & ";" & _
        "Extended Properties=""Excel 12.0;HDR=Yes;"";"

' Run all queries
...
...

' When finished close the connection
conn.Close

How to Copy Rows Using ADO

Copying a row is pretty straightforward using ADO.

There is a function Range.CopyFromRecordset which writes out the entire data from the query.

This is very convenient but it doesn’t copy the header and it is slower than many other methods.

In the code below we write out all the items that are “Laptop Model A”:

' ADO_CopyRow()
'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' Details: Returns the records for item "Laptop Model A".
' https://excelmacromastery.com/
Sub ADO_CopyRow()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions Filter")
    Set shWrite = ThisWorkbook.Worksheets("Report")
     
    With shWrite
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "dd/mm/yyyy"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        .Columns(4).NumberFormat = "0"
        .Columns(5).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' To add ADO reference select Tools->Reference and
    ' check "Microsoft ActiveX Data Objects Objects 6.1 Library"
    Dim conn As New ADODB.Connection
    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & ThisWorkbook.FullName & ";" & _
            "Extended Properties=""Excel 12.0;HDR=Yes;"";"

    ' Store the query in a string
    Dim query As String
    query = "Select Item,Day,Price,Volume,Sales from [" & shRead.name _
            & "$] Where Item='Laptop Model A' "
    
    Dim rs As New Recordset
    rs.Open query, conn
    
    ' Write header
    Dim i As Long
    For i = 0 To rs.Fields.count - 1
        shWrite.Cells(1, i + 1).Value2 = rs.Fields(i).name
    Next i
    
    ' Write data
    shWrite.Range("A2").CopyFromRecordset rs
    
    conn.Close
    
End Sub

Copying Individual Fields

Copying individual fields using ADO couldn’t be simpler.

We specify the fields we want in the Select statement:

' Select all the fields
sQuery = "Select Item,Day,Price,Volume,Sales from [Transactions$]"

' Select Item and Sales
sQuery = "Select Item,Sales from [Transactions$]"

 ' Select Volume
sQuery = "Select Volume from [Transactions$]"

 ' Select Price, Item - changes order of fields
sQuery = "Select Item,Price from [Transactions$]"

Summing Totals

Summing totals is also simple. It only requires adding a Group By statement to the query

Let’s get the total volume and sales for each item:

' ADO_SumColums()'
' Requirements:
' 1. A worksheet "Transactions" containing data from this
' section: "The Dataset for this Article".
' 2. A worksheet for output called "Report".
'
' Details: Returns the total sales amount for each item.
' https://excelmacromastery.com/
Sub ADO_SumColums()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions Filter")
    Set shWrite = ThisWorkbook.Worksheets("Report")
     
    With shWrite
    
        ' Clear the data in output worksheet
        .Cells.ClearContents
        
        ' Set the cell formats
        .Columns(2).NumberFormat = "0"
        .Columns(3).NumberFormat = "$#,##0;[Red]$#,##0"
        
    End With
    
    ' To add ADO reference select Tools->Reference and
    ' check "Microsoft ActiveX Data Objects Objects 6.1 Library"
    Dim conn As New ADODB.Connection
    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & ThisWorkbook.FullName & ";" & _
            "Extended Properties=""Excel 12.0;HDR=Yes;"";"

    ' Store the query in a string
    Dim query As String
    query = "Select Item,Sum(Volume) As [Total Volume] " _
        & ",Sum(Sales) As [Total Sales] " _
        & "from [" & shRead.name & "$] Group By Item "
    
    Dim rs As New Recordset
    rs.Open query, conn
    
    ' Write header
    Dim i As Long
    For i = 0 To rs.Fields.count - 1
        shWrite.Cells(1, i + 1).Value2 = rs.Fields(i).name
    Next i
    
    ' Write data
    shWrite.Range("A2").CopyFromRecordset rs
    
    conn.Close
    
End Sub

Pivot Table

Using a Pivot Table is a very powerful way to sum data. It is faster than using the other summing methods.

It is more flexible than using For with a Dictionary but easier to use than ADO as no knowledge of SQL is necessary.

We don’t actually need a copy method with the Pivot Table. We just need to specify range where the Pivot table will be created.

Summing Data

The Pivot Table automatically sums values.

We have rows and value fields in a Pivot Table. We can use these to sum data.

  1. The Row field is the thing that we plan to get the total of e.g. Items.
  2. The Value field is the value that we wish to sum e.g. Volume, Sales.

We can set these fields like this:

' Set the Row fields
.PivotFields("Item").Orientation = xlRowField

' Set the Value fields
.PivotFields("Volume").Orientation = xlDataField
.PivotFields("Sales").Orientation = xlDataField

The full PivotTable code looks like this:

' https://excelmacromastery.com/
Sub PivotTable_Sum()

    ' Get the worksheets
    Dim shRead As Worksheet, shWrite As Worksheet
    Set shRead = ThisWorkbook.Worksheets("Transactions")
    Set shWrite = ThisWorkbook.Worksheets("Report")
    
    ' Get the range
    Dim rg As Range
    Set rg = shRead.Range("A1").CurrentRegion
   
    ' Clear any existing pivot tables
    Dim piv As PivotTable
    For Each piv In shWrite.PivotTables
        piv.TableRange2.Clear
    Next piv
    
    ' Clear the data in output worksheet
    .Cells.ClearContents
  
    ' Create the cache
    Dim ptCache As PivotCache
    Set ptCache = ThisWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase _
        , SourceData:=rg _
        , Version:=xlPivotTableVersion15)
    
    ' Create the table
    Dim ptTable As PivotTable
    Set ptTable = shWrite.PivotTables.Add( _
             PivotCache:=ptCache _
            , TableDestination:=shWrite.Range("A1"))
    
    ' Set the name and style
    ptTable.name = "pvItems"
    ptTable.TableStyle2 = "PivotStyleDark14"
    
    ' Set the fields
    Dim ptField As PivotField
    With ptTable

        ' Set the rows fields
        .PivotFields("Item").Orientation = xlRowField
        
        ' Set the data(value) fields
        .PivotFields("Volume").Orientation = xlDataField
        .PivotFields("Sales").Orientation = xlDataField
        
    End With
    
End Sub

If we want to filter with the PivotTable then it is a bit long winded as we have to set each item individually.

To add a filter we can use the code below. Add this code before the “End With” line in the above code.

' Filter items
Dim pvItem As PivotItem
For Each pvItem In .PivotFields("Item").PivotItems
    If pvItem.Caption = "Laptop Model A" Then
        pvItem.Visible = True
    Else
        pvItem.Visible = False
    End If
Next pvItem

Conclusion

That concludes this article on copying and filtering data using the different Excel VBA Copy methods.

If you think something is missing or you have any questions or queries then please leave a comment below.

What’s Next?

Free VBA Tutorial If you are new to VBA or you want to sharpen your existing VBA skills then why not try out the The Ultimate VBA Tutorial.

Related Training: Get full access to the Excel VBA training webinars and all the tutorials.

(NOTE: Planning to build or manage a VBA Application? Learn how to build 10 Excel VBA applications from scratch.)

На чтение 3 мин. Просмотров 51.7k.

Итог: Изучите 3 различных способа копирования и вставки ячеек или диапазонов в Excel с помощью макросов VBA. Это серия из трех частей, также вы сможете скачать файл, содержащий код.

Уровень мастерства: Начинающий

Копировать и вставить: наиболее распространенное действие Excel

Копирование и вставка, вероятно, является одним из самых
распространенных действий в Excel. Это также одна из самых распространенных
задач, которые мы автоматизируем при написании макросов.

Есть несколько различных способов выполнить эту задачу, и устройство записи макросов не всегда дает вам наиболее эффективный код VBA.

В следующих трех видео я объясняю:

  • Самый эффективный метод для простого копирования
    и вставки в VBA.
  • Самый простой способ вставить значения.
  • Как использовать метод PasteSpecial для других
    типов вставок.

Видео № 1: Простой метод «Копировать-вставить»

Видео лучше всего просматривать в полноэкранном HD.

Sub Примеры_копирования_диапазона()
'Используйте метод Range.Copy для простого копирования / вставки

    'Метод Range.Copy - копирование и вставка с 1 строкой
    Range("A1").Copy Range("C1")
    Range("A1:A3").Copy Range("D1:D3")
    Range("A1:A3").Copy Range("D1")
    
    'Range.Copy с одного листа на другой
    Worksheets("Лист1").Range("A1").Copy Worksheets("Лист2").Range("A1")
    
    'Range.Copy с одного файла (на другой
    Workbooks("План.xlsx").Worksheets("Лист1").Range("A1").Copy _
        Workbooks("Факт.xlsx").Worksheets("Лист1").Range("A1")

End Sub

Видео № 2: Простой способ вставить значения

Sub Копируем_только_значения()
'Установите значения ячеек равными другим, чтобы вставить значения

'Устанавливает равенство одного диапазона другому
    Range("C1").Value = Range("A1").Value
    Range("D1:D3").Value = Range("A1:A3").Value
     
'Равенство значений между листами
    Worksheets("Лист1").Range("A1").Value = Worksheets("Лист2").Range("A1").Value
     
'Равенство значений между книгами
    Workbooks("Факт.xlsx").Worksheets("Лист1").Range("A1").Value = _
        Workbooks("План.xlsx").Worksheets("Лист1").Range("A1").Value
        
End Sub

Видео № 3: Метод PasteSpecial

Sub Копируем_с_помощью_специальной_вставки()
'Используйте метод Range.PasteSpecial для выбора типа вставки

 'Копируем и вставляем через СпецВставку
Range("A1").Copy
Range("A5").PasteSpecial Paste:=xlPasteFormats

'Используем спецвставку между листами
Worksheets("Лист1").Range("A2").Copy
Worksheets("Лист2").Range("A2").PasteSpecial Paste:=xlPasteFormulas

'Используем спецвставку между файлами
Workbooks("План.xlsx").Worksheets("Лист1").Range("A3").Copy
Workbooks("Факт.xlsx").Worksheets("Лист1").Range("A1").PasteSpecial Paste:=xlPasteFormats

'Убираем "бегающих муравьёв" после копирования (очищаем буфер обмена)
Application.CutCopyMode = False    
   
End Sub

Вставить данные ниже последней заполненной строки

Один из самых распространенных вопросов, которые я получаю о копировании и вставке с помощью VBA: «Как мне вставить данные в конец таблицы? «

Сначала нужно найти последнюю заполненную строку данных, а затем скопировать и вставить ниже неё.

Переходите по ссылке, чтобы научиться 3 способам поиска последней заполненной ячейки

Работа с буфером обмена в VBA Excel: копирование и вставка ячеек, копирование текста из переменной, очистка буфера обмена. Объект DataObject. Примеры.

Копирование и вставка ячеек

Копирование содержимого и форматов ячеек (диапазона) в буфер обмена осуществляется методом Range.Copy, а вставка – методом Worksheet.Paste:

‘Копирование одной ячейки в буфер обмена

Range(«A10»).Copy

Cells(10, 1).Copy

‘Копирование диапазона ячеек в буфер обмена

Range(«B8:H12»).Copy

Range(Cells(8, 2), Cells(12, 8)).Copy

‘Вставка ячейки (диапазона) из буфера обмена на рабочий лист

ActiveSheet.Paste Range(«A20»)

ActiveSheet.Paste Cells(20, 1)

При вставке диапазона ячеек из буфера обмена на рабочий лист достаточно указать верхнюю левую ячейку места (диапазона) вставки.

Для вставки из буфера обмена отдельных компонентов скопированных ячеек (значения, формулы, примечания и т.д.), а также применения к диапазону транспонирования или вычислений, используется метод Range.PasteSpecial (специальная вставка).

Буфер обмена и переменная

Передача текста между переменной и буфером обмена в VBA Excel осуществляется с помощью объекта DataObject. Стоит иметь в виду, что на некоторых компьютерах DataObject может некорректно работать при открытом окне проводника.

Объект DataObject

DataObject – это область временного хранения форматированных фрагментов текста, используемая в операциях переноса данных.

Подробнее об элементе DataObject вы можете прочитать на сайте разработчиков.

Методы объекта DataObject:

Метод Описание
GetFromClipboard Копирует данные из буфера обмена в DataObject
GetText Извлекает текстовую строку из объекта DataObject в указанном формате
PutInClipboard Перемещает данные из DataObject в буфер обмена
SetText Копирует текстовую строку в DataObject, используя указанный формат

Копирование текста из переменной в буфер обмена

Sub Primer2()

Dim s As String, myData As New DataObject

s = «Копирование текста из переменной в буфер обмена»

‘Копируем текст из переменной в DataObject

myData.SetText (s)

‘Перемещаем текст из DataObject в буфер обмена

myData.PutInClipboard

‘Проверяем содержимое буфера обмена

ActiveSheet.Paste Range(«A1»)

End Sub

Копирование текста из буфера обмена в переменную

Sub Primer3()

Dim s As String, myData As New DataObject

Range(«A1») = «Копирование текста из буфера обмена в переменную»

‘Копируем данные из ячейки в буфер обмена

Range(«A1»).Copy

‘Копируем данные из буфера обмена в DataObject

myData.GetFromClipboard

‘Извлекаем текст из объекта DataObject и присваиваем переменной s

s = myData.GetText

‘Проверяем содержимое переменной s

MsgBox s

End Sub

Очистка буфера обмена

Специального метода для очистки буфера обмена в VBA Excel нет. Для решения этой задачи можно использовать выход из режима вырезания-копирования:

Application.CutCopyMode = False

Следующий пример демонстрирует вставку скопированной ячейки "A1" в ячейки "A2" и "A3" и отсутствие вставки в ячейки "A4" и "A5" после строки Application.CutCopyMode = False:

Sub Primer4()

Range(«A1») = «Очистка буфера обмена»

Range(«A1»).Copy

ActiveSheet.Paste Range(«A2»)

ActiveSheet.Paste Range(«A3»)

Application.CutCopyMode = False

On Error Resume Next

ActiveSheet.Paste Range(«A4»)

ActiveSheet.Paste Range(«A5»)

End Sub

Оператор On Error Resume Next необходим для обработки (пропуска) ошибки, возникающей при вставке из пустого буфера обмена.

Функции для работы с буфером обмена

В некоторых системах, начиная с Windows 8, метод DataObject.PutInClipboard не работает правильно: если открыт хотя бы один экземпляр Проводника (папка), в буфер обмена записываются два квадратика. Следующие функции должны решить эту проблему:

‘Функция записи текста в буфер обмена

Function SetClipBoardText(ByVal Text As Variant) As Boolean

    SetClipBoardText = CreateObject(«htmlfile»).parentWindow.clipboardData.SetData(«Text», Text)

End Function

‘Функция вставки текста из буфера обмена

Function GetClipBoardText() As String

    On Error Resume Next

    GetClipBoardText = CreateObject(«htmlfile»).parentWindow.clipboardData.GetData(«Text»)

End Function

‘Функция очистки буфера обмена

Function ClearClipBoardText() As Boolean

    ClearClipBoardText = CreateObject(«htmlfile»).parentWindow.clipboardData.clearData(«Text»)

End Function

Пример использования функций для работы с буфером обмена:

Sub Primer()

Dim s As String

    s = «Копирование текста из переменной в буфер обмена»

    ‘Копируем текст в буфер обмена

    SetClipBoardText (s)

    ‘Вставляем текс из буфера обмена в ячейку «A1»

    Range(«A1») = GetClipBoardText

    ‘Очищаем буфер обмена, если это необходимо

    ClearClipBoardText

End Sub


Понравилась статья? Поделить с друзьями:
  • Vba excel копирование диапазона ячеек с одного листа на другой
  • Vba excel копирование диапазона с листа на лист
  • Vba excel копирование данных с листа на лист
  • Vba excel копирование данных по условию
  • Vba excel копирование всей строки