VBA Intersect - Exemplos de Intersect no Excel VBA - Métodos

Excel VBA Intersect

O VBA Intersect é usado para obter um objeto de intervalo que é uma interseção de dois ou mais intervalos. O mínimo de dois intervalos deve ser fornecido para encontrar o ponto de interseção do intervalo. Todos os outros argumentos são opcionais com base no requisito.

Abaixo está a sintaxe da fórmula VBA INTERSECT.

  • Arg1 como intervalo: primeiro intervalo de interseção.
  • Arg2 como intervalo: segundo intervalo de interseção.

Nos exemplos abaixo, veremos algumas das técnicas úteis.

Exemplos

Exemplo 1

Por exemplo, use os dados abaixo.

Etapa 1: declare a variável como variante.

Código:

Sub Intersect_Example () Dim MyValue As Variant End Sub

Passo 2: Para esta variável atribua o valor por meio da fórmula Intersect.

Código:

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

Etapa 3: selecione o primeiro intervalo de B2 a B9.

Código:

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

Etapa 4: selecione o segundo intervalo de A5 a D5.

Código:

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

Etapa 5: estamos testando apenas com dois intervalos aqui. Feche a fórmula e selecione o método como um endereço de célula VBA.

Código:

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

Etapa 6: Mostrar o valor na caixa de mensagem em VBA.

Código:

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

Ok, terminamos e vemos o que teremos na caixa de mensagem.

Obtivemos o resultado como B5, ou seja, o endereço da célula do ponto de interseção do intervalo fornecido.

Assim, usando o método VBA INTERSECT, podemos fazer muito mais coisas.

Exemplo # 2

Selecione a célula de interseção

Para selecionar a célula de interseção do intervalo fornecido, use o código abaixo.

Código:

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

Isso selecionará a célula de interseção do intervalo fornecido.

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

  • No Excel, para obter o valor de interseção do intervalo, precisamos fornecer caracteres de espaço entre dois intervalos.
  • Usando a codificação VBA, podemos destacar, formatar, excluir ou alterar e fazer muitas outras coisas com o valor de interseção.
  • Se várias linhas e colunas forem fornecidas para a função de interseção, obteremos os dois valores do meio.

Artigos interessantes...