VBA Set Statemment - Cum se atribuie valori variabilelor de obiect?

Declarație set VBA Excel

Setul VBA este o declarație care este utilizată pentru a atribui orice cheie de valoare spune un obiect sau o referință la o variabilă, folosim această funcție pentru a defini parametrul pentru o anumită variabilă, de exemplu, dacă scriem setul M = A care înseamnă acum referința are aceleași valori și atribute similare cu ceea ce are A.

În VBA, un obiect este un nucleu al excelului, deoarece fără obiecte nu putem face nimic. Obiectele sunt Caiet de lucru, Foaie de lucru și Interval. Când declarăm o variabilă, trebuie să îi atribuim un tip de date și putem atribui și obiecte ca tipuri de date. Pentru a atribui o valoare variabilelor obiect declarate, trebuie să folosim cuvântul „SET”. Cuvântul „Set” folosit pentru a se referi la un obiect nou în VBA, de exemplu, referindu-se la intervalul specific al foii de lucru.

Cum se utilizează declarația de setare VBA Excel?

# 1 - Setați declarația cu variabilele de obiect Range

De exemplu, presupuneți că doriți să utilizați intervalul A1 până la D5 destul de des. În loc să scriem codul ca Range („A1: D5”) de fiecare dată, putem declara variabila ca interval și putem seta referința intervalului ca Range („A1: D5”)

Pasul 1: declarați variabila ca obiect Range.

Cod:

Sub Set_Example ()

Dim MyRange As Range

Sfârșitul Sub

Pasul 2: În momentul în care atribuim tipul de date ca interval, folosiți cuvântul „Set”.

Cod:

Sub Set_Example () Dim MyRange as Range Set MyRange = End Sub

Pasul 3: menționați acum gama.

Cod:

Sub Set_Example () Dim MyRange as Range Set MyRange = Range ("A1: D5") End Sub

Pasul 4: Acum, variabila „MyRange” este egală cu intervalul A1 până la D5. Folosind această variabilă, putem accesa toate proprietățile și metodele acestui interval.

Putem copia, adăuga un comentariu în Excel și putem face multe alte lucruri.

De exemplu, scopul, am creat câteva numere aici.

Acum folosind variabila, voi schimba dimensiunea fontului la 12.

Cod:

Sub Set_Example () Dim MyRange as Range Set MyRange = Range ("A1: D5") MyRange.Font.Size = 12 End Sub

Aceasta va modifica dimensiunea fontului pentru intervalul atribuit.

Like this, we can do many things with a particular range by using the word “Set.”

#2 - Set Statement with Worksheet Object Variables

We have seen how “set” works with a range object in VBA. It works exactly the same as the worksheet object as well.

Let’s say you have 5 worksheets in your workbook, and you want to keep going back to the one particular worksheet. You can set that worksheet name to the defined object variable.

For example, look at the below code.

Code:

Sub Set_Worksheet_Example() Dim Ws As Worksheet Set Ws = Worksheets("Summary Sheet") End Sub

In the above code, the variable “Ws” defined as an object variable, and in the next line, by using the word “Set,” we set the variable to the worksheet named “Summary Sheet.”

Now by using this variable, we can do all the things associated with it. Take a look at the below two sets of code.

#1 - Without “Set” Word

Code:

Sub Set_Worksheet_Example1() 'To select the sheet Worksheets("Summary Sheet").Select 'To Activate the sheet Worksheets("Summary Sheet").Activate 'To hide the sheet Worksheets("Summary Sheet").Visible = xlVeryHidden 'To unhide the sheet Worksheets("Summary Sheet").Visible = xlVisible End Sub

Every time I have used the worksheets object to refer to the sheet “Summary Sheet,” This makes the code so lengthy and requires a lot of time to type.

As part of the huge code, it is frustrating to type the worksheet name like this every time you need to reference the worksheet.

Now take a look at the advantage of using the word Set in Code.

#2 - With “Set” Word

Code:

Sub Set_Worksheet_Example() Dim Ws As Worksheet Set Ws = Worksheets("Summary Sheet") 'To select the sheet Ws.Select 'To Activate the sheet Ws.Activate 'To hide the sheet Ws.Visible = xlVeryHidden 'To unhide the sheet Ws.Visible = xlVisible End Sub

The moment we set the worksheet name, we can see the variable name while entering the code as part of the list.

#3 - Set Statement with Workbook Object Variables

The real advantage of the word “Set” in VBA arises when we need to reference different workbooks.

When we work with different workbooks, it is so hard to type in the full name of the workbook, along with its file extension.

Assume you have two different workbooks named “Sales Summary File 2018.xlsx” and “Sales Summary File 2019.xlsx” we can set the two workbooks like the below code.

Code:

Sub Set_Workbook_Example1 () Dim Wb1 As Workbook Dim Wb2 As Workbook Set Wb1 = Workbooks ("Sales Summary File 2018.xlsx") Set Wb2 = Workbooks ("Sales Summary File 2019.xlsx") End Sub

Acum variabila Wb1 este egală cu registrul de lucru numit „Sales Summary File 2018.xlsx”, iar variabila Wb2 este egală cu registrul de lucru numit „Sales Summary File 2019.xlsx”.

Folosind această variabilă, putem accesa de fapt toate proprietățile și metodele asociate registrului de lucru.

Putem scurta codul ca mai jos.

Fără a utiliza Set Keyword pentru a activa registrul de lucru:

Cărți de lucru („Fișier sumar vânzări 2018.xlsx”). Activați

Folosind Setarea cuvântului cheie pentru a activa registrul de lucru:

Wb1.Activați

Acest lucru face scrierea codului mult mai simplă și, de asemenea, odată ce numele registrului de lucru este setat, există o grijă de eroare de scriere a numelor registrului de lucru.

Articole interesante...