VBA leikkaa - Esimerkkejä leikkauspisteistä Excel VBA: ssa - Menetelmät

Sisällysluettelo

Excel VBA leikkaa

VBA Intersect -toimintoa käytetään alueobjektin hankkimiseen, joka on kahden tai useamman alueen leikkauspiste. Vähintään kaksi aluetta tulisi toimittaa leikkaavan aluepisteen löytämiseksi. Kaikki muut argumentit ovat valinnaisia ​​vaatimuksen perusteella.

Alla on VBA INTERSECT -kaavan syntaksi.

  • Arg1 alueena: Ensimmäinen leikkaava alue.
  • Arg2 alueena: Toinen leikkaava alue.

Seuraavissa esimerkeissä näemme joitain hyödyllisiä tekniikoita.

Esimerkkejä

Esimerkki 1

Käytä esimerkiksi alla olevia tietoja.

Vaihe 1: Ilmoita muuttuja Variantiksi.

Koodi:

Sub Intersect_Example () Dim MyValue Variant End Sub

Vaihe 2: Määritä tälle muuttujalle arvo Intersect-kaavan kautta.

Koodi:

Sub Intersect_Example () Dim MyValue Variant MyValue = Intersect (End Sub

Vaihe 3: Valitse ensimmäinen alue B2 - B9.

Koodi:

Sub Intersect_Example () Dim MyValue Variant MyValue = Intersect (Range ("B2: B9"), End Sub)

Vaihe 4: Valitse toinen alue välillä A5 - D5.

Koodi:

Sub Intersect_Example () Dim MyValue As Variant MyValue = Intersect (Range ("B2: B9"), Range ("A5: D5") End Sub

Vaihe 5: Testaamme vain kahdella alueella. Sulje kaava ja valitse menetelmä VBA-soluosoitteeksi.

Koodi:

Sub Intersect_Example () Dim MyValue As Variant MyValue = Intersect (Range ("B2: B9"), Range ("A5: D5")). Osoitteen loppuosa Sub

Vaihe 6: Näytä arvo VBA: n viestiruudussa.

Koodi:

Sub Intersect_Example () Dim MyValue Variant MyValue = Intersect (Range ("B2: B9"), Range ("A5: D5")). Osoite MsgBox MyValue End Sub

Ok, olemme valmiita ja näemme mitä saamme viestiruudusta.

Tulokseksi saatiin B5 eli toimitetun alueen leikkauspisteen soluosoite.

Tällä tavoin VBA INTERSECT -menetelmällä voimme tehdä paljon enemmän asioita.

Esimerkki 2

Valitse Risteyssolu

Käytä alla olevaa koodia valitaksesi toimitetun alueen leikkaussolu.

Koodi:

Sub Intersect_Example2 () Intersect (Range ("B2: B9"), Range ("A5: D5")). Valitse End Sub

Tämä valitsee toimitetun alueen leikkaussolun.

Example #3

Clear Content of the Intersection Cell: In order to clear the content of the intersection cell of the supplied range uses the below code.

Code:

Sub Intersect_Example2() Intersect(Range("B2:B9"), Range("A5:D5")).ClearContents End Sub

Example #4

Change the Cell Color Background and Font Color of Intersection Cell: In order to change the background color of the intersection cell and the font color of the intersection cell value using the below code.

Code:

Sub Intersect_Example2() Intersect(Range("B2:B9"), Range("A5:D5")).Cells.Interior.Color = rgbBlue Intersect(Range("B2:B9"), Range("A5:D5")).Cells.Font.Color = rgbAliceBlue End Sub

Change the Value of the Intersection Cell: Using the Intersect function, we can also change the value of that cell into something else.

In the above data, the intersect value of the range “B2:B9” & “A5:D5” is cell B5 i.e., marked with blue color. Now by supplying this range to intersect function, we can actually change the value to something else.

The below code will change the value from 29398 to “New Value.”

Code:

Sub Intersect_Example3() Intersect(Range("B2:B9"), Range("A5:D5")).Value = "New Value" End Sub

Run the code above. We will get the word “New Value” in place of 29398.

Like this, by using the Intersect function, we can play around with the middle position value of the supplied range.

Things to Remember

  • Excelin alueella, jotta saisimme alueen leikkausarvon, meidän on annettava välilyönti kahden alueen välillä.
  • VBA-koodausta käyttämällä voimme korostaa, muotoilla, poistaa tai muuttaa ja tehdä monia muita asioita leikkausarvoon.
  • Jos useat rivit ja sarakkeet toimitetaan leikkausfunktioon, saamme kaksi keskimmäistä arvoa.

Mielenkiintoisia artikkeleita...