Průnik VBA - Příklady průniku v aplikaci Excel VBA - Metody

Průnik Excel VBA

VBA Intersect se používá k získání objektu rozsahu, který je průsečíkem dvou nebo více rozsahů. K nalezení bodu protínajícího se rozsahu je třeba zadat minimálně dva rozsahy. Všechny ostatní argumenty jsou volitelné na základě požadavku.

Níže je uvedena syntaxe vzorce VBA INTERSECT.

  • Arg1 jako Range: První protínající se rozsah.
  • Arg2 jako Range: Druhý protínající se rozsah.

V následujících příkladech uvidíme některé z užitečných technik.

Příklady

Příklad č. 1

Použijte například níže uvedená data.

Krok 1: Deklarujte proměnnou jako variantu.

Kód:

Sub Intersect_Example () Dim MyValue jako varianta End Sub

Krok 2: U této proměnné přiřaďte hodnotu pomocí vzorce Průnik.

Kód:

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

Krok 3: Vyberte první rozsah jako B2 až B9.

Kód:

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

Krok 4: Vyberte druhý rozsah od A5 do D5.

Kód:

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

Krok 5: Testujeme zde pouze dva rozsahy. Zavřete vzorec a vyberte metodu jako adresu buňky VBA.

Kód:

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

Krok 6: Zobrazit hodnotu v okně se zprávou ve VBA.

Kód:

Sub Intersect_Example () Dim MyValue jako varianta MyValue = Intersect (Range ("B2: B9"), Range ("A5: D5")). Adresa MsgBox MyValue End Sub

Dobře, máme hotovo a uvidíme, co do zprávy dostaneme.

Výsledek jsme dostali jako B5, tj. Adresa buňky průsečíku zadaného rozsahu.

Podobně jako pomocí metody VBA INTERSECT můžeme dělat mnohem více věcí.

Příklad č. 2

Vyberte průnikovou buňku

Chcete-li vybrat průnikovou buňku dodaného rozsahu, použijte níže uvedený kód.

Kód:

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

Tím vyberete průnikovou buňku dodaného rozsahu.

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

  • V aplikaci Excel, abychom získali průsečíkovou hodnotu rozsahu, musíme dát mezeru mezi dvěma rozsahy.
  • Pomocí kódování VBA můžeme zvýraznit, formátovat, mazat nebo měnit a dělat mnoho dalších věcí s hodnotou průniku.
  • Pokud je funkci intersect dodáno více řádků a sloupců, dostaneme prostřední dvě hodnoty.

Zajímavé články...