Devolve a posição da primeira ocorrência de uma sequência em outra.
Sintaxe
InStr([start, ]string1, string2[, compare])
Argumentos:
Tabela 11-15 Constantes de Comparação e Descrições
| Constante | Valor | Descrição |
|---|---|---|
vbBinaryCompare |
0 | Executa uma comparação binária |
vbTextCompare |
1 | Executa uma comparação textual |
Valor de Retorno
A função InStr retorna os seguintes valores:
Tabela 11-16 Valores de Retorno da Função Instr
| Se | Instr Retornará |
|---|---|
| a string1 tiver tamanho zero | 0 |
| a string2 tiver tamanho zero | Start |
| a string2 não for encontrada | 0 |
| a string2 for encontrada na string1 | Posição na qual a correspondência foi encontrada. |
| start > Len(string2) | 0 |
Comentários
Os exemplos a seguir usam InStr para pesquisar uma string:
Exemplo 1:
Dim SearchString, SearchChar, MyPos SearchString ="XXpXXpXXPXXP" ' String to search in. SearchChar = "P" ' Search for "P". MyPos = Instr(SearchString, SearchChar) ' Comparison is binary by default (last argument is omitted). ' Output: 9
Exemplo 2:
Dim SearchString, SearchChar, MyPos SearchString ="XXpXXpXXPXXP" ' String to search in. SearchChar = "P" ' Search for "P". MyPos = Instr(1, SearchString, SearchChar) ' Comparison is binary by default (last argument is omitted) ' Default Comparison starting at the first character ' Output: 9
Exemplo 3:
Dim SearchString, SearchChar, MyPos SearchString ="XXpXXpXXPXXP" ' String to search in. SearchChar = "P" ' Search for "P". MyPos = Instr(4, SearchString, SearchChar, 1) ' A textual comparison starting at position 4. 'Output: 6 MyPos = Instr(1, SearchString, SearchChar, 0) ' A binary comparison starting at position 1. 'Output: 9
Exemplo 4:
Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
MyPos = Instr(1, SearchString, "W") ' A binary comparison starting at position 1.
'Output: 0 ("W" is not found).