Kaip naudoti VBA kiekvienai kilpai? (su „Excel“ pavyzdžiais)

„Excel VBA“ kiekvienai kilpai

VBA už kiekvieną kilpą peržiūri visą objektų ar daiktų kolekciją ir atlieka panašų veiklų rinkinį. Ji atsižvelgs į visus turimus nurodytus objektus ir atliks nurodytą veiklą kiekviename objekte.

VBA privaloma suprasti kilpas. Kilpa leidžia atlikti tą pačią veiklą daugeliui „Excel“ langelių ar objektų. Šiandienos straipsnyje daugiausia dėmesio skirsime „Kiekvienos kilpos“ mechanizmui.

Sintaksė

Kiekviena kilpa gali apžvelgti visą nustatytą objektų ar daiktų kolekciją. Grupė yra ne kas kita, kaip „Visos atidarytos darbaknygės“, „Visi darbaknygės darbalapiai“, „Visas darbaknygės formų ir diagramų rinkinys“.

Pažvelkime į sintaksę.

Ką daryti kiekvienam kolekcijos objektui? Kitas objektas

Pvz., Savo darbaknygėje turite 10 lapų ir norite paslėpti visus darbalapius, išskyrus tuos, kuriuose esate. Ar galite paslėpti rankiniu būdu? Taip, galite, bet ką daryti, jei turite 100 tokių lapų? Ar tai nėra varginanti ir daug laiko reikalaujanti užduotis? Tai galite padaryti naudodami kiekvieną kilpą.

Kaip naudoti kiekvienai VBA kilpai? (Pavyzdžiai)

1 pavyzdys - Įterpkite tą patį tekstą į visus lapus

Pažiūrėsime, kaip naudoti KIEKVIENAM VBA su paprastu pavyzdžiu. Tarkime, kad turite 5 darbalapius darbalapyje ir norite įterpti žodį „Labas“ į visus A1 langelio darbalapius.

Tai galime padaryti naudodami KIEKVIENĄ LOOP. Čia reikia atsiminti vieną dalyką: mes atliekame šią veiklą kiekviename darbalapyje, o ne tame pačiame darbalapyje. Norėdami parašyti VBA kodą, atlikite toliau nurodytus veiksmus.

1 veiksmas: paleiskite „Excel“ makrokomandą.

Kodas:

Sub For_Each_Example1 () Pabaiga Sub

2 žingsnis: Kadangi kalbame apie darbalapius, deklaruokite kintamąjį kaip „Darbalapį“.

Kodas:

Sub For_Each_Example1 () Dim Ws kaip darbalapio pabaigos sub

3 žingsnis: Dabar, naudodamiesi KIEKVIENAM LOOP, turime nurodyti kiekvieną aktyviosios darbaknygės darbalapį.

Kodas:

Sub For_Each_Example1 () Dim Ws kaip kiekvieno Ws darbalapis „ActiveWorkbook“. Darbo lapai „Next Ws End Sub“

4 žingsnis: Dabar kiekviename darbalapyje užrašykite tai, ką norime padaryti. Kiekviename darbalapyje mes turime įterpti žodį „Labas“ į langelį A1.

Kodas:

Sub For_Each_Example1 () Dim Ws kaip kiekvieno Ws darbalapis „ActiveWorkbook.Worksheets Ws.Range“ („A1“). Reikšmė = „Sveiki“ Kitas Ws End Sub

5 žingsnis: Dabar paleiskite šį kodą rankiniu būdu naudodami parinktį arba paspauskite spartųjį klavišą F5. Nesvarbu, kiek lapų turite; į visus darbalapius bus įterptas žodis „Labas“.

2 pavyzdys - slėpti visus lapus

Kaip pasakyta anksčiau įraše, ką daryti, jei turite paslėpti šimtus lapų, išskyrus tą, kuriame esate. Naudodamiesi kiekviena kilpa, visus „Excel“ lapus galime paslėpti.

1 veiksmas: paleiskite makrokomandą savo vardu.

Kodas:

Sub For_Each_Example2 () Pabaiga Sub

2 žingsnis: paskelbkite kintamąjį kaip „ Ws“.

Kodas:

Sub For_Each_Example2() Dim Ws As Worksheet End Sub

Step 3: Now, in each worksheet, what you need to do is hide the sheet.

Code:

Sub For_Each_Example2() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Visible = xlSheetVeryHidden Next Ws End Sub

Step 4: But if you run the above code, it will try to hide all the sheets, but excel needs at least one sheet visible. So we need to tell which sheet not to hide.

Code:

Sub For_Each_Example2() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets If Ws.Name "Main Sheet" Then Ws.Visible = xlSheetVeryHidden End If Next Ws End Sub

The operator symbol means not equal to in VBA.

So code says when you are looping through all the worksheets in the active workbook, hide only if the sheet name is not equal to the sheet name of Main Sheet.

This can be done by using the IF statement in VBA. Write the code as IF Ws.Name “Main Sheet” Then hide or if it is equal to the sheet name “Main Sheet,” then don’t hide.

Step 5: Now run the code using the F5 key or manually. Then, it will hide all the worksheets except the one named “Main Sheet.”

Example #3 - Unhide All the Sheets

We have seen how to hide all sheets except the one we are in. Similarly, we can unhide all the worksheets as well.

We need to change the code from xlSheetVeryHidden to xlSheetVisible.

Code:

Sub For_Each_Example3() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Visible = xlSheetVisible Next Ws End Sub

Here we don’t need the IF condition because we are unhiding all the sheets. If you don’t want to unhide any specific sheet, then you can use the IF condition and supply the sheet name.

Example #4 - Protect and UnProtect All the Sheets

Protect All Sheets: We can protect all the sheets in the workbook with just a piece of code. All the principle is the same only thing we need to do here is instead of Ws. Visible, we need to put the code Ws. Protect and type the password.

Code:

Sub For_Each_Example4() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Protect Password:="Excel@2019" Next Ws End Sub

Pašalinti visų lapų apsaugą: panašioje pastaboje, naudodami VBA, taip pat galime apsaugoti visus darbaknygėje saugomus lapus. Turime įdėti žodį „Apsaugoti ir slaptažodį“.

Kodas:

Sub For_Each_Example6 () Dim Ws kaip kiekvieno Ws darbalapis „ActiveWorkbook“. Darbo lapai Ws. Nepanaikinkite slaptažodžio: = "Excel @ 2019" Kitas Ws End Sub

Ką reikia atsiminti

  • Kiekvienas skirtas objektams rinkti.
  • Ji atsižvelgs į visus nurodytus objektus nurodytoje darbaknygėje.
  • Deklaruodami kintamąjį turime nurodyti, kurį objektą mes nurodome, pavyzdžiui, darbalapį, darbaknygę, diagramą ir kt.

Įdomios straipsniai...