VBA Paste Special - Top 5 moduri de utilizare a funcției speciale VBA Paste

Similar cu foaia de lucru atunci când copiem date și le lipim pe o gamă de celule diferită, avem o metodă specială de lipire care ne permite să lipim datele ca el însuși sau numai formulele sau doar valorile și aceeași modă pe care o putem folosi Lipiți special în VBA folosind metoda proprietății intervalului după cum urmează. paste special () oferind tipul pe care îl dorim între paranteze.

Lipiți special în VBA

Paste Special in Excel servește în multe feluri în munca noastră de zi cu zi. Folosind paste speciale, putem face mult mai multe lucruri decât cele obișnuite. Copierea și lipirea există peste tot în lumea computerelor. Dar lipirea specială este lucrul avansat în Excel.

La fel ca și pasta Excel specială specială în VBA, avem și o metodă specială de lipire a datelor copiate. Copierea lucrurilor în Excel nu este un lucru ciudat pentru utilizatorii Excel, aceștia copiază, lipesc și, de cele mai multe ori, folosesc paste speciale pentru a-și servi scopul în mai multe moduri.

În Excel obișnuit, lipirea include multe opțiuni, cum ar fi lipirea numai a valorilor, formule de lipire, formate de lipire etc. etc.

Lipiți special trebuie să lipiți, operațiunea, săriți golurile și să transpuneți astfel în VBA. Avem toți parametrii cu metoda Paste Special .

Formula specială a lipirii în VBA

Mai jos este Formula pentru Paste Special în VBA

Paste Special este disponibil cu obiectul VBA Range deoarece după copierea datelor, îl vom lipi în intervalul de celule, astfel încât metoda specială paste este disponibilă cu obiectul Range .

Tip lipire: după copierea datelor, cum doriți să lipiți. Indiferent dacă doriți să lipiți valori, formule, formate, validare etc … Mai jos este lista completă a opțiunilor disponibile sub Tip lipire.

Lipiți operațiunea specială: în timp ce lipiți, doriți să efectuați orice fel de operații cum ar fi adunarea, scăderea, divizarea, înmulțirea sau niciuna.

  • (Omiteți golurile): Dacă doriți să omiteți golurile, puteți alege ADEVĂRAT sau FALS.
  • (Transpunere): dacă doriți să transpuneți datele, puteți alege ADEVĂR sau FALS.

Exemple de lipire specială în Excel VBA

Următoarele sunt exemplele de paste speciale în VBA.

Exemplul # 1 - Lipiți doar valori folosind funcția VBA PasteSpecial

În primul exemplu, vom efectua lipirea numai a valorilor folosind paste special. Să presupunem mai jos datele pe care le aveți în numele foii numite Date de vânzare.

Acum vom efectua sarcina de copiere și lipire folosind mai multe metode speciale de lipire. Urmați pașii de mai jos.

Pasul 1: Creați mai întâi un nume de macro.

Pasul 2: Mai întâi, copiați intervalul A1 în D14 din numele foii „Date de vânzare”. Pentru a copia intervalul, aplicați codul de mai jos.

Cod:

Gama („A1: D14”). Copiați

Pasul 3: După copierea datelor, vom lipi valorile de la G1 la J14. În primul rând, faceți referire la interval.

Cod:

Gama ("G1: J14")

Pasul 4: După selectarea intervalului, trebuie să lipim. Deci puneți un punct (.) Și selectați Metoda lipire specială.

Cod:

Sub PasteSpecial_Example1 () Range ("A1: D14"). Copy Range ("G1: J14"). PasteSpecial End Sub

Pasul 5: Din lista derulantă, selectați opțiunea „xlPasteValues”.

Cod:

Sub PasteSpecial_Example1 () Range ("A1: D14"). Copy Range ("G1: J14"). PasteSpecial xlPasteValues ​​End Sub

Pasul 6: Rulați acum acest cod folosind tasta F5 sau manual și vedeți ce se întâmplă.

Deci, codul nostru a copiat datele de la A1 la D14 și a lipit de la G1 la J14 ca valori.

S - a efectuat sarcina de comenzi rapide cheie Excel în foaia de lucru ALT + E + S + V .

Exemplul # 2 - Lipiți toate folosind VBA PasteSpecial

Acum vom vedea ce se întâmplă dacă executăm sarcina xlPasteAll.

Cod:

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

  • Dacă doriți să omiteți spațiile goale, trebuie să introduceți argumentul ca TRUE în mod implicit. Este nevoie de FALS.
  • Dacă doriți să transpuneți datele, trebuie să selectăm transpunerea ca ADEVĂRAT.
  • Putem efectua o singură pastă specială odată.

Articole interesante...