Notifications
Clear all

ERRO IF

11 Posts
2 Usuários
0 Reactions
2,839 Visualizações
(@gfsouza86)
Posts: 132
Estimable Member
Topic starter
 

Boa noite galera
Tenho o código abaixo que verifica se o campp senha esta em branco
Caso esteja em branco ele mostra a msgbox Digite uma senha para pesquisar! e manter as opções desabilitadas

Private Sub cmdpesquisar_Click()
With Worksheets("base").Range("A:A")
Set c = .Find(txtsenha.Value, LookIn:=xlValues, LookAt:=xlWhole)

If txtsenha.Text = "" Then
MsgBox "Digite uma senha para pesquisar!", vbInformation, "SENHA EM BRANCO"

'desabilitando as opcoes
Me.txtfornecedor.Enabled = False
Me.txtdata.Enabled = False

Me.cbrecebida.Enabled = False
Me.cbrecusada.Enabled = False
Me.cbnoshow.Enabled = False
Me.frmoc.Enabled = False
Me.cbsemoc.Enabled = False
Me.cbavaria.Enabled = False
Me.cbhorario.Enabled = False
Me.cbpaletizacao.Enabled = False
Me.cbqualidade.Enabled = False
Me.cbshelf.Enabled = False
Me.cberrocad.Enabled = False
Me.cbnf.Enabled = False
Me.cberroped.Enabled = False
Me.cbveiculo.Enabled = False
Me.frmobs.Enabled = False
Me.txtobs.Enabled = False
Me.txtsenha.Enabled = False
Me.txtobs.BorderStyle = fmBorderStyleSingle
Me.txtobs.SpecialEffect = fmSpecialEffectSunken

Else
.........

A msgbox aparece normalmente, porem o que ele deixa desabilitado são somente as opções txtfornecedor e txtdata

obs.: o restante do código esta funcionando e por esse motivo não coloquei o restante

 
Postado : 19/07/2018 4:09 pm
(@klarc28)
Posts: 0
New Member
 

Quanto ao erro citado, não vi erro no código.
Apenas troquei a ordem de algumas linhas, por algo mais lógico:

Private Sub cmdpesquisar_Click()
With Worksheets("base").Range("A:A")

If txtsenha.Text = "" Then


'desabilitando as opcoes
Me.txtfornecedor.Enabled = False
Me.txtdata.Enabled = False

Me.cbrecebida.Enabled = False
Me.cbrecusada.Enabled = False
Me.cbnoshow.Enabled = False
Me.frmoc.Enabled = False
Me.cbsemoc.Enabled = False
Me.cbavaria.Enabled = False
Me.cbhorario.Enabled = False
Me.cbpaletizacao.Enabled = False
Me.cbqualidade.Enabled = False
Me.cbshelf.Enabled = False
Me.cberrocad.Enabled = False
Me.cbnf.Enabled = False
Me.cberroped.Enabled = False
Me.cbveiculo.Enabled = False
Me.frmobs.Enabled = False
Me.txtobs.Enabled = False
Me.txtsenha.Enabled = True
Me.txtobs.BorderStyle = fmBorderStyleSingle
Me.txtobs.SpecialEffect = fmSpecialEffectSunken
MsgBox "Digite uma senha para pesquisar!", vbInformation, "SENHA EM BRANCO"
Else

Set c = .Find(txtsenha.Value, LookIn:=xlValues, LookAt:=xlWhole)

.........
 
Postado : 19/07/2018 4:45 pm
(@gfsouza86)
Posts: 132
Estimable Member
Topic starter
 

Boa tarde
klarc28

coloquei o seu código e mesmo assim ele esta deixando as opções habilitadas após aparecer a msgbox

 
Postado : 20/07/2018 2:11 pm
(@klarc28)
Posts: 0
New Member
 

Creio que o erro não está nessa parte do código.
Provavelmente está desabilitando aí e logo em seguida outra parte do código já habilita novamente.

 
Postado : 20/07/2018 2:14 pm
(@gfsouza86)
Posts: 132
Estimable Member
Topic starter
 

klarc28
o código do botão esta assim

Private Sub cmdpesquisar_Click()
With Worksheets("base").Range("A:A")
'Set c = .Find(txtsenha.Value, LookIn:=xlValues, LookAt:=xlWhole)

If txtsenha.Text = "" Then
'MsgBox "Digite uma senha para pesquisar!", vbInformation, "SENHA EM BRANCO"

'desabilitando as opcoes
Me.txtfornecedor.Enabled = False
Me.txtdata.Enabled = False

Me.cbrecebida.Enabled = False
Me.cbrecusada.Enabled = False
Me.cbnoshow.Enabled = False
Me.frmoc.Enabled = False
Me.cbsemoc.Enabled = False
Me.cbavaria.Enabled = False
Me.cbhorario.Enabled = False
Me.cbpaletizacao.Enabled = False
Me.cbqualidade.Enabled = False
Me.cbshelf.Enabled = False
Me.cberrocad.Enabled = False
Me.cbnf.Enabled = False
Me.cberroped.Enabled = False
Me.cbveiculo.Enabled = False
Me.frmobs.Enabled = False
Me.txtobs.Enabled = False
Me.txtsenha.Enabled = False
Me.txtobs.BorderStyle = fmBorderStyleSingle
Me.txtobs.SpecialEffect = fmSpecialEffectSunken

MsgBox "Digite uma senha para pesquisar!", vbInformation, "SENHA EM BRANCO"
Else

Set c = .Find(txtsenha.Value, LookIn:=xlValues, LookAt:=xlWhole)

'Else

If Not c Is Nothing Then

'c.Activate
Sheets("base").Visible = True
txtsenha.Value = c.Value
txtfornecedor.Value = c.Offset(0, 1).Value
txtdata.Value = c.Offset(0, 2).Value

'opcoes
'opcao recebida
If c.Offset(0, 3).Value = "1" Then
cbrecebida.Value = True
Else
cbrecebida.Value = False
End If

'opcao recusada
If c.Offset(0, 4).Value = "1" Then
cbrecusada.Value = True
Else
cbrecusada.Value = False
End If

'opcao noshow
If c.Offset(0, 5).Value = "1" Then
cbnoshow.Value = True
Else
cbnoshow.Value = False
End If

'opcoes
If c.Offset(0, 6).Value = "1" Then
cbsemoc.Value = True
Else
cbsemoc.Value = False
End If

If c.Offset(0, 7).Value = "1" Then
cbavaria.Value = True
Else
cbavaria.Value = False
End If

If c.Offset(0, 8).Value = "1" Then
cbhorario.Value = True
Else
cbhorario.Value = False
End If

If c.Offset(0, 9).Value = "1" Then
cbpaletizacao.Value = True
Else
cbpaletizacao.Value = False
End If

If c.Offset(0, 10).Value = "1" Then
cbqualidade.Value = True
Else
cbqualidade.Value = False
End If

If c.Offset(0, 11).Value = "1" Then
cbshelf.Value = True
Else
cbshelf.Value = False
End If

If c.Offset(0, 12).Value = "1" Then
cberrocad.Value = True
Else
cberrocad.Value = False
End If

If c.Offset(0, 13).Value = "1" Then
cbnf.Value = True
Else
cbnf.Value = False
End If

If c.Offset(0, 14).Value = "1" Then
cberroped.Value = True
Else
cberroped.Value = False
End If

If c.Offset(0, 15).Value = "1" Then
cbveiculo.Value = True
Else
cbveiculo.Value = False
End If

Sheets("base").Visible = False

Else

'Limpar as caixas de texto
txtsenha.Value = Empty
txtfornecedor.Value = Empty
txtdata.Value = Empty

MsgBox "Verifique a senha digitada!", vbCritical, "SENHA NÃO LOCALIZADA"

'foco na senha
txtsenha.SetFocus
End If
End If
End With

'habilitando as opcoes
Me.txtfornecedor.Enabled = False
Me.txtdata.Enabled = False
Me.cbrecebida.Enabled = True
Me.cbrecusada.Enabled = True
Me.cbnoshow.Enabled = True
Me.frmoc.Enabled = True
Me.cbsemoc.Enabled = True
Me.cbavaria.Enabled = True
Me.cbhorario.Enabled = True
Me.cbpaletizacao.Enabled = True
Me.cbqualidade.Enabled = True
Me.cbshelf.Enabled = True
Me.cberrocad.Enabled = True
Me.cbnf.Enabled = True
Me.cberroped.Enabled = True
Me.cbveiculo.Enabled = True
Me.frmobs.Enabled = True
Me.txtobs.Enabled = True
Me.txtsenha.Enabled = True
Me.txtsenha2.Enabled = True
Me.txtobs.BorderStyle = fmBorderStyleNone
Me.txtobs.SpecialEffect = fmSpecialEffectSunken

'desabilitando os botoes
Me.cmdnovo.Visible = False
Me.cmdeditar.Visible = False

End Sub

 
Postado : 23/07/2018 6:24 am
(@klarc28)
Posts: 0
New Member
 

Cinco dicas que foram muito úteis para mim:

1) Quando não sei fazer algo no VBA, vou ao menu EXIBIÇÃO >> MACROS >> GRAVAR MACRO
Faço o que eu quero aí volto ao menu EXIBIÇÃO >> MACROS >> PARAR GRAVAÇÃO
Aperto Alt + F11 e vejo como a macro fez aquilo. Tento entender e tento adaptar.

2) Quando vou criar um código e o resultado não está saindo como o esperado, entro no código e vou apertando F8 para executar passo a passo, aí vou passando o mouse sobre as variáveis para verificar se o valores delas estão corretos, já consertei milhares de códigos dessa forma.

3) Antes de executar o código, vou ao menu Depurar >> Compilar. Isso ajuda a corrigir erros mais simples, como o nome de uma variável digitado errado.

4) Declaro todas as variáveis. Isso também evita erros.

5) Sempre uso o Option Explicit lá no início. Ele me obriga a declarar as variáveis.

Private Sub cmdpesquisar_Click()
With Worksheets("base").Range("A:A")
'Set c = .Find(txtsenha.Value, LookIn:=xlValues, LookAt:=xlWhole)

If txtsenha.Text = "" Then
'MsgBox "Digite uma senha para pesquisar!", vbInformation, "SENHA EM BRANCO"

'desabilitando as opcoes
Me.txtfornecedor.Enabled = False
Me.txtdata.Enabled = False

Me.cbrecebida.Enabled = False
Me.cbrecusada.Enabled = False
Me.cbnoshow.Enabled = False
Me.frmoc.Enabled = False
Me.cbsemoc.Enabled = False
Me.cbavaria.Enabled = False
Me.cbhorario.Enabled = False
Me.cbpaletizacao.Enabled = False
Me.cbqualidade.Enabled = False
Me.cbshelf.Enabled = False
Me.cberrocad.Enabled = False
Me.cbnf.Enabled = False
Me.cberroped.Enabled = False
Me.cbveiculo.Enabled = False
Me.frmobs.Enabled = False
Me.txtobs.Enabled = False
Me.txtsenha.Enabled = False
Me.txtobs.BorderStyle = fmBorderStyleSingle
Me.txtobs.SpecialEffect = fmSpecialEffectSunken

MsgBox "Digite uma senha para pesquisar!", vbInformation, "SENHA EM BRANCO"
goto fim
Else

Set c = .Find(txtsenha.Value, LookIn:=xlValues, LookAt:=xlWhole)

'Else

If Not c Is Nothing Then

'c.Activate
Sheets("base").Visible = True
txtsenha.Value = c.Value
txtfornecedor.Value = c.Offset(0, 1).Value
txtdata.Value = c.Offset(0, 2).Value

'opcoes
'opcao recebida
If c.Offset(0, 3).Value = "1" Then
cbrecebida.Value = True
Else
cbrecebida.Value = False
End If

'opcao recusada
If c.Offset(0, 4).Value = "1" Then
cbrecusada.Value = True
Else
cbrecusada.Value = False
End If

'opcao noshow
If c.Offset(0, 5).Value = "1" Then
cbnoshow.Value = True
Else
cbnoshow.Value = False
End If

'opcoes
If c.Offset(0, 6).Value = "1" Then
cbsemoc.Value = True
Else
cbsemoc.Value = False
End If

If c.Offset(0, 7).Value = "1" Then
cbavaria.Value = True
Else
cbavaria.Value = False
End If

If c.Offset(0, 8).Value = "1" Then
cbhorario.Value = True
Else
cbhorario.Value = False
End If

If c.Offset(0, 9).Value = "1" Then
cbpaletizacao.Value = True
Else
cbpaletizacao.Value = False
End If

If c.Offset(0, 10).Value = "1" Then
cbqualidade.Value = True
Else
cbqualidade.Value = False
End If

If c.Offset(0, 11).Value = "1" Then
cbshelf.Value = True
Else
cbshelf.Value = False
End If

If c.Offset(0, 12).Value = "1" Then
cberrocad.Value = True
Else
cberrocad.Value = False
End If

If c.Offset(0, 13).Value = "1" Then
cbnf.Value = True
Else
cbnf.Value = False
End If

If c.Offset(0, 14).Value = "1" Then
cberroped.Value = True
Else
cberroped.Value = False
End If

If c.Offset(0, 15).Value = "1" Then
cbveiculo.Value = True
Else
cbveiculo.Value = False
End If

Sheets("base").Visible = False

Else

'Limpar as caixas de texto
txtsenha.Value = Empty
txtfornecedor.Value = Empty
txtdata.Value = Empty

MsgBox "Verifique a senha digitada!", vbCritical, "SENHA NÃO LOCALIZADA"

'foco na senha
txtsenha.SetFocus
End If
End If
End With

'habilitando as opcoes
Me.txtfornecedor.Enabled = False
Me.txtdata.Enabled = False
Me.cbrecebida.Enabled = True
Me.cbrecusada.Enabled = True
Me.cbnoshow.Enabled = True
Me.frmoc.Enabled = True
Me.cbsemoc.Enabled = True
Me.cbavaria.Enabled = True
Me.cbhorario.Enabled = True
Me.cbpaletizacao.Enabled = True
Me.cbqualidade.Enabled = True
Me.cbshelf.Enabled = True
Me.cberrocad.Enabled = True
Me.cbnf.Enabled = True
Me.cberroped.Enabled = True
Me.cbveiculo.Enabled = True
Me.frmobs.Enabled = True
Me.txtobs.Enabled = True
Me.txtsenha.Enabled = True
Me.txtsenha2.Enabled = True
Me.txtobs.BorderStyle = fmBorderStyleNone
Me.txtobs.SpecialEffect = fmSpecialEffectSunken

'desabilitando os botoes
Me.cmdnovo.Visible = False
Me.cmdeditar.Visible = False
fim:
End Sub
 
Postado : 23/07/2018 6:35 am
(@gfsouza86)
Posts: 132
Estimable Member
Topic starter
 

Boa tarde
Funcionou com o acrescimo que voce fez do goto fim.......fim:

Muito obrigado!

Ultima pergunta: qual o comando que você utiliza para limpar uma textbox no formulario?
Já coloquei

textbox.value=null
textbox.value=empty
textbox.value=""

e em todas elas ele escreve FALSO ao invés de deixar sem nada

 
Postado : 23/07/2018 1:25 pm
(@klarc28)
Posts: 0
New Member
 
textbox.text=""

Mas verifique se em alguma parte do seu código você não colocou :

textbox.value=false

Se tiver isso, mude essa linha.

Às vezes você faz algo acima, como por exemplo, limpa a caixa de texto, mas abaixo você escreve que o valor dessa caixa vai ser falso. Aí aquilo que ficou acima foi feito, mas o que ficou abaixo foi feito por último e foi o que ficou valendo.

Outra dica. A cada if ou else chame um procedimento diferente. Isso vai facilitar bastante a leitura do código e correção de erros. Do jeito que está seu código, fica difícil saber onde termina cada If. Por exemplo:

sub geral (  )
if tal coisa then

call procedimento1

else

call procedimento2
end if

End Sub

sub procedimento1 (  )
'fazer tal coisa
end sub

sub procedimento2 (  )
'fazer tal coisa
end sub
 
Postado : 23/07/2018 1:31 pm
(@gfsouza86)
Posts: 132
Estimable Member
Topic starter
 

klarc28 realmente existia um codigo false após a instrucao que eu estava verificando

Pelo que entendi na dica dos procedimentos seria algo assim

sub geral (  )
if cb1.value=true
call procedimento1
else
call procedimento2
end if
End Sub

sub procedimento1 (  )
txtsexo="masculino"
end sub

sub procedimento2 (  )
txtsexo="feminino"
end sub

Mais uma duvida:
Primeiro escolho uma opção em PRIMEIRA ESCOLHA

Após escolher uma opção as opçõe da SEGUNDA ESCOLHA são liberadas, porem se eu escolher:

*RECEBIDA todas as opções da SEGUNDA ESCOLHA são liberadas, porem se eu marcar alguma ocorrencia a opção SEM OCORRÊNCIA fica desabilitada
*RECUSADA a opção SEM OCORRENCIA deve ficar inativa
*NAO COMPARECEU nao é habilitada nenhuma opção da SEGUNDA ESCOLHA

O erro acontece quando eu marco RECUSADA e clico em alguma ocorrencia e desmarco a opção escolhida, ai a opção SEM OCORRENCIA fica habilitada para ser escolhida

O que eu preciso é que se eu marcar a opção RECUSADA a opção SEM OCORRENCIA fique sempre inativa

 
Postado : 26/07/2018 2:54 pm
(@klarc28)
Posts: 0
New Member
 

Quando eu disse chamar um procedimento, não estava me referindo à caixa de texto.
O procedimento1 seria, por exemplo, para fazer o que deve ser feito se a senha estiver preenchida e o procedimento2 seria, por exemplo, para fazer aquilo que deve ser feito se a senha não estiver preenchida. Quando há muitos ifs e elses, chamar procedimentos deixa a parte que tem os ifs mais curta e facilita a leitura na hora de ver onde começa e onde termina um if.

Notei que você usa o prefixo cb para checkbox.
Recomendo que você utilize os seguintes prefixos:

cbo - combobox
txt - textbox
chk - checkbox
cmd - command button
frm - userform
fra - frame
opt - option button
img - image
lbl - label
lst - listbox
lsw - listview

Notei também que seu código não possui indentação. Se tiver interesse, pesquise na internet sobre o que é isso.
Trata-se de espaçamento à esquerda para deixar mais claro o que está dentro de quê.

Private Sub cb1_Click()
    If Me.cb1.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb2.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb3.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb4.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cbrecusada.Value = True Then
        Me.cbsem.Enabled = False
        
    Else
        Me.cbsem.Enabled = True
    End If
End Sub

Private Sub cb2_Click()
    If Me.cb2.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb3.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb4.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb1.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cbrecusada.Value = True Then
        Me.cbsem.Enabled = False
        
    Else
        Me.cbsem.Enabled = True
    End If
End Sub

Private Sub cb3_Click()
    If Me.cb3.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb4.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb1.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb2.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cbrecusada.Value = True Then
        Me.cbsem.Enabled = False
        
    Else
        Me.cbsem.Enabled = True
    End If
End Sub

Private Sub cb4_Click()
    If Me.cb4.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb1.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb2.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cb3.Value = True Then
        Me.cbsem.Enabled = False
    ElseIf Me.cbrecusada.Value = True Then
        Me.cbsem.Enabled = False
        
    Else
        Me.cbsem.Enabled = True
    End If
End Sub

Private Sub cbnao_Click()
    If cbnao.Value = True Then
        Me.cbrecusada.Value = False
        Me.cbrecebida.Value = False
        
        'bloqueando as opcoes
        Me.cbsem.Enabled = False
        Me.cb1.Enabled = False
        Me.cb2.Enabled = False
        Me.cb3.Enabled = False
        Me.cb4.Enabled = False
        
        'limpando as opcoes
        Me.cbsem.Value = False
        Me.cb1.Value = False
        Me.cb2.Value = False
        Me.cb3.Value = False
        Me.cb4.Value = False
        
    Else
        Me.cbnao.Value = False
        
        'desbloqueando as opcoes
        'Me.cbsem.Enabled = True
        'Me.cb1.Enabled = True
        'Me.cb2.Enabled = True
        'Me.cb3.Enabled = True
        'Me.cb4.Enabled = True
    End If
End Sub

Private Sub cbrecebida_Click()
    If Me.cbrecebida.Value = True Then
        Me.cbrecusada.Value = False
        Me.cbnao.Value = False
        
        'habilitando as opcoes
        Me.cbsem.Enabled = True
        Me.cb1.Enabled = True
        Me.cb2.Enabled = True
        Me.cb3.Enabled = True
        Me.cb4.Enabled = True
        
    Else
        Me.cbrecebida.Value = False
        
        'desabilitando as opcoes
        Me.cbsem.Enabled = False
        Me.cb1.Enabled = False
        Me.cb2.Enabled = False
        Me.cb3.Enabled = False
        Me.cb4.Enabled = False
    End If
End Sub

Private Sub cbrecusada_Click()
    If cbrecusada.Value = True Then
        Me.cbrecebida.Value = False
        Me.cbnao.Value = False
        Me.cbsem.Value = False
        Me.cbsem.Enabled = False
        
        'habilitando as opcoes
        Me.cb1.Enabled = True
        Me.cb2.Enabled = True
        Me.cb3.Enabled = True
        Me.cb4.Enabled = True
        
    Else
        Me.cbsem.Enabled = True
        Me.cbrecusada.Value = False
        
        'desabilitando as opcoes
        'habilitando as opcoes
        Me.cbsem.Enabled = False
        Me.cb1.Enabled = False
        Me.cb2.Enabled = False
        Me.cb3.Enabled = False
        Me.cb4.Enabled = False
    End If
End Sub

Private Sub cbsem_Click()
    If Me.cbsem.Value = True Then
        Me.cb1.Enabled = False
        Me.cb2.Enabled = False
        Me.cb3.Enabled = False
        Me.cb4.Enabled = False
        
        Me.cb1.Value = False
        Me.cb2.Value = False
        Me.cb3.Value = False
        Me.cb4.Value = False
    Else
        Me.cb1.Enabled = True
        Me.cb2.Enabled = True
        Me.cb3.Enabled = True
        Me.cb4.Enabled = True
    End If
End Sub



Private Sub UserForm_Initialize()
    Me.cbsem.Enabled = False
    Me.cb1.Enabled = False
    Me.cb2.Enabled = False
    Me.cb3.Enabled = False
    Me.cb4.Enabled = False
End Sub

 
Postado : 26/07/2018 4:30 pm
(@gfsouza86)
Posts: 132
Estimable Member
Topic starter
 

klarc28

Desculpa a demora em responder mas estava meio sem tempo

Muito obrigado pela ajuda e pode deixar que irei buscar conhecimento nos pontos que você me indicou!

 
Postado : 06/08/2018 11:36 am