0
I’m having trouble activating the cell that was found.
The code is as follows::
Private Sub cmdPesquisar_Click()
'Verificar se foi digitado um nome na primeira caixa de texto
If Txtnif.Text = "" Then
MsgBox "Digite o NIF de um cliente"
Txtnif.SetFocus
GoTo Linha1
End If
With Worksheets("Central.de.Clientes").Range("F:F")
Set c = .Find(Txtnif.Value, LookIn:=xlValues, LookAt:=xlPart)
If Not c Is Nothing Then
c.Activate
Txtnif.Value = c.Value
Txtempresa.Value = c.Offset(0, -1).Value
Txttelefone.Value = c.Offset(0, 1).Value
Txtmorada.Value = c.Offset(0, 2).Value
Txtlocal.Value = c.Offset(0, 3).Value
Txtcontacto.Value = c.Offset(0, 4).Value
Txtemailrel.Value = c.Offset(0, 5).Value
Txtemailfact.Value = c.Offset(0, 6).Value
'Carregando o botão de opção
If c.Offset(0, 8) = "Masculino" Then
OptionButton1.Value = True
Else
OptionButton2.Value = True
End If
Else
MsgBox "Cliente não encontrado!"
End If
End With
Linha1:
End Sub
But when I run, the following error appears:
And the debug identifies the line:
c.Activate
Can help me identify the error in the code?
In a quick test, the code works for me. It can be the way you are declaring variables, what type of variable is
Txtnif
(Texbox? Combobox? Cell? )? And addOption Explicit
in the code header, which is considered good practice, as you will need to declare all variables correctly. And this way you are only finding the first of this value, try using the example of the . Find method– danieltakeshi
Are you selecting another sheet at the time you perform this function? Add
Worksheets("Central.de.Clientes").Activate
before the wrong code. Remember that it is recommended to avoid using Activate and Select, but in some cases it may be necessary to use these. Especially if you want to focus on the value found.– danieltakeshi