Vilc, bom dia.
Cara, peguei seu modelo e testei os códigos para realizar esse filtro.
No formulário de pesquisa, será necessário acrescentar dois blocos de código abaixo:
1º) Conforme abaixo, no evento
Userform_Activate, adicione um código para configurar o
ListView, de forma a receber as informações corretamente:
Código: Selecionar todos
Private Sub UserForm_Activate()
With Me.ListView1
.FullRowSelect = True 'seleciona linha
.View = lvwReport
.Gridlines = True 'efeito grade
.ColumnHeaders.Clear 'limpa cabeçalho
.ListItems.Clear 'limpa informações do ListView
.ColumnHeaders.Add Text:="Ref", Width:=0 'define 1º cabeçalho
.ColumnHeaders.Add Text:="Nome", Width:=150, Alignment:=lvwColumnLeft 'define 2º cabeçalho
.ColumnHeaders.Add Text:="CPF/CNPJ", Width:=100, Alignment:=lvwColumnCenter 'define 3º cabeçalho
End With
End Sub
2º) Neste ponto, foi utilizado um evento
Change do controle [colorFF0000]
TxtPesquisa2[/color]. O código está realizando o filtro das informações, da forma que você necessita:
Código: Selecionar todos
Private Sub TxtPesquisa2_Change()
'Confere se o campo nome foi preenchido
If Txtpesquisa2.Value = "" Then
MsgBox ("Campo Nome é obrigatório")
Txtpesquisa2.SetFocus
Exit Sub
End If
'Criamos as variáveis.
Dim objWsh As Worksheet
Dim intLinhaTotal As Integer
Dim intLinha As Integer
Dim intLista As Integer
Set objWsh = Worksheets("BD_Clientes")
intLinhaTotal = objWsh.UsedRange.Rows.Count
For intLinha = 1 To intLinhaTotal
With objWsh
If .Cells(intLinha, 1).Value = "" Then Exit For
If VBA.LCase(.Cells(intLinha, 1).Value) Like _
VBA.LCase(Me.Txtpesquisa2.Value) & "*" Then
intLista = Me.ListView1.ListItems.Count + 1
Me.ListView1.ListItems.Add intLista, , intLinha
Me.ListView1.ListItems(intLista).ListSubItems.Add 1, , .Cells(intLinha, 1).Value
Me.ListView1.ListItems(intLista).ListSubItems.Add 2, , .Cells(intLinha, 2).Value
End If
End With
Next intLinha
End Sub
3º) Neste bloco, foi utilizado o evento
Click do
ListView. As informações serão enviadas aos campos (
TextBox´s), quando o nome for selecionado no
ListView.
Código: Selecionar todos
Private Sub ListView1_Click()
'Criamos as variáveis.
Dim intRef As Integer
Dim objWsh As Worksheet
'Aqui é selecionado a Planilha com seus dados (Plan1) no nosso exemplo
Set objWsh = Worksheets("BD_Clientes")
intRef = Me.ListView1.SelectedItem.Text
With objWsh
txtnome.Value = .Cells(intRef, 1).Value
txtCPF_CNPJ.Value = .Cells(intRef, 2).Value
txtIE.Value = .Cells(intRef, 3).Value
txtidentidade.Value = .Cells(intRef, 4).Value
txtorgaoemissor.Value = .Cells(intRef, 5).Value
txtendereco.Value = .Cells(intRef, 6).Value
txtcidade.Value = .Cells(intRef, 7).Value
TxtUF.Value = .Cells(intRef, 8).Value
txtbairro.Value = .Cells(intRef, 9).Value
TxtCep.Value = .Cells(intRef, 10).Value
txttelefone.Value = .Cells(intRef, 11).Value
txtcelular.Value = .Cells(intRef, 12).Value
txtemail.Value = .Cells(intRef, 13).Value
txtnomedamae.Value = .Cells(intRef, 14).Value
txtnascimento.Value = .Cells(intRef, 15).Value
End With
End Sub
Acrescentando esses três códigos acima, o
ListView será filtrado, conforme vai digitando o nome no campo de pesquisa.
Obs.: quanto maior for o número de registros existentes na planilha, o desempenho dessa rotina irá cair. Isso se deve ao fato de que cada letra digitada no código, ativará a rotina de filtro. Analisando dessa forma, a sugestão dada pelo Tomás, talvez seja bem interessante para um grande número de registros. Mas não será utilizado o ListView para o filtro.
Faça as alterações e nos retorne, tendo sucesso ou mais alguma dúvida.
Forte abraço e bons estudos.