Pasta especial VBA - Las 5 mejores formas de utilizar VBA Paste ¿Función especial?

Tabla de contenido

De manera similar a la hoja de trabajo, cuando copiamos un dato y lo pegamos en un rango de celdas diferente, tenemos un método especial de pegado que nos permite pegar los datos como sí mismos o solo las fórmulas o solo los valores y de la misma manera podemos usar Pegado especial en VBA usando el método de propiedad de rango como sigue rango. paste special () proporcionando el tipo que queremos entre paréntesis.

Pegar especial en VBA

Paste Special en Excel sirve de muchas formas en nuestro trabajo diario. Utilizando pegado especial, podemos hacer muchas más cosas de las habituales. Copiar y pegar está presente en todas partes del mundo de la informática. Pero pegar especial es lo avanzado en Excel.

Al igual que el pegado especial de Excel regular en VBA, también tenemos un método especial de pegado para pegar los datos copiados. Copiar las cosas en Excel no es algo extraño para los usuarios de Excel, copian, pegan y, la mayoría de las veces, usan el pegado especial para cumplir su propósito de muchas maneras.

En Excel normal, pegar incluye muchas opciones como pegar solo valores, pegar fórmulas, pegar formatos, etc.

Pegar especial tiene que Pegar, Operar, Omitir espacios en blanco y Transponer de esta manera en VBA también. Tenemos todos los parámetros con el método Paste Special .

La fórmula de Paste Special en VBA

A continuación se muestra la fórmula para Paste Special en VBA

Paste Special está disponible con el objeto Range VBA porque después de copiar los datos, los pegaremos en el rango de celdas, por lo que el método especial de pegado está disponible con el objeto range .

Tipo de pegado: después de copiar los datos, ¿cómo desea pegarlos? Ya sea que desee pegar valores, fórmulas, formatos, validación, etc. A continuación se muestra la lista completa de opciones disponibles en Pegar tipo.

Pegar Operación especial: Al pegar, ¿desea realizar algún tipo de operación como sumar, restar, dividir, multiplicar o ninguna?

  • (Omitir espacios en blanco): si desea omitir espacios en blanco, puede elegir VERDADERO o FALSO.
  • (Transponer): si desea transponer los datos, puede elegir VERDADERO o FALSO.

Ejemplos de Paste Special en Excel VBA

Los siguientes son ejemplos de pegado especial en VBA.

Ejemplo n. ° 1: pegar solo valores usando la función PasteSpecial de VBA

En el primer ejemplo, realizaremos pegar solo valores usando pegar especial. Suponga que a continuación se encuentran los datos que tiene en el nombre de la hoja denominada Datos de ventas.

Ahora realizaremos la tarea de copiar y pegar usando varios métodos especiales de pegado. Siga los pasos siguientes.

Paso 1: primero cree un nombre de macro.

Paso 2: primero, copie el rango A1 a D14 del nombre de la hoja "Datos de ventas". Para copiar el rango, aplique el siguiente código.

Código:

Rango ("A1: D14"). Copiar

Paso 3: Después de copiar los datos, pegaremos los valores de G1 a J14. Primero, haga referencia al rango.

Código:

Rango ("G1: J14")

Paso 4: Después de seleccionar el rango, necesitamos pegar. Así que ponga un punto (.) Y seleccione Pegar método especial.

Código:

Sub PasteSpecial_Example1 () Rango ("A1: D14"). Copiar rango ("G1: J14"). PasteSpecial End Sub

Paso 5: en la lista desplegable, seleccione la opción "xlPasteValues".

Código:

Sub PasteSpecial_Example1 () Rango ("A1: D14"). Copiar rango ("G1: J14"). PasteSpecial xlPasteValues ​​End Sub

Paso 6: Ahora ejecute este código usando la tecla F5 o manualmente y vea qué sucede.

Entonces, nuestro código copió los datos de A1 a D14 y los pegó de G1 a J14 como valores.

Se ha realizado la tarea de la tecla de acceso directo en Excel hoja de trabajo ALT + E + S + V .

Ejemplo # 2 - Pegar todo usando VBA PasteSpecial

Ahora veremos qué pasa si realizamos la tarea de xlPasteAll.

Código:

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 or 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, I have increased the column width for 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 we can see Sales column width has been increased to the column 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 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, then we need to select the range in that sheet. The 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 in a different sheet, we need to 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

  • Si desea omitir los espacios en blanco, debe ingresar el argumento como VERDADERO de manera predeterminada. Se necesita FALSO.
  • Si desea transponer los datos, debemos seleccionar la transposición como TRUE.
  • Solo podemos realizar una pasta especial a la vez.

Articulos interesantes...