a variavel do objeto ou a variavel do bloco with não foi def

glaubermoraes93

14-05-2015 14:53:15

Pessoal preciso de uma ajuda.
estou iniciando em VBA e estou com uma dificuldade, preciso que o usuario preencha o CNPJ e quando ele der TAB ou Enter o sistema compare com o cnpj da planilha, tendo como chave a matricula do prestador.

Segue o código.

Private Sub txtcnpj_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Or KeyCode = 9 Then

Dim lngRow As Long
Dim CNPJ As Currency
Dim Prestador As Currency
Dim c As Range

If Me.txtcnpj.Text = "" Then
MsgBox "Favor Preencher o CNPJ", vbOKOnly, "Atenção"
Me.txtcnpj.SetFocus

Else
Prestador = Me.txtCPProtocolador.Value
CNPJ = Me.txtcnpj.Value
With ThisWorkbook.Worksheets("Prestadores")
.Activate

Set c = .Range("A1:A10").Find(Prestador, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then
If Prestador = c.Value And CNPJ = c.Offset(0, 9).Value Then
Me.frmdadosfiscais.Visible = True
End If
Else
MsgBox "CNPJ incorreto", 64, " Atenção"
Me.txtcnpj.Text = ""
Me.txtcnpj.SetFocus
End If
End With
End If
End If


End Sub

e no c.value apresenta a mensagem "a variavel do objeto ou a variavel do bloco with não foi definida"

netocasaroli

14-05-2015 20:27:50

Tente colocar a instrução End With após a linha Set c = .Range("A1:A10").Find(Prestador, LookIn:=xlValues, LookAt:=xlWhole), e exclua ela do final do código...

glaubermoraes93

15-05-2015 11:05:06

Obrigado, problema resolvido.