VBA Insert Coloane - Cum se introduce o coloană în foaia de lucru Excel cu VBA?

În mod normal, în foaia de lucru Excel avem două metode diferite pentru a adăuga sau a insera coloane, una fiind comanda rapidă de la tastatură și alta utilizând metoda clic dreapta și inserare, dar în VBA trebuie să folosim comanda insert și întreaga instrucțiune de coloană pentru a adăuga orice coloană împreună, truc pentru asta este că, dacă trebuie să inserăm o singură coloană, oferim o singură coloană de referință, dar pentru mai multe coloane oferim mai multe coloane de referință.

Introduceți coloane în Excel folosind VBA

Ca parte a codului VBA, este posibil să fie nevoie să inserăm câteva coloane în foaia noastră de date pentru a se potrivi nevoilor noastre. Inserarea coloanei este tasta de comandă rapidă simplă din foaia de lucru apăsând Ctrl +, dar ce zici de inserarea unei coloane noi prin codul VBA. În acest articol despre „VBA Insert Column”, vă vom arăta procesul de adăugare a coloanelor în Excel și vă vom arăta exemple de scenarii diferite.

Cum se inserează coloane în foaia de lucru Excel folosind VBA?

Putem insera coloane în VBA folosind proprietatea COLUMNS și obiectul RANGE. Trebuie să de ce avem nevoie de coloane și obiecte pentru a insera o nouă coloană.

Pentru a insera o nouă coloană, trebuie să identificăm după ce coloană trebuie să inserăm, fără să spunem, după ce coloană trebuie să inserăm modul în care VBA poate înțelege logica.

De exemplu, dacă doriți să inserați coloana după coloana B, atunci puteți construi codul astfel.

Coloane („C”)

Notă: Motivul pentru care am folosit C pentru a insera o coloană după, deoarece celula selectată va fi mutată în partea dreaptă.

După specificarea coloanelor, trebuie să accesăm proprietatea „Întreaga coloană”.

Coloane („C”). EntireColumn

Apoi, trebuie să alegem metoda „Insert”.

Coloane („C”). EntireColumn.Insert

Aceasta va insera coloana după coloana C, iar coloana C existentă va fi mutată în D.

Exemplul # 1 - Introduceți coloane folosind Range Object în VBA

Acum, uitați-vă la modul de inserare a coloanei folosind obiectul RANGE. Pentru aceasta, luați în considerare datele de mai jos pentru un exemplu.

Acum vreau să introduc VBA coloana după coloana A, urmați pașii de mai jos pentru a construi codul.

Pasul 1: Începeți procedura secundară.

Pasul 2: Deschideți obiectul Range .

Cod:

Sub ColumnInsert_Example1 () Range (End Sub

Pasul 3: menționați acum coloana între ghilimele duble .

Cod:

Sub ColumnInsert_Example1 () Range ("B: B") End Sub

Pasul 4: Alegeți acum proprietatea Întreagă coloană .

Cod:

Sub ColumnInsert_Example1 () Range ("B: B"). Ent End Sub

Pasul 5: După selectarea proprietății Întreagă coloană, alegeți metoda „Insert” .

Cod:

Sub ColumnInsert_Example1 () Range ("B: B"). EntireColumn.ins End Sub

Acum, codul dvs. arată astfel.

Cod:

Sub ColumnInsert_Example1 () Range ("B: B"). EntireColumn.Insert End Sub

Rulați codul. Se va introduce noua coloană B.

Exemplul # 2 - Inserați mai multe coloane

For example, if you want to insert two new columns after column A, then we need to mention two column addresses.

Code:

Sub ColumnInsert_Example1() Range("B:C").EntireColumn.Insert End Sub

The above code will insert two columns after column A.

Example #3 - With “Insert” Only Method

We can insert a new column by using “Insert” the only method without accessing the Entire Column property. For this, let’s understand the “insert” method syntax.

Expression.Insert((Shift),(Copy Origin))

(Shift): When we insert the new column, whether we need the columns to shift to the right side or to shift to the downside in case of rows. Here we can use two options, “xlToRight” and “xlDownTo”

(Copy Origin): This will specify the format for the newly inserted column. Whether you need the format from the left side of the column or from the above cells. Here we can use two options “xlFormatFromLeftOrAbove” and “xlFormatFromRightOrBelow”.

Below is the example code for you.

Sub ColumnInsert_Example1() Range("B:B").Insert Shift:=xlToRight, Copyorigin:=xlFormatFromLeftOrAbove End Sub

Example #4 - Insert Columns Using COLUMNS Property in VBA

We have seen how to VBA insert columns using the RANGE object; now, we will show we can insert columns using the “COLUMNS” property.

Open the COLUMNS property.

Code:

Sub ColumnInsert_Example2() Columns( End Sub

We can specify the column in two ways here. One is using as usual alphabetic references, and another one is using column numbers.

If you want to insert the column after column A, then the code will be COLUMNS(“B”). If you are using numerical reference, then the code will be COLUMNS(2).

Code:

Sub ColumnInsert_Example2() Columns("B"). End Sub

Now the problem for you arises because when we use COLUMNS property, we don’t get to access the IntelliSense list.

Code:

Sub ColumnInsert_Example2() Columns("B").Entir End Sub

Here we need to sure of what we are doing. So this is the reason I have showed you the RANGE object first.

Code:

Sub ColumnInsert_Example2() Columns("B:B").EntireColumn.Insert End Sub

Example #5 - Insert Alternative Columns

Assume you have the data like the below image.

If you want to insert new columns after every alternate row, then we need to use VBA loops. The below code it tailor-made the code for you.

Code:

Sub ColumnInsert_Example3 () Dim k As Intreger Columns (2). Selectați Pentru k = 2 până la 8 ActiveCell.EntireColumn.Insert ActiveCell.Offset (0, 2). Selectați Next k End Sub

Aceasta va insera coloana astfel.

Exemplul # 6 - Introduceți coloana pe baza valorii celulei

De asemenea, putem insera o coloană pe baza valorii celulei. De exemplu, uitați-vă la datele de mai jos.

Aici vreau să inserez coloana dacă valoarea celulei de pe primul rând este „An”, iar datele mele ar trebui să placă după inserarea coloanelor noi.

Utilizați codul de mai jos pentru a efectua această sarcină.

Cod:

Sub ColumnInsert_Example4 () Dim k As Integer Dim x As Integer x = 2 For k = 2 To 12 If Cells (1, x) .Value = "Year" Then Cells (1, x) .EntireColumn.Insert x = x + 1 End Dacă x = x + 1 Următorul k End Sub

Puteți descărca acest VBA Insert Columns Excel aici. Șablon Excel pentru inserarea coloanelor VBA

Articole interesante...