VBA Paste Special
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
Paste Special in VBA
Paste Special in excel serves in many ways in our daily work. Using Paste Special, we can do many more things than usual. Of course, copy and paste are everywhere in the computer world. But, Paste Special is the advanced thing in Excel.
Like regular Excel Paste Special in VBA, too, we have a Paste Special method to paste the copied data. Copying things in Excel is not strange for Excel users. They copy and paste, and most of the time, they use Paste Special to serve their purpose in many ways.
In regular Excel, paste includes many options like paste only values, paste formulas, paste formats, etc.
Paste Special has to paste, operate, skip blanks, and transpose like in VBA. So, we have all the parameters with the Paste Special method.
Table of contents
- Paste Special in VBA
- The Formula of Paste Special in VBA
- Examples of Paste Special in Excel VBA
- Example #1 - Paste only Values using VBA PasteSpecial Function
- Example #2 - Paste All using VBA PasteSpecial
- Example #3 - Paste Formats using VBA PasteSpecial Function
- Example #4 - Paste Column Width using VBA Paste Special
- Example #5 - Copy the Data from One Sheet to Another Sheet using VBA Paste Special Option
- Things to Remember About Excel VBA PasteSpecial Function
- Recommended Articles
The Formula of Paste Special in VBA
Below is the formula for Paste Special in VBA.
The Paste Special is available with the VBA Range object because after copying the data, we will be pasting it in the cell range, so the Paste Special method is available with the Range object.
Paste Type: After copying the data, how do you want to paste it? Whether you want to paste values, formulas, formats, validation, etc. Below is the complete list of options available under Paste Type.
Paste Special Operation: While pasting, do you want to perform any operations like add, subtract, division, multiplication, or none?
- : If you want to skip blanks, you can choose TRUE or FALSE.
- : If you want to transpose the data, you can choose TRUE or FALSE.
Examples of Paste Special in Excel VBA
The following are examples of Paste Special in VBA.
Example #1 - Paste only Values using VBA PasteSpecial Function
In the first example, we will perform pasting only values using paste special. For example, assume below is the data you have in the sheet name called "Sales Data."
Now, we will perform the task of copying and pasting using several Paste Special methods. Follow the below steps.
Step 1: Create a Macro name first.
Step 2: First, copy the range A1 to D14 from the sheet name "Sales Data." To copy the range, apply the below code.
Code:
Range("A1:D14").Copy
Step 3: After copying the data, we will paste the values from G1 to J14. First, reference the range.
Code:
Range ("G1:J14")
Step 4: After selecting the range, we need to paste. So, put a dot (.) and select the Paste Special method.
Code:
Sub PasteSpecial_Example1() Range("A1:D14").Copy Range("G1:J14").PasteSpecial End Sub
Step 5: From the dropdown list, select the option "xlPasteValues."
Code:
Sub PasteSpecial_Example1() Range("A1:D14").Copy Range("G1:J14").PasteSpecial xlPasteValues End Sub
Step 6: Run this code using the F5 key or manually and see what happens.
So, our code copied the data from A1 to D14 and pasted it from G1 to J14 as values.
It has performed the task of shortcut excel key in worksheet ALT + E + S + V.
Example #2 - Paste All using VBA PasteSpecial
Now, we will see what happens if we perform the task of xlPasteAll.
Code:
Sub PasteSpecial_Example2() Range("A1:D14").Copy Range("G1:J14").PasteSpecial xlPasteAll End Sub
Now, if you run this code manually through the "Run" option, by pressing the F5 key, we will have as it is data.
Example #3 - Paste Formats using VBA PasteSpecial Function
Now, we will see how to paste only formats. The below code would do the job for us.
Code:
Sub PasteSpecial_Example3() Range("A1:D14").Copy Range("G1:J14").PasteSpecial xlPasteFormats End Sub
If you run this code using the F5 key or manually, we will get the only format of the copied range, nothing else.
Example #4 - Paste Column Width using VBA Paste Special
Now, we will see how to paste only column width from the copied range. For this, we have increased the width of one of my data columns.
Apply the below code. It will paste only the column width of the copied range.
Code:
Sub PasteSpecial_Example3() Range("A1:D14").Copy Range("G1:J14").PasteSpecial xlPasteColumnWidths End Sub
Run this code and see the difference in the column width.
Now, the "Sales" column width increases to the width of our copied range column.
Example #5 - Copy the Data from One Sheet to Another Sheet using VBA Paste Special Option
We have seen how to copy and paste the data on the same sheet. Now, we will learn how to paste from one sheet to another sheet.
Step 1: Before we select the range, we need to tell from which sheet we need to select the data.
Code:
Sub PasteSpecial_Example5() Worksheets ("Sales Data") End Sub
Step 2: After selecting the sheet by its name, we need to select the range in that sheet. They copy it.
Code:
Sub PasteSpecial_Example5() Worksheets("Sales Data").Range("A1:D14").Copy End Sub
The above code says in the sheet name "Sales Data" copy the Range ("A1:D14").
Step 3: Since we are pasting it on a different sheet, we must select the sheet by its name.
Code:
Sub PasteSpecial_Example5() Worksheets("Sales Data").Range("A1:D14").Copy Worksheets ("Month Sheet") End Sub
Step 4: Now, in the sheet "Month Sheet," select the range.
Code:
Sub PasteSpecial_Example5() Worksheets("Sales Data").Range("A1:D14").Copy Worksheets("Month Sheet").Range ("A1:D14") End Sub
Step 5: Using Paste Special, we will be pasting values and format.
Code:
Sub PasteSpecial_Example5() Worksheets("Sales Data").Range("A1:D14").Copy Worksheets("Month Sheet").Range("A1:D14").PasteSpecial xlPasteValuesAndNumberFormats End Sub
Step 6: We are not only pasting values and format using VBA Paste Special, but we are pasting it as TRANSPOSE as well.
Code:
Sub PasteSpecial_Example5() Worksheets("Sales Data").Range("A1:D14").Copy Worksheets("Month Sheet").Range("A1:D14").PasteSpecial xlPasteValuesAndNumberFormats, Transpose:=True End Sub
Now run this code. It will copy and transpose the data to the "Month Sheet."
Things to Remember About Excel VBA PasteSpecial Function
- To skip blanks, we must enter the argument as TRUE by default. It takes FALSE.
- If we want to transpose the data, we must select the transpose as TRUE.
- We can perform only one Paste Special at a time.
Recommended Articles
This article has been a guide to Excel VBA PasteSpecial. Here, we learn the top 5 ways to use VBA PasteSpecial function, practical examples, and downloadable VBA codes. Below are some useful Excel articles related to VBA: -