Notifications
Clear all

Erro em Tempo de Execução '1004'

10 Posts
2 Usuários
0 Reactions
1,479 Visualizações
(@gabrielcj)
Posts: 19
Eminent Member
Topic starter
 

Bom dia dr.,

Estou com um problema nesse trecho do código, pois está dando o Erro 1004 de tempo de execução,

If Not IsEmpty(.Cells(indiceRegistro, colRazSocial)) Then

segue o código completo abaixo:

Private Sub CarregaDados()
'carrega os dados do primeiro registro
    With wsCadastroFornecedores
        If Not IsEmpty(.Cells(indiceRegistro, colRazSocial)) Then
            Me.txtCodigo.Text = .Cells(indiceRegistro, colCodigo).Value
            Me.txtRazSocial.Text = .Cells(indiceRegistro, colRazSocial).Value
            Me.txtNomeFant.Text = .Cells(indiceRegistro, colNomeFant).Value
            Me.txtCnpjCpf.Text = .Cells(indiceRegistro, colCnpjCpf).Value
            Me.txtInscMun.Text = .Cells(indiceRegistro, colInscMun).Value
            Me.txtInscEst.Text = .Cells(indiceRegistro, colInscEst).Value
            Me.txtAtivSetor.Text = .Cells(indiceRegistro, colAtivSetor).Value
            Me.txtLinProd.Text = .Cells(indiceRegistro, colLinProd).Value
            Me.txtTel1.Text = .Cells(indiceRegistro, colTel1).Value
            Me.txtTel2.Text = .Cells(indiceRegistro, colTel2).Value
            Me.txtEndereco.Text = .Cells(indiceRegistro, colEndereco).Value
            Me.txtNum.Text = .Cells(indiceRegistro, colNum).Value
            Me.txtComplemento.Text = .Cells(indiceRegistro, colComplemento).Value
            Me.txtBairro.Text = .Cells(indiceRegistro, colBairro).Value
            Me.txtCidade.Text = .Cells(indiceRegistro, colCidade).Value
            Me.txtEstado.Text = .Cells(indiceRegistro, colEstado).Value
            Me.txtCep.Text = .Cells(indiceRegistro, colCep).Value
            Me.txtTipoCompra.Text = .Cells(indiceRegistro, colTipoCompra).Value
            Me.txtSite.Text = .Cells(indiceRegistro, colSite).Value
            Me.txtEmail.Text = .Cells(indiceRegistro, colEmail).Value
            Me.txtBanco.Text = .Cells(indiceRegistro, colBanco).Value
            Me.txtAgencia.Text = .Cells(indiceRegistro, colAgencia).Value
            Me.txtContaCorrente.Text = .Cells(indiceRegistro, colContaCorrente).Value
        End If
    End With
Call AtualizaRegistroAtual
End Sub

Aguardo contato e desde já agradeço a vocês.

 
Postado : 21/11/2013 5:25 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Esse erro ocorre quando é tentado uma "atividade" que não pode ser completada.
Em seu código acima, não há, aparentemente, nada errado.
Podemos supor que uma (ou algumas) das variaveis utilizadas, não tenham sido corretamente declaradas ou, por exemplo, indiceRegistro retorna o valor Zero

 
Postado : 21/11/2013 5:41 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Sem ver o projeto inteiro para saber de onde estão vindo os dados carregados nas variaveis, fica um pouco dificil, então sugiro eliminar a condição If e colocar um MsgBox para ver se o que ese encontra nas variaveis indiceRegistro, colRazSocial estão corretas :

    Private Sub CarregaDados()
    'carrega os dados do primeiro registro
        With wsCadastroFornecedores
            'If Not IsEmpty(.Cells(indiceRegistro, colRazSocial)) Then

MsgBox .Cells(indiceRegistro, colRazSocial)

                Me.txtCodigo.Text = .Cells(indiceRegistro, colCodigo).Value
                Me.txtRazSocial.Text = .Cells(indiceRegistro, colRazSocial).Value
                Me.txtNomeFant.Text = .Cells(indiceRegistro, colNomeFant).Value
                Me.txtCnpjCpf.Text = .Cells(indiceRegistro, colCnpjCpf).Value
                Me.txtInscMun.Text = .Cells(indiceRegistro, colInscMun).Value
                Me.txtInscEst.Text = .Cells(indiceRegistro, colInscEst).Value
                Me.txtAtivSetor.Text = .Cells(indiceRegistro, colAtivSetor).Value
                Me.txtLinProd.Text = .Cells(indiceRegistro, colLinProd).Value
                Me.txtTel1.Text = .Cells(indiceRegistro, colTel1).Value
                Me.txtTel2.Text = .Cells(indiceRegistro, colTel2).Value
                Me.txtEndereco.Text = .Cells(indiceRegistro, colEndereco).Value
                Me.txtNum.Text = .Cells(indiceRegistro, colNum).Value
                Me.txtComplemento.Text = .Cells(indiceRegistro, colComplemento).Value
                Me.txtBairro.Text = .Cells(indiceRegistro, colBairro).Value
                Me.txtCidade.Text = .Cells(indiceRegistro, colCidade).Value
                Me.txtEstado.Text = .Cells(indiceRegistro, colEstado).Value
                Me.txtCep.Text = .Cells(indiceRegistro, colCep).Value
                Me.txtTipoCompra.Text = .Cells(indiceRegistro, colTipoCompra).Value
                Me.txtSite.Text = .Cells(indiceRegistro, colSite).Value
                Me.txtEmail.Text = .Cells(indiceRegistro, colEmail).Value
                Me.txtBanco.Text = .Cells(indiceRegistro, colBanco).Value
                Me.txtAgencia.Text = .Cells(indiceRegistro, colAgencia).Value
                Me.txtContaCorrente.Text = .Cells(indiceRegistro, colContaCorrente).Value
'End if
        End With
    Call AtualizaRegistroAtual
    End Sub

Faça os teste e retorne.

[]s

 
Postado : 21/11/2013 5:42 am
(@gabrielcj)
Posts: 19
Eminent Member
Topic starter
 

Tentei fazer oq me disse, para colocar o Msgbox, mas deu na mesma.

Vou colocar o código completo abaixo:

Option Explicit

'define constantes para controlar as colunas de dados
Const colCodigo As Integer = 1
Const colRazSocial As Integer = 2
Const colNomeFant As Integer = 3
Const colCnpjCpf As Integer = 4
Const colInscMun As Integer = 5
Const colInscEst As Integer = 6
Const colAtivSetor As Integer = 7
Const colLinProd As Integer = 8
Const colTel1 As Integer = 9
Const colTel2 As Integer = 10
Const colEndereco As Integer = 11
Const colNum As Integer = 12
Const colComplemento As Integer = 13
Const colBairro As Integer = 14
Const colCidade As Integer = 15
Const colEstado As Integer = 16
Const colCep As Integer = 17
Const colContato As Integer = 18
Const colTipoCompra As Integer = 19
Const colSite As Integer = 20
Const colEmail As Integer = 21
Const colBanco As Integer = 22
Const colAgencia As Integer = 23
Const colContaCorrente As Integer = 24
Const indiceMinimo As Byte = 2

'define variavies para controlar a
Private alterar As Boolean
Private novo As Boolean
Private excluir As Boolean
'Private pesquisar As Boolean 'Por enquanto a função pesquisar estará de fora!

'define as constantes para as cores do textbox
Const corDesabilitaTextBox As Long = -2147483633
Const corHabilitaTextBox As Long = -2147483643

'define a planilha usada e o indice do registro
Private wsCadastroFornecedores As Worksheet
Private indiceRegistro As Long

Private Sub btnAlterar_Click()
    alterar = True
    If txtCodigo.Text <> vbNullString And txtCodigo.Text <> "" Then
        Call HabilitaControles
        Call DesabilitaBotoesAlteracao
        'dá o foco ao primeiro controle de dados
        txtRazSocial.SetFocus
    Else
        lblMensagem.Caption = "Não há registro a ser alterado "
    End If
End Sub

Private Sub btnAnterior_Click()
If indiceRegistro > indiceMinimo Then
indiceRegistro = indiceRegistro - 1
End If
If indiceRegistro > 1 Then
Call CarregaRegistro
End If
End Sub

Private Sub btnCancelar_Click()
    btnOk.Enabled = False
    btnCancelar.Enabled = False
    Call DesabilitaControles
    Call CarregaDados
    Call HabilitaBotoesAlteracao
End Sub

Private Sub btnExcluir_Click()
    excluir = True
    If txtCodigo.Text <> vbNullString And txtCodigo.Text <> "" Then
        Call DesabilitaBotoesAlteracao
        lblMensagem.Caption = "Você confirma a exclusão deste registro. (Para excluir clique no botão OK.) "
    Else
        lblMensagem.Caption = "Não existe registro a ser excluído"
    End If
End Sub

Private Sub btnNovo_Click()
    novo = True
    excluir = False
    alterar = False
'    pesquisar = False
    Call LimpaControles
    Call HabilitaControles
    Call DesabilitaBotoesAlteracao
    'dá o foco ao primeiro controle de dados
    txtRazSocial.SetFocus
End Sub

Private Sub btnOk_Click()

'valida campos do formulário
If ValidaCamposFormulario = False Then
Exit Sub
End If
Dim proximoId As Long

'Alterar registros
If alterar = True Then
Call SalvaRegistro(CLng(txtCodigo.Text), indiceRegistro)
lblMensagem.Caption = "O Registro alterado com sucesso."
alterar = False
End If

'Novo registro
If novo = True Then
proximoId = ObterProximoId

'pega a próxima linha
Dim proximoIndice As Long
proximoIndice = wsCadastroFornecedores.UsedRange.Rows.Count + 1
Call SalvaRegistro(proximoId, proximoIndice)
txtCodigo = proximoId
lblMensagem.Caption = "Novo registro salvo com sucesso."
novo = False
End If

'Excluir um registro
If excluir = True Then
    Dim resultado As VbMsgBoxResult
    resultado = MsgBox("Deseja excluir o registro nº " & txtCodigo.Text & "?", vbYesNo, ",Confirmação ")
    If resultado = vbYes Then
        wsCadastroFornecedores.Range(wsCadastroFornecedores.Cells(indiceRegistro, colCodigo), wsCadastroFornecedores.Cells(indiceRegistro, colCodigo)).EntireRow.Delete
        Call CarregaDados
        lblMensagem.Caption = "O Registro escolhido foi excluído com sucesso."
    End If
    excluir = False
End If
Call HabilitaBotoesAlteracao
Call DesabilitaControles
End Sub

Private Sub btnPrimeira_Click()
Call limpaMensagem
indiceRegistro = indiceMinimo
If indiceRegistro > 1 Then
Call CarregaRegistro
End If
End Sub

Private Sub btnProximo_Click()
Call limpaMensagem
If indiceRegistro < wsCadastroFornecedores.UsedRange.Rows.Count Then
indiceRegistro = indiceRegistro + 1
End If
If indiceRegistro > 1 Then
Call CarregaRegistro
End If
End Sub

Private Sub btnUltima_Click()
Call limpaMensagem
indiceRegistro = wsCadastroFornecedores.UsedRange.Rows.Count
If indiceRegistro > 1 Then
Call CarregaRegistro
End If
End Sub

Private Sub UserForm_Initialize()
    
    novo = False
    alterar = False
    excluir = False
    'pesquisar = False
    Set wsCadastroFornecedores = ThisWorkbook.Worksheets("BD_FORNECEDORES")
    Call HabilitaBotoesAlteracao
    Call CarregaDados
    Call DesabilitaControles
End Sub

Private Sub HabilitaBotoesAlteracao()
'habilita os botões de alteração
    btnAlterar.Enabled = True
    btnExcluir.Enabled = True
    btnNovo.Enabled = True
    btnPesquisar.Enabled = True
    btnOk.Enabled = False
    btnCancelar.Enabled = False
End Sub

Private Sub CarregaDados()
'carrega os dados do primeiro registro
    With wsCadastroFornecedores
        If Not IsEmpty(.Cells(indiceRegistro, colRazSocial)) Then
            Me.txtCodigo.Text = .Cells(indiceRegistro, colCodigo).Value
            Me.txtRazSocial.Text = .Cells(indiceRegistro, colRazSocial).Value
            Me.txtNomeFant.Text = .Cells(indiceRegistro, colNomeFant).Value
            Me.txtCnpjCpf.Text = .Cells(indiceRegistro, colCnpjCpf).Value
            Me.txtInscMun.Text = .Cells(indiceRegistro, colInscMun).Value
            Me.txtInscEst.Text = .Cells(indiceRegistro, colInscEst).Value
            Me.txtAtivSetor.Text = .Cells(indiceRegistro, colAtivSetor).Value
            Me.txtLinProd.Text = .Cells(indiceRegistro, colLinProd).Value
            Me.txtTel1.Text = .Cells(indiceRegistro, colTel1).Value
            Me.txtTel2.Text = .Cells(indiceRegistro, colTel2).Value
            Me.txtEndereco.Text = .Cells(indiceRegistro, colEndereco).Value
            Me.txtNum.Text = .Cells(indiceRegistro, colNum).Value
            Me.txtComplemento.Text = .Cells(indiceRegistro, colComplemento).Value
            Me.txtBairro.Text = .Cells(indiceRegistro, colBairro).Value
            Me.txtCidade.Text = .Cells(indiceRegistro, colCidade).Value
            Me.txtEstado.Text = .Cells(indiceRegistro, colEstado).Value
            Me.txtCep.Text = .Cells(indiceRegistro, colCep).Value
            Me.txtTipoCompra.Text = .Cells(indiceRegistro, colTipoCompra).Value
            Me.txtSite.Text = .Cells(indiceRegistro, colSite).Value
            Me.txtEmail.Text = .Cells(indiceRegistro, colEmail).Value
            Me.txtBanco.Text = .Cells(indiceRegistro, colBanco).Value
            Me.txtAgencia.Text = .Cells(indiceRegistro, colAgencia).Value
            Me.txtContaCorrente.Text = .Cells(indiceRegistro, colContaCorrente).Value
        End If
    End With
Call AtualizaRegistroAtual
End Sub

Private Sub DesabilitaControles()

    Me.txtCodigo.Locked = True
    Me.txtRazSocial.Locked = True
    Me.txtNomeFant.Locked = True
    Me.txtCnpjCpf.Locked = True
    Me.txtInscMun.Locked = True
    Me.txtInscEst.Locked = True
    Me.txtAtivSetor.Locked = True
    Me.txtLinProd.Locked = True
    Me.txtTel1.Locked = True
    Me.txtTel2.Locked = True
    Me.txtEndereco.Locked = True
    Me.txtNum.Locked = True
    Me.txtComplemento.Locked = True
    Me.txtBairro.Locked = True
    Me.txtCidade.Locked = True
    Me.txtCep.Locked = True
    Me.txtTipoCompra.Locked = True
    Me.txtSite.Locked = True
    Me.txtEmail.Locked = True
    Me.txtBanco.Locked = True
    Me.txtAgencia.Locked = True
    Me.txtContaCorrente.Locked = True
        
    'altera a cor dos controles
    Me.txtCodigo.BackColor = corDesabilitaTextBox
    Me.txtRazSocial.BackColor = corDesabilitaTextBox
    Me.txtNomeFant.BackColor = corDesabilitaTextBox
    Me.txtCnpjCpf.BackColor = corDesabilitaTextBox
    Me.txtInscMun.BackColor = corDesabilitaTextBox
    Me.txtInscEst.BackColor = corDesabilitaTextBox
    Me.txtAtivSetor.BackColor = corDesabilitaTextBox
    Me.txtLinProd.BackColor = corDesabilitaTextBox
    Me.txtTel1.BackColor = corDesabilitaTextBox
    Me.txtTel2.BackColor = corDesabilitaTextBox
    Me.txtEndereco.BackColor = corDesabilitaTextBox
    Me.txtNum.BackColor = corDesabilitaTextBox
    Me.txtComplemento.BackColor = corDesabilitaTextBox
    Me.txtBairro.BackColor = corDesabilitaTextBox
    Me.txtCidade.BackColor = corDesabilitaTextBox
    Me.txtCep.BackColor = corDesabilitaTextBox
    Me.txtTipoCompra.BackColor = corDesabilitaTextBox
    Me.txtSite.BackColor = corDesabilitaTextBox
    Me.txtEmail.BackColor = corDesabilitaTextBox
    Me.txtBanco.BackColor = corDesabilitaTextBox
    Me.txtAgencia.BackColor = corDesabilitaTextBox
    Me.txtContaCorrente.BackColor = corDesabilitaTextBox
    
End Sub

Private Sub HabilitaControles()

    Me.txtCodigo.Locked = False
    Me.txtRazSocial.Locked = False
    Me.txtNomeFant.Locked = False
    Me.txtCnpjCpf.Locked = False
    Me.txtInscMun.Locked = False
    Me.txtInscEst.Locked = False
    Me.txtAtivSetor.Locked = False
    Me.txtLinProd.Locked = False
    Me.txtTel1.Locked = False
    Me.txtTel2.Locked = False
    Me.txtEndereco.Locked = False
    Me.txtNum.Locked = False
    Me.txtComplemento.Locked = False
    Me.txtBairro.Locked = False
    Me.txtCidade.Locked = False
    Me.txtCep.Locked = False
    Me.txtTipoCompra.Locked = False
    Me.txtSite.Locked = False
    Me.txtEmail.Locked = False
    Me.txtBanco.Locked = False
    Me.txtAgencia.Locked = False
    Me.txtContaCorrente.Locked = False
        
    'altera a cor dos controles
    Me.txtCodigo.BackColor = corHabilitaTextBox
    Me.txtRazSocial.BackColor = corHabilitaTextBox
    Me.txtNomeFant.BackColor = corHabilitaTextBox
    Me.txtCnpjCpf.BackColor = corHabilitaTextBox
    Me.txtInscMun.BackColor = corHabilitaTextBox
    Me.txtInscEst.BackColor = corHabilitaTextBox
    Me.txtAtivSetor.BackColor = corHabilitaTextBox
    Me.txtLinProd.BackColor = corHabilitaTextBox
    Me.txtTel1.BackColor = corHabilitaTextBox
    Me.txtTel2.BackColor = corHabilitaTextBox
    Me.txtEndereco.BackColor = corHabilitaTextBox
    Me.txtNum.BackColor = corHabilitaTextBox
    Me.txtComplemento.BackColor = corHabilitaTextBox
    Me.txtBairro.BackColor = corHabilitaTextBox
    Me.txtCidade.BackColor = corHabilitaTextBox
    Me.txtCep.BackColor = corHabilitaTextBox
    Me.txtTipoCompra.BackColor = corHabilitaTextBox
    Me.txtSite.BackColor = corHabilitaTextBox
    Me.txtEmail.BackColor = corHabilitaTextBox
    Me.txtBanco.BackColor = corHabilitaTextBox
    Me.txtAgencia.BackColor = corHabilitaTextBox
    Me.txtContaCorrente.BackColor = corHabilitaTextBox
    
End Sub

Private Sub SalvaRegistro(ByVal id As Long, ByVal indice As Long)
With wsCadastroFornecedores
    .Cells(indice, colCodigo).Value = id
    .Cells(indice, colRazSocial).Value = Me.txtRazSocial.Text
    .Cells(indice, colNomeFant).Value = Me.txtNomeFant.Text
    .Cells(indice, colCnpjCpf).Value = Me.txtCnpjCpf.Text
    .Cells(indice, colInscMun).Value = Me.txtInscMun.Text
    .Cells(indice, colInscEst).Value = Me.txtInscEst.Text
    .Cells(indice, colAtivSetor).Value = Me.txtAtivSetor.Text
    .Cells(indice, colLinProd).Value = Me.txtLinProd.Text
    .Cells(indice, colTel1).Value = Me.txtTel1.Text
    .Cells(indice, colTel2).Value = Me.txtTel2.Text
    .Cells(indice, colEndereco).Value = Me.txtEndereco.Text
    .Cells(indice, colNum).Value = Me.txtNum.Text
    .Cells(indice, colComplemento).Value = Me.txtComplemento.Text
    .Cells(indice, colBairro).Value = Me.txtBairro.Text
    .Cells(indice, colCidade).Value = Me.txtCidade.Text
    .Cells(indice, colEstado).Value = Me.txtEstado.Text
    .Cells(indice, colCep).Value = Me.txtCep.Text
    .Cells(indice, colContato).Value = Me.txtContato.Text
    .Cells(indice, colTipoCompra).Value = Me.txtTipoCompra.Text
    .Cells(indice, colSite).Value = Me.txtSite.Text
    .Cells(indice, colEmail).Value = Me.txtEmail.Text
    .Cells(indice, colBanco).Value = Me.txtBanco.Text
    .Cells(indice, colAgencia).Value = Me.txtAgencia.Text
    .Cells(indice, colContaCorrente).Value = Me.txtContaCorrente.Text
    
End With
Call AtualizaRegistroAtual
End Sub

Private Function ObterProximoId() As Long
Dim rangeIds As Range
'pega o range que se refere a toda a coluna do código (id)
Set rangeIds = wsCadastroFornecedores.Range(wsCadastroFornecedores.Cells(indiceMinimo, colCodigo), wsCadastroFornecedores.Cells(wsCadastroFornecedores.UsedRange.Rows.Count, colCodigo))
ObterProximoId = WorksheetFunction.Max(rangeIds) + 1
End Function

Private Sub AtualizaRegistroAtual()
lblRegistro.Caption = indiceRegistro - 1 & " de " & wsCadastroFornecedores.UsedRange.Rows.Count - 1
End Sub

Private Function ValidaCamposFormulario() As Boolean
If Me.txtRazSocial.Value = "" Then
    Me.txtNome.SetFocus
    MsgBox " 'Razão Social' é um campo obrigatório. ", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtNomeFant.Value = "" Then
    Me.txtNomeFant.SetFocus
    MsgBox " 'Nome Fantasia' é um campo obrigatório.", vbOKOnly, "Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtCnpjCpf.Value = "" Then
    Me.txtCnpjCpf.SetFocus
    MsgBox "'CNPJ ou CPF' é um campo obrigatório. ", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtAtivSetor.Value = "" Then
    Me.txtAtivSetor.SetFocus
    MsgBox "'Atividade / Setor' é um campo obrigatório. ", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtLinProd.Value = "" Then
    Me.txtLinProd.SetFocus
    MsgBox " 'Linha de Produção' é um campo obrigatório. ", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtTel1.Value = "" Then
    Me.txtTel1.SetFocus
    MsgBox "'Telefone 1' é um campo obrigatório.", vbOKOnly, "Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtEndereco.Value = "" Then
    Me.txtEndereco.SetFocus
    MsgBox "'Endereço' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtNum.Value = "" Then
    Me.txtNum.SetFocus
    MsgBox "'Nº do Endereço' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtBairro.Value = "" Then
    Me.txtBairro.SetFocus
    MsgBox "'Bairro' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtCidade.Value = "" Then
    Me.txtCidade.SetFocus
    MsgBox "'Cidade' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtEstado.Value = "" Then
    Me.txtEstado.SetFocus
    MsgBox "'Estado' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtCep.Value = "" Then
    Me.txtCep.SetFocus
    MsgBox "'CEP' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtContato.Value = "" Then
    Me.txtContato.SetFocus
    MsgBox "'Contato' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtTipoCompra.Value = "" Then
    Me.txtTipoCompra.SetFocus
    MsgBox "'Tipo de Compra' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtBanco.Value = "" Then
    Me.txtBanco.SetFocus
    MsgBox "'Banco' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtAgencia.Value = "" Then
    Me.txtAgencia.SetFocus
    MsgBox "'Agência' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
ElseIf Me.txtContaCorrente.Value = "" Then
    Me.txtContaCorrente.SetFocus
    MsgBox "'Conta Corrente' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
    ValidaCamposFormulario = False
    Exit Function
End If
ValidaCamposFormulario = True
End Function
 
Postado : 21/11/2013 5:53 am
(@gabrielcj)
Posts: 19
Eminent Member
Topic starter
 

Esse erro ocorre quando é tentado uma "atividade" que não pode ser completada.
Em seu código acima, não há, aparentemente, nada errado.
Podemos supor que uma (ou algumas) das variaveis utilizadas, não tenham sido corretamente declaradas ou, por exemplo, indiceRegistro retorna o valor Zero

o IndiceRegistro está retornando Zero, como faço para corrigi-lo?

 
Postado : 21/11/2013 5:59 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

O ideal seria ter o modelo compactado para analise e então dar uma resposta certeira, desta forma vamos ficar em suposições.

Ou seja, onde está a Macro "Call CarregaRegistro" ?

o IndiceRegistro está retornando Zero, como faço para corrigi-lo? - Sugiro seguir a rotina Passo a Passo utilizando a Tecla "F8" e ir conferindo o Valor da Variavel IndiceRegistro para saber se está vindo como zero desde o inicio ou está sendo alterada pelo caminho.

 
Postado : 21/11/2013 6:11 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Me parece que a rotina "CarregaDados" não é do projeto VBA inicial.
Todas as demais chamam "CarregaRegistro" (além de outras sub rotinas não postadas).
Experimente alterar a rotina CarregaDados assim:

    Private Sub CarregaDados()
    'carrega os dados do primeiro registro
    If indiceRegistro = 0 Then indiceRegistro = indiceMinimo
    
        With wsCadastroFornecedores
            If Not IsEmpty(.Cells(indiceRegistro, colRazSocial)) Then
                Me.txtCodigo.Text = .Cells(indiceRegistro, colCodigo).Value
                Me.txtRazSocial.Text = .Cells(indiceRegistro, colRazSocial).Value
                Me.txtNomeFant.Text = .Cells(indiceRegistro, colNomeFant).Value
                Me.txtCnpjCpf.Text = .Cells(indiceRegistro, colCnpjCpf).Value
                Me.txtInscMun.Text = .Cells(indiceRegistro, colInscMun).Value
                Me.txtInscEst.Text = .Cells(indiceRegistro, colInscEst).Value
                Me.txtAtivSetor.Text = .Cells(indiceRegistro, colAtivSetor).Value
                Me.txtLinProd.Text = .Cells(indiceRegistro, colLinProd).Value
                Me.txtTel1.Text = .Cells(indiceRegistro, colTel1).Value
                Me.txtTel2.Text = .Cells(indiceRegistro, colTel2).Value
                Me.txtEndereco.Text = .Cells(indiceRegistro, colEndereco).Value
                Me.txtNum.Text = .Cells(indiceRegistro, colNum).Value
                Me.txtComplemento.Text = .Cells(indiceRegistro, colComplemento).Value
                Me.txtBairro.Text = .Cells(indiceRegistro, colBairro).Value
                Me.txtCidade.Text = .Cells(indiceRegistro, colCidade).Value
                Me.txtEstado.Text = .Cells(indiceRegistro, colEstado).Value
                Me.txtCep.Text = .Cells(indiceRegistro, colCep).Value
                Me.txtTipoCompra.Text = .Cells(indiceRegistro, colTipoCompra).Value
                Me.txtSite.Text = .Cells(indiceRegistro, colSite).Value
                Me.txtEmail.Text = .Cells(indiceRegistro, colEmail).Value
                Me.txtBanco.Text = .Cells(indiceRegistro, colBanco).Value
                Me.txtAgencia.Text = .Cells(indiceRegistro, colAgencia).Value
                Me.txtContaCorrente.Text = .Cells(indiceRegistro, colContaCorrente).Value
            End If
        End With
    Call AtualizaRegistroAtual
    End Sub
 
Postado : 21/11/2013 6:12 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

No Inciar o Formulário temos a chamada para a Rotina "CarregaDados" e na sequencia desta rotina a instgrução:
If Not IsEmpty(.Cells(indiceRegistro, colRazSocial)) Then, então como ainda não foi definido o Valor desta Variável ela será sempre Zero uma vez que está na Declaração como Long sem nenhum valor inicial.
Esta variavel no projeto original é definido em uma instrução tipo :

indiceRegistro = wsCadastroFornecedores.UsedRange.Rows.Count

ou seja precisa ver em seu projeto em que passo está atribuindo o valor a esta variavel.
[]s

 
Postado : 21/11/2013 6:18 am
(@gabrielcj)
Posts: 19
Eminent Member
Topic starter
 

Me parece que a rotina "CarregaDados" não é do projeto VBA inicial.
Todas as demais chamam "CarregaRegistro" (além de outras sub rotinas não postadas).
Experimente alterar a rotina CarregaDados assim:

    Private Sub CarregaDados()
    'carrega os dados do primeiro registro
    If indiceRegistro = 0 Then indiceRegistro = indiceMinimo
    
        With wsCadastroFornecedores
            If Not IsEmpty(.Cells(indiceRegistro, colRazSocial)) Then
                Me.txtCodigo.Text = .Cells(indiceRegistro, colCodigo).Value
                Me.txtRazSocial.Text = .Cells(indiceRegistro, colRazSocial).Value
                Me.txtNomeFant.Text = .Cells(indiceRegistro, colNomeFant).Value
                Me.txtCnpjCpf.Text = .Cells(indiceRegistro, colCnpjCpf).Value
                Me.txtInscMun.Text = .Cells(indiceRegistro, colInscMun).Value
                Me.txtInscEst.Text = .Cells(indiceRegistro, colInscEst).Value
                Me.txtAtivSetor.Text = .Cells(indiceRegistro, colAtivSetor).Value
                Me.txtLinProd.Text = .Cells(indiceRegistro, colLinProd).Value
                Me.txtTel1.Text = .Cells(indiceRegistro, colTel1).Value
                Me.txtTel2.Text = .Cells(indiceRegistro, colTel2).Value
                Me.txtEndereco.Text = .Cells(indiceRegistro, colEndereco).Value
                Me.txtNum.Text = .Cells(indiceRegistro, colNum).Value
                Me.txtComplemento.Text = .Cells(indiceRegistro, colComplemento).Value
                Me.txtBairro.Text = .Cells(indiceRegistro, colBairro).Value
                Me.txtCidade.Text = .Cells(indiceRegistro, colCidade).Value
                Me.txtEstado.Text = .Cells(indiceRegistro, colEstado).Value
                Me.txtCep.Text = .Cells(indiceRegistro, colCep).Value
                Me.txtTipoCompra.Text = .Cells(indiceRegistro, colTipoCompra).Value
                Me.txtSite.Text = .Cells(indiceRegistro, colSite).Value
                Me.txtEmail.Text = .Cells(indiceRegistro, colEmail).Value
                Me.txtBanco.Text = .Cells(indiceRegistro, colBanco).Value
                Me.txtAgencia.Text = .Cells(indiceRegistro, colAgencia).Value
                Me.txtContaCorrente.Text = .Cells(indiceRegistro, colContaCorrente).Value
            End If
        End With
    Call AtualizaRegistroAtual
    End Sub

Com este acerto que você mostrou funcionou

If indiceRegistro = 0 Then indiceRegistro = indiceMinimo

Funcionou muito bem, agora vou verificar se existe mais algum erro, agradeço a todos.

 
Postado : 21/11/2013 6:23 am
(@gabrielcj)
Posts: 19
Eminent Member
Topic starter
 

No Inciar o Formulário temos a chamada para a Rotina "CarregaDados" e na sequencia desta rotina a instgrução:
If Not IsEmpty(.Cells(indiceRegistro, colRazSocial)) Then, então como ainda não foi definido o Valor desta Variável ela será sempre Zero uma vez que está na Declaração como Long sem nenhum valor inicial.
Esta variavel no projeto original é definido em uma instrução tipo :

indiceRegistro = wsCadastroFornecedores.UsedRange.Rows.Count

ou seja precisa ver em seu projeto em que passo está atribuindo o valor a esta variavel.
[]s

Entendi perfeitamente, utilizei o método em que o Reinaldo sugeriu, funcionou.

Mas guardarei está dica tbem, pode ser útil em algum momento.

 
Postado : 21/11/2013 6:26 am