Activate found cell

Asked

Viewed 175 times

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: inserir a descrição da imagem aqui

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 add Option 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

  • 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.

1 answer

0

I believe you are trying to activate a cell in a spreadsheet that is hidden or not active.

Try to activate the spreadsheet:

'[...]

' Ativar a Planilha:    
Worksheets("Central.de.Clientes").Activate

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
' [...]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.