Cartea de lucru VBA - Exemple de utilizare a obiectului Excel VBA Workbook

Cartea de lucru Excel VBA

VBA Workbook este un obiect care face parte din colecția de obiecte Workbooks. Vedem cu ușurință diferența dintre partea colecției de obiecte și obiectul în sine, cuvântul plural din „Cărți de lucru” se referă la faptul că are multe „Cărți de lucru”. În VBA, avem alte câteva obiecte precum foi de lucru, celule și intervale, diagrame și forme.

În platforma Excel, fișierul Excel este numit „Workbook”, în special în VBA. Nu îl numim niciodată fișier; mai degrabă, îl numim „registru de lucru”.

Referindu-ne la registrul de lucru, putem face toate sarcinile legate de acesta. Unele dintre sarcinile importante sunt „Open Workbook”, „Save Workbook”, „Save As Workbook” și „Close Workbook”. Putem selecta, activa registrele de lucru care sunt deschise.

Sintaxă

Acum, uitați-vă la care este sintaxa registrului de lucru.

Un index nu este altceva decât registrul de lucru pe care doriți să îl selectați. Ne putem referi la registrul de lucru după numărul registrului de lucru sau după numele registrului de lucru.

Utilizarea codului de obiect VBA WorkBook

Exemplul nr. 1

De exemplu, am două fișiere deschise chiar acum. Primul nume al registrului de lucru este „Fișierul 1”, iar al doilea nume al registrului de lucru este „Fișierul 2”.

Acum scriu codul în al treilea fișier. Din acest fișier, doresc să activez registrul de lucru denumit „Fișier 1”.

Pasul 1: Porniți macro-ul prin crearea unei subproceduri VBA.

Cod:

Sub Workbook_Example1 () End Sub

Pasul 2: Selectați acum obiectul din registrul de lucru.

Pasul 3: Acum, introduceți registrul de lucru pe care dorim să îl activăm.

Cod:

Sub Workbook_Example1 () Workbooks ("Fișier 1 Sfârșit Sub

Pasul 4: După introducerea numelui registrului de lucru, trebuie să introducem și extensia de fișier. Am salvat acest registru de lucru ca un registru de lucru obișnuit, adică un registru de lucru „xlsx” .

Cod:

Sub Workbook_Example1 () Workbooks ("File 1.xlsx") End Sub

Pasul 5: Acum, trebuie să decidem ce vrem să facem cu acest registru de lucru. Introduceți punctul pentru a vedea toate opțiunile disponibile cu acest registru de lucru.

Pasul 6: Acum, trebuie să activăm registrul de lucru, selectăm metoda ca „Activare”.

Cod:

Sub Workbook_Example1 () Workbooks ("File 1.xlsx"). Activați End Sub

It doesn’t matter which workbook you are in. It will activate the specified workbook.

As soon as you select the workbook, it becomes an “Active Workbook.”

Example #2 - Enter Values in the Workbook

As I told as soon as you select the workbook, it becomes an Active Workbook. Using Active Workbook, we can reference the cell.

In the active workbook, we need to select the sheet by its name, or else we use the word Active Sheet.

In the active worksheet, we need to select the cell by using the Range object.

Code:

Sub Workbook_Example1() Workbooks("File 1.xlsx").Activate ActiveWorkbook.ActiveSheet.Range("A1").Value = "Hello" End Sub

When you run this code using the F5 key or manually, it will insert the word “Hello” in the cell A1 in the workbook “File 1.xlsx”.

We can also use the below code to do the same job.

Code:

Sub Workbook_Example1() Workbooks("File 1.xlsx").ActiveSheet.Range("A1").Value = "Hello" End Sub

This will also insert the word “Hello” to the workbook “File 1.xlsx.”

Example #3 - Assign Workbook to Variable

We can also assign the data type as a “workbook” to the declared variable. Declare the variable as Workbook.

Code:

Sub Workbook_Example2()

Dim WB As Workbook

End Sub

Now we need to set the object variable to the workbook name by using the word “Set.”

Code:

Sub Workbook_Example2() Dim WB As Workbook Set WB = Workbooks("File 1.xlsx") End Sub

From now onwards, the variable “WB” holds the name of the workbook “File 1.xlsx”.

Using the variable name, we can insert the words.

Code:

Sub Workbook_Example2() Dim WB As Workbook Set WB = Workbooks("File 1.xlsx") WB.Worksheets("Sheet1").Range("A1") = "Hello" WB.Worksheets("Sheet1").Range("B1") = "Good" WB.Worksheets("Sheet1").Range("C1") = "Morning" End Sub

Run this code manually or use shortcut key F5 and see the result, as shown in the below screenshot.

WB.Worksheets("Sheet1").Range("A1") = "Hello"

Here WB is referencing the workbook, in that workbook, we are referencing the worksheet Sheet1 by using the Worksheets object. In that worksheet cell, A1 is equal to the value of “Hello.”

We can also reference the workbook by index number as well. For example, look at the below code.

Code:

Sub Workbook_Example3() Workbooks(1).Activate Workbooks(2).Activate Workbooks(3).Activate End Sub

Here Workbooks (1) means whichever the workbook first on the list, like this similarly Workbooks (2) refers to the second workbook, and Workbooks (3) refers to the third workbook.

The main problem with this index number referencing is we don’t know exactly which workbook activated. It is dangerous to use index numbers.

Example #4 - For Each Loop for Workbook Object

As I told in the beginning, the workbook is a collection object of Workbooks in VBA. Whenever we want to perform the same kind of activity for all the opened workbooks, we need to use For Each loop in VBA.

For Each Loop is the loop for all the objects in VBA. Use the below code to save all the opened workbooks.

Code:

Sub Save_All_Workbooks () Reduceți WB ca registru de lucru pentru fiecare WB din registrele de lucru WB.Salvați următorul WB End Sub

Când rulați acest cod prin intermediul tastei F5 sau manual, apare o fereastră pop-up, care cere salvarea registrului de lucru. Faceți clic pe Ok pentru a salva.

Utilizați codul de mai jos pentru a închide toate registrele de lucru, cu excepția celui pe care lucrați.

Cod:

Sub Close_All_Workbooks () Dim WB Ca registru de lucru pentru fiecare WB din registrele de lucru If WB.Name ThisWorkbook.Name Then WB.Close End If Next WB End Sub

O fereastră pop-up apare înainte de a închide registrul de lucru.

Articole interesante...