question

Como criar uma macro para o microsoft excel para fazer uma pesquisa?

preciso de ajuda!!!!!!!!!! Estou tentando obter o código de uma macro que irá pesquisar uma planilha do excel para um item específico, então tome os resultados da pesquisa e olhar para que o conteúdo em outra planilha... como isso é possível??? por favor me ajude!!!!!!!!!
resposta Resposta
Deve ser bastante simples, mas você vai precisar incluir mais detalhes sobre o que você quer - especificamente, o que deve acontecer se/quando o texto é encontrado em qualquer folha? Realçar as células, mostrar uma mensagem,....?

O código a seguir irá Pesquisar ambas as folhas para o texto inserido pelo usuário e mostrar uma mensagem com os endereços das células onde (se) o texto foi encontrado

Adicionar um botão à sua folha principal e adicione o seguinte código:

Private Sub CommandButton1_Click()

Dim SearchStr As String

Dim RowCnt As Integer

Dim ColCnt As Integer

Dim SearchRng As Range

Dim Rng como Range

Dim ColLetter As String

Ws Dim como planilha

Dim FirstResult As String

Dim SecondResult As String

Dim ResultStr As String

' ' ' ' ' ' OBTER TEXTO A PROCURAR

SearchStr = InputBox ("Enter a Cadeia de caracteres de pesquisa")

' ' ' ' ' ' NÚMERO DE LINHAS E COLUNAS

' ' ' ' ' CM PARA SELEÇÃO

RowCnt = _ Application.WorksheetFunction

.CountA(Range("A:A"))

ColCnt = _ Application.WorksheetFunction

.CountA(Range("1:1"))

' ' ' ' ' ' OBTER ÚLTIMA COLUNA LETRA

ColLetter = Left (células (1, ColCnt). _

Address(0, 0), 1-(ColCnt > 26))

' ' ' ' ' ' DEFINIR ÁREA DE PESQUISA

Definir SearchRng = Range ("A1:" + ColLetter + CStr(RowCnt))

CM ' ' ' ' ' VERIFICAR CADA CÉLULA E REGISTRE O ENDEREÇO SE O TEXTO DA PESQUISA FOR ENCONTRADO

CM CM CM CM CM CM SAIR FOR/AVANÇAR LOOP PARA ECONOMIZAR TEMPO

Para cada Rng em SearchRng

Se Rng.Value = SearchStr Then

FirstResult = Rng.Address

Saída para

End If

Próximo

' ' ' ' ' CM SAIA SE TEXTO NÃO ENCONTRADO NA PRIMEIRA FOLHA

CM CM CM CM CM CM EXCLUIR ESSA PARTE SE DESEJAR PESQUISAR A SEGUNDA FOLHA DE QUALQUER MANEIRA

Se Len(FirstResult) = 0 Then

MsgBox "Nenhum resultado encontrado"

Exit Sub

End If

' ' ' ' ' CM EXAMINAR SEGUNDA FOLHA COMO ACIMA

Definir ws = Sheets("Test")

Com ws

RowCnt = Application.WorksheetFunction. _

CONT (.Range("A:A"))

ColCnt = _ Application.WorksheetFunction

.CONT (.Range("1:1"))

ColLetter = Left (.Células (1, ColCnt). _

Address(0, 0), 1-(ColCnt > 26))

Definir SearchRng =.Range ("A1:" + ColLetter + _

CStr(RowCnt))

Terminar com

Para cada Rng em SearchRng

Se Rng.Value = SearchStr Then

SecondResult = Rng.Address

Saída para

End If

Próximo

Se Len(SecondResult) = 0 Then

ResultStr = "Item encontrado na primeira folha somente na célula:" + FirstResult

Outra pessoa

ResultStr = "Item encontrado na primeira célula da folha:" + _ FirstResult

+ vbCrLf + _

"Item encontrado na segunda célula da folha:" + SecondResult

End If

' ' ' ' ' ' EXIBIR UMA MENSAGEM COM OS RESULTADOS

MsgBox ResultStr

Definir ws = Nothing

Set SearchRng = Nothing

Definir Rng = Nothing



EndSub

PS: se você não quiser ser diferencia maiúscminúsc de minúsculas, substitua:

SearchStr = InputBox ("Enter a Cadeia de caracteres de pesquisa")

e

RNG.Value

com

SearchStr = UCase (InputBox ("Insira Search String"))

e

UCase(Rng.Value)

ComentáriosComentários
Acho que a resposta não está correta ou que você gostaria de acrescentar mais
alguma informação? Envie o seu comentário abaixo..

Guest


HTML não é permitido!

Image Code

Digite os caracteres que aparecem na imagem por isso sabemos que você é humano!

Receber um email quando alguém acrescenta outro comentário a esta pergunta


Topo da página


Home  Terms
Copyright © Accelerated Ideas 2005-2024
All rights reserved