Usuário com melhor resposta
Selecionar Celula pelo listbox ( Resolvido )

Pergunta
-
Saudações a Todos!!
Tenho uma código que uso para fazer consulta de dados numa planilha, porém gostaria ao clicar na resultado exibido na listbox a celula correspondente fosse selecionada.
segue abaixo o código caso alguem queira ajudar.
o formulário deve conter:
1 TextBox
1 ListBox
1 CommandButton
o preenchimento da planilha para melhor compreensão deve ser como mostrado abaixo:
A B
1 MARIA
2 JOAO
1 MARIA
2 JOAO
1 MARIA
2 JOAO
Segue Código:
Option Explicit
Public Lancto As Integer
Private Sub CommandButton1_Click()
Dim N
N = 1
Range("A2").Select
Do While ActiveCell <> ""
If CStr(ActiveCell.Value) = TextBox1.Value Then
Range("AN" & N).Offset(1, 0).Value = ActiveCell.Offset(0, 0)
Range("AN" & N).Offset(1, 1).Value = ActiveCell.Offset(0, 1)
N = N + 1
End If
ActiveCell.Offset(1, 0).Select
Loop
If N > 1 Then
ListBox1.RowSource = "AN2:BY" & N
Else
ListBox1.RowSource = ""
End If
End Sub
Private Sub ListBox1_Click()
If ListBox1.ListIndex = -1 Then
Exit Sub
End If
Lancto = ListBox1.ListIndex + 2
Localizar
End Sub
Private Sub UserForm_Initialize()
Dim preencher As Boolean
Range("A1").Select
preencher = True
Do While preencher = True
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = Empty Then
Exit Do
End If
Loop
ListBox1.ColumnHeads = True
ListBox1.ColumnCount = 2
ListBox1.ColumnWidths = "20;20"
End Sub
Sub Localizar()
With Sheets(1)
TextBox1.Value = .Cells(Lancto, 2).Value
.Cells(Lancto, 1).Select
End With
End Sub
Faça a consulta usando o textbox e o commandbutton, inserindo no textbox o numéro à procurar. Dai poderão ver que quando clicamos no resultado correspondente a celula correta nao é selecionada.
Desde já grato a todos.- Editado Joilson Quinteiro quinta-feira, 13 de maio de 2010 00:06
Respostas
-
Oi Joilson,
Tentei não mudar muita coisa no seu código, ficando assim:
Option Explicit Public Lancto As Integer Private Sub CommandButton1_Click() Dim N N = 1 Range("AN:AR").Delete Range("A2").Select Do While ActiveCell <> "" If CStr(ActiveCell.Value) = TextBox1.Value Then Range("AN" & N).Offset(1, 0).Value = ActiveCell.Offset(0, 0) Range("AN" & N).Offset(1, 1).Value = ActiveCell.Offset(0, 1) Range("AN" & N).Offset(1, 2).Value = ActiveCell.Row ' N = N + 1 End If ActiveCell.Offset(1, 0).Select Loop If N > 1 Then ListBox1.RowSource = "AN2:BY" & N Else ListBox1.RowSource = "" End If End Sub Private Sub ListBox1_Click() If ListBox1.ListIndex = -1 Then Exit Sub End If Lancto = ListBox1.ListIndex + 1 ' Localizar End Sub Private Sub UserForm_Initialize() Dim preencher As Boolean Range("A1").Select preencher = True Do While preencher = True ActiveCell.Offset(1, 0).Select If ActiveCell.Value = Empty Then Exit Do End If Loop ListBox1.ColumnHeads = True ListBox1.ColumnCount = 2 ListBox1.ColumnWidths = "20;20" End Sub Sub Localizar() With Sheets(1) TextBox1.Value = Range("AN1").Offset(Lancto, 1).Value '.Cells(Lancto, 2).Value .Cells(Range("AN1").Offset(Lancto, 2).Value, 2).Select End With End Sub
Alexandre- Marcado como Resposta Joilson Quinteiro terça-feira, 18 de maio de 2010 00:36
Todas as Respostas
-
Oi Joilson,
Tentei não mudar muita coisa no seu código, ficando assim:
Option Explicit Public Lancto As Integer Private Sub CommandButton1_Click() Dim N N = 1 Range("AN:AR").Delete Range("A2").Select Do While ActiveCell <> "" If CStr(ActiveCell.Value) = TextBox1.Value Then Range("AN" & N).Offset(1, 0).Value = ActiveCell.Offset(0, 0) Range("AN" & N).Offset(1, 1).Value = ActiveCell.Offset(0, 1) Range("AN" & N).Offset(1, 2).Value = ActiveCell.Row ' N = N + 1 End If ActiveCell.Offset(1, 0).Select Loop If N > 1 Then ListBox1.RowSource = "AN2:BY" & N Else ListBox1.RowSource = "" End If End Sub Private Sub ListBox1_Click() If ListBox1.ListIndex = -1 Then Exit Sub End If Lancto = ListBox1.ListIndex + 1 ' Localizar End Sub Private Sub UserForm_Initialize() Dim preencher As Boolean Range("A1").Select preencher = True Do While preencher = True ActiveCell.Offset(1, 0).Select If ActiveCell.Value = Empty Then Exit Do End If Loop ListBox1.ColumnHeads = True ListBox1.ColumnCount = 2 ListBox1.ColumnWidths = "20;20" End Sub Sub Localizar() With Sheets(1) TextBox1.Value = Range("AN1").Offset(Lancto, 1).Value '.Cells(Lancto, 2).Value .Cells(Range("AN1").Offset(Lancto, 2).Value, 2).Select End With End Sub
Alexandre- Marcado como Resposta Joilson Quinteiro terça-feira, 18 de maio de 2010 00:36
-
-
Fala Joilson,
Pode linkar sim, não vejo problema.
E marque o post como resposta para a pergunta não ficar aberta aqui no fórum :)
Alexandre
- Marcado como Resposta Joilson Quinteiro terça-feira, 18 de maio de 2010 00:36
- Não Marcado como Resposta Joilson Quinteiro terça-feira, 18 de maio de 2010 00:36