deyvsonaguiar
04-10-2012 14:50:27
Segui modelo de tutorial de uma planilha de cadastro em VBA com vários botões mas alguns não funcionam. Eu queria que revisassem esse código, pois sou novato na área, to aprendendo VBA sozinho pela net e quero que essa planilha funcione, os erros que detectei foram o seguinte:
1) No botões que o cadastro de um a um, está dando o erro "Erro de compilação 'Sub' ou 'Function' não definida
2) O contador não funciona
3) O botão fechar ta com erro
4) Ele cadastra, salva na planilha a partir da linha 16, e queria que começasse a salvar a partir da linha 2 em diante.
5) Os botões Primeiro e ùltimo também não funfam...
Please, me ajudem a entender no que eu errei para que eu possa fazer outras planilhas já ciente!!! Obrigado pessoal,
meu e-mail é deyvsonaguiar@hotmail.com ou http://www.facebook.com/deyvsonaguiar
segue o código:
1) No botões que o cadastro de um a um, está dando o erro "Erro de compilação 'Sub' ou 'Function' não definida
2) O contador não funciona
3) O botão fechar ta com erro
4) Ele cadastra, salva na planilha a partir da linha 16, e queria que começasse a salvar a partir da linha 2 em diante.
5) Os botões Primeiro e ùltimo também não funfam...
Please, me ajudem a entender no que eu errei para que eu possa fazer outras planilhas já ciente!!! Obrigado pessoal,
meu e-mail é deyvsonaguiar@hotmail.com ou http://www.facebook.com/deyvsonaguiar
segue o código:
Código: Selecionar todos
Option Explicit
'Define constantes para controlar as colunas de dados
Const colNome As Integer = 2
Const colRg As Integer = 3
Const colExped As Integer = 4
Const colUf As Integer = 5
Const colNascimento As Integer = 6
Const colCpf As Integer = 7
Const colPeriodo As Integer = 8
Const colCurso As Integer = 9
Const colFuncao As Integer = 10
Const colEndereco As Integer = 11
Const colComplemento As Integer = 12
Const colFone1 As Integer = 12
Const colFone2 As Integer = 14
Const colEmail1 As Integer = 15
Const colEmail2 As Integer = 16
Const colCodigo As Integer = 1
Const indiceMinimo As Byte = 17
'define variaveis para controlar a
Private alterar As Boolean
Private novo As Boolean
Private excluir As Boolean
'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 wsplanilha_de_cadastro_de_membros_VBA As Worksheet
Private indiceRegistro As Long
Private Sub cmdAlterar_Clic()
alterar = True
If txtCodigo.Text <> vbNullString And txtCodigo1.Text <> "" Then
Call HabilitaControles
Call DesabilitaBotoesAlteracao
'dá o foco ao primeiro controle de dados
txtNome.SetFocus
Else
lblMensagem.Caption = "Não há registro a ser alterado"
End If
End Sub
Private Sub cmdAnterior_Click()
If indiceRegistro > indiceMinimo Then
indiceRegistro = indiceRegistro - 1
End If
If indiceRegistro > 1 Then
Call CarregaRegistro
End If
End Sub
Private Sub cmdCancelar_Click()
cmdOk.Enabled = False
cmdCancelar.Enabled = False
Call DesabilitaControles
Call carregaDados
Call HabilitaBotoesAlteracao
End Sub
Private Sub cmdExcluir_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 cmdNovo_Click()
novo = True
excluir = False
alterar = False
Call LimpaControles
Call HabilitaControles
Call DesabilitaBotoesAlteracao
'dá o foco ao primeiro controle de dados
txtNome.SetFocus
End Sub
Private Sub cmdOk_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 = wsplanilha_de_cadastro_de_membros_VBA.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
wsplanilha_de_cadastro_de_membros_VBA.Range(wsplanilha_de_cadastro_de_membros_VBA.Cells(indiceRegistro, colCodigo), wsplanilha_de_cadastro_de_membros_VBA.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 SalvaRegistro(ByVal id As Long, ByVal indice As Long)
With wsplanilha_de_cadastro_de_membros_VBA
.Cells(indice, colNome).Value = Me.txtNome.Text
.Cells(indice, colRg).Value = Me.txtRg.Text
.Cells(indice, colExped).Value = Me.txtExped.Text
.Cells(indice, colUf).Value = Me.txtUf.Text
.Cells(indice, colNascimento).Value = Me.txtNascimento.Text
.Cells(indice, colCpf).Value = Me.txtCpf.Text
.Cells(indice, colPeriodo).Value = Me.txtPeriodo.Text
.Cells(indice, colCurso).Value = Me.txtCurso.Text
.Cells(indice, colFuncao).Value = Me.txtFuncao.Text
.Cells(indice, colEndereco).Value = Me.txtEndereco.Text
.Cells(indice, colComplemento).Value = Me.txtComplemento.Text
.Cells(indice, colFone1).Value = Me.txtFone1.Text
.Cells(indice, colFone2).Value = Me.txtFone2.Text
.Cells(indice, colEmail1).Value = Me.txtEmail1.Text
.Cells(indice, colEmail2).Value = Me.txtEmail2.Text
.Cells(indice, colCodigo).Value = id
End With
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 = wsplanilha_de_cadastro_de_membros_VBA.Range(wsplanilha_de_cadastro_de_membros_VBA.Cells(indiceMinimo, colCodigo), wsplanilha_de_cadastro_de_membros_VBA.Cells(wsplanilha_de_cadastro_de_membros_VBA.UsedRange.Rows.Count, colCodigo))
ObterProximoId = WorksheetFunction.Max(rangeIds) + 1
End Function
Private Sub cmdPrimeiro_Click()
Call limpaMensagem
indiceRegistro = indiceMinimo
If indiceRegistro > 1 Then
Call CarregaRegistro
End If
End Sub
Private Sub cmdProximo_Click()
Call limpaMensagem
If indiceRegistro < wsplanilha_de_cadastro_de_membros_VBA.UsedRange.Rows.Count Then
indiceRegistro = indiceRegistro + 1
End If
If indiceRegistro > 1 Then
Call CarregaRegistro
End If
End Sub
Private Sub cmdUltimo_Click()
Call limpaMensagem
indiceRegistro = wsplanilha_de_cadastro_de_membros_VBA.UsedRange.Rows.Count
If indiceRegistro > 1 Then
Call CarregaRegistro
End If
End Sub
Private Sub txtCodigo_Change()
End Sub
Private Sub UserForm_Initialize()
novo = False
alterar = False
excluir = False
Set wsplanilha_de_cadastro_de_membros_VBA = ThisWorkbook.Worksheets("Membros")
Call HabilitaBotoesAlteracao
Call carregaDados
Call DesabilitaControles
End Sub
Private Sub carregaDados()
indiceRegistro = 2
Call CarregaRegistro
End Sub
Private Sub CarregaRegistro()
'carrega os dados do primeiro registro
With wsplanilha_de_cadastro_de_membros_VBA
If Not IsEmpty(.Cells(indiceRegistro, colNome)) Then
Me.txtNome.Text = .Cells(indiceRegistro, colNome).Value
Me.txtRg.Text = .Cells(indiceRegistro, colRg).Value
Me.txtExped.Text = .Cells(indiceRegistro, colExped).Value
Me.txtUf.Text = .Cells(indiceRegistro, colUf).Value
Me.txtNascimento.Text = .Cells(indiceRegistro, colNascimento).Value
Me.txtCpf.Text = .Cells(indiceRegistro, colCpf).Value
Me.txtPeriodo.Text = .Cells(indiceRegistro, colPeriodo).Value
Me.txtCurso.Text = .Cells(indiceRegistro, colCurso).Value
Me.txtFuncao.Text = .Cells(indiceRegistro, colFuncao).Value
Me.txtEndereco.Text = .Cells(indiceRegistro, colEndereco).Value
Me.txtComplemento.Text = .Cells(indiceRegistro, colComplemento).Value
Me.txtFone1.Text = .Cells(indiceRegistro, colFone1).Value
Me.txtFone2.Text = .Cells(indiceRegistro, colFone2).Value
Me.txtEmail1.Text = .Cells(indiceRegistro, colEmail1).Value
Me.txtEmail2.Text = .Cells(indiceRegistro, colEmail2).Value
End If
End With
End Sub
Private Sub LimpaControles()
Me.txtNome.Text = ""
Me.txtRg.Text = ""
Me.txtExped.Text = ""
Me.txtUf.Text = ""
Me.txtNascimento.Text = ""
Me.txtCpf.Text = ""
Me.txtPeriodo.Text = ""
Me.txtCurso.Text = ""
Me.txtFuncao.Text = ""
Me.txtEndereco.Text = ""
Me.txtComplemento.Text = ""
Me.txtFone1.Text = ""
Me.txtFone2.Text = ""
Me.txtEmail1.Text = ""
Me.txtEmail2.Text = ""
Me.txtCodigo.Text = ""
End Sub
Private Sub HabilitaControles()
Me.txtNome.Locked = False
Me.txtRg.Locked = False
Me.txtExped.Locked = False
Me.txtUf.Locked = False
Me.txtNascimento.Locked = False
Me.txtCpf.Locked = False
Me.txtPeriodo.Locked = False
Me.txtCurso.Locked = False
Me.txtFuncao.Locked = False
Me.txtEndereco.Locked = False
Me.txtComplemento.Locked = False
Me.txtFone1.Locked = False
Me.txtFone2.Locked = False
Me.txtEmail1.Locked = False
Me.txtEmail2.Locked = False
'altera a cor dos controles
Me.txtNome.BackColor = corHabilitaTextBox
Me.txtRg.BackColor = corHabilitaTextBox
Me.txtExped.BackColor = corHabilitaTextBox
Me.txtUf.BackColor = corHabilitaTextBox
Me.txtNascimento.BackColor = corHabilitaTextBox
Me.txtCpf.BackColor = corHabilitaTextBox
Me.txtPeriodo.BackColor = corHabilitaTextBox
Me.txtCurso.BackColor = corHabilitaTextBox
Me.txtFuncao.BackColor = corHabilitaTextBox
Me.txtEndereco.BackColor = corHabilitaTextBox
Me.txtComplemento.BackColor = corHabilitaTextBox
Me.txtFone1.BackColor = corHabilitaTextBox
Me.txtFone2.BackColor = corHabilitaTextBox
Me.txtEmail1.BackColor = corHabilitaTextBox
Me.txtEmail2.BackColor = corHabilitaTextBox
End Sub
Private Sub DesabilitaControles()
Me.txtNome.Locked = True
Me.txtRg.Locked = True
Me.txtExped.Locked = True
Me.txtUf.Locked = True
Me.txtNascimento.Locked = True
Me.txtCpf.Locked = True
Me.txtPeriodo.Locked = True
Me.txtCurso.Locked = True
Me.txtFuncao.Locked = True
Me.txtEndereco.Locked = True
Me.txtComplemento.Locked = True
Me.txtFone1.Locked = True
Me.txtFone2.Locked = True
Me.txtEmail1.Locked = True
Me.txtEmail2.Locked = True
'altera a cor dos controles
Me.txtNome.BackColor = corDesabilitaTextBox
Me.txtRg.BackColor = corDesabilitaTextBox
Me.txtExped.BackColor = corDesabilitaTextBox
Me.txtUf.BackColor = corDesabilitaTextBox
Me.txtNascimento.BackColor = corDesabilitaTextBox
Me.txtCpf.BackColor = corDesabilitaTextBox
Me.txtPeriodo.BackColor = corDesabilitaTextBox
Me.txtCurso.BackColor = corDesabilitaTextBox
Me.txtFuncao.BackColor = corDesabilitaTextBox
Me.txtEndereco.BackColor = corDesabilitaTextBox
Me.txtComplemento.BackColor = corDesabilitaTextBox
Me.txtFone1.BackColor = corDesabilitaTextBox
Me.txtFone2.BackColor = corDesabilitaTextBox
Me.txtEmail1.BackColor = corDesabilitaTextBox
Me.txtEmail2.BackColor = corDesabilitaTextBox
End Sub
Private Sub HabilitaBotoesAlteracao()
'habilita os botões de alteração
cmdAlterar.Enabled = True
cmdExcluir.Enabled = True
cmdNovo.Enabled = True
cmdOk.Enabled = False
cmdCancelar.Enabled = False
End Sub
Private Sub DesabilitaBotoesAlteracao()
'desabilita os botões de alteração
cmdAlterar.Enabled = False
cmdExcluir.Enabled = False
cmdNovo.Enabled = False
cmdOk.Enabled = True
cmdCancelar.Enabled = True
End Sub
Private Sub cmdPesquisar_Click()
frmPesquisarClientes.Show
End Sub
Private Function ValidaCamposFormulario() As Boolean
If Me.txtNome.Value = "" Then
Me.txtNome.SetFocus
MsgBox " 'Nome' é um campo obrigatório. ", vbOKOnly, " Campo Obrigatório "
ValidaCamposFormulario = False
Exit Function
ElseIf Me.txtCpf.Value = "" Then
Me.txtCpf.SetFocus
MsgBox " 'Cpf' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
ValidaCamposFormulario = False
Exit Function
ElseIf Me.txtNascimento.Value = "" Then
Me.txtNascimento.SetFocus
MsgBox "'Nascimento' é um campo obrigatório. ", vbOKOnly, " Campo Obrigatório "
ValidaCamposFormulario = False
Exit Function
ElseIf Me.txtRg.Value = "" Then
Me.txtRg.SetFocus
MsgBox "'Rg' é um campo obrigatório. ", vbOKOnly, " Campo Obrigatório "
ValidaCamposFormulario = False
Exit Function
ElseIf Me.txtEmail1.Value = "" Then
Me.txtEmail1.SetFocus
MsgBox " 'Email' é um campo obrigatório. ", vbOKOnly, " Campo Obrigatório "
ValidaCamposFormulario = False
Exit Function
ElseIf Me.txtFone1.Value = "" Then
Me.txtFone1.SetFocus
MsgBox "'Telefone' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
ValidaCamposFormulario = False
Exit Function
ElseIf Me.txtEmail2.Value = "" Then
Me.txtEmail2.SetFocus
MsgBox "'Email' é um campo obrigatório.", vbOKOnly, " Campo Obrigatório "
ValidaCamposFormulario = False
Exit Function
End If
ValidaCamposFormulario = True
End Function