Usuário com melhor resposta
Cadastro utilizando o UserForm

Pergunta
-
Olá pessoal,
É o seguinte:
1- Criei uma tela do userform que realiza cadastro de veículos de transportes com as informações: placa, cubagem, peso suportado, renavan, e etc...
2- A minha duvida é em como faço para que o userform não cadastre esses veículos em duplicidade.
Ex.: Ao digitar a placa e em seguida clicar em cadastrar, ele verifique se já tem algum veículo com esta placa, no caso de sim então não concluir o cadastro.
Agradeço a compreensão de todos.
Respostas
-
Considere que o TextBox1 é a placa do carro. Se não for, troque no código abaixo:
Private Sub CommandButton1_Click() If EleOf(TextBox1.Value, Plan2.Columns("A")) > 0 Then MsgBox "Essa placa já existe no banco de dados!" Exit Sub End If If TextBox1.Value = "" Or TextBox5.Value = "" Or TextBox6.Value = "" Or TextBox7.Value = "" Or TextBox8.Value = "" Or ComboBox5.Value = "" Or ComboBox1.Value = "" Or ComboBox2.Value = "" Then MsgBox "Os campos em NEGRITO são OBRIGATORIOS, favor informá-los!", vbCritical, "Ateção" Else Plan2.Rows("2:2").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Plan2.Range("a2").Value = TextBox1.Value Plan2.Range("b2").Value = TextBox2.Value Plan2.Range("c2").Value = TextBox3.Value Plan2.Range("d2").Value = TextBox4.Value Plan2.Range("e2").Value = TextBox5.Value Plan2.Range("f2").Value = TextBox6.Value Plan2.Range("G2").Value = TextBox7.Value Plan2.Range("H2").Value = TextBox8.Value Plan2.Range("I2").Value = ComboBox5.Value Plan2.Range("j2").Value = ComboBox1.Value Plan2.Range("k2").Value = ComboBox2.Value Plan2.Range("l2").Value = TextBox12.Value Plan2.Range("m2").Value = TextBox13.Value Plan2.Range("n2").Value = TextBox14.Value Plan2.Range("p2").Value = TextBox16.Value Plan2.Range("q2").Value = TextBox17.Value Plan2.Range("r2").Value = CheckBox1.Value Plan2.Range("s2").Value = CheckBox2.Value Plan2.Range("t2").Value = Label22.Object Plan2.Range("o2").Select ActiveCell.FormulaR1C1 = "=RC[-3]*RC[-2]*RC[-1]" Range("S3").Select TextBox1 = Empty TextBox2 = Empty TextBox3 = Empty TextBox4 = Empty TextBox5 = Empty TextBox6 = Empty TextBox7 = Empty TextBox8 = Empty ComboBox5 = Empty ComboBox1 = Empty ComboBox2 = Empty TextBox12 = Empty TextBox13 = Empty TextBox14 = Empty TextBox15 = Empty TextBox16 = Empty TextBox17 = Empty CheckBox1 = Empty CheckBox2 = Empty MsgBox "Veículo cadastrado com sucesso!", vbInformation, "Parabéns!" ActiveWorkbook.Save End If End Sub Function EleOf(va As Variant, rng As Range) As Long 'Retorna o valor da linha ou coluna de um elemento num vetor. Caso não seja encontrado, é retornado 0. On Error Resume Next EleOf = WorksheetFunction.Match(va, rng, 0) End Function
Felipe Costa Gualberto - http://www.ambienteoffice.com.br
- Marcado como Resposta alexhsantos quarta-feira, 16 de maio de 2012 00:32
Todas as Respostas
-
Use algo como mostrado abaixo:
Sub GravarDados() If EleOf(TextBox1.Value, Sheets("BD").Columns("A")) > 0 Then 'O registro já existe no banco de dados Else 'O registro não existe no banco de dados End If End Sub Function EleOf(va As Variant, rng As Range) As Long 'Retorna o valor da linha ou coluna de um elemento num vetor. Caso não seja encontrado, é retornado 0. On Error Resume Next EleOf = WorksheetFunction.Match(va, rng, 0) End Function
Felipe Costa Gualberto - http://www.ambienteoffice.com.br
-
Felipe,
Não consegui adaptar o teu código para o meu caso, o meu cadastro é feito atravéz do código abaixo, você poderia me ajudar nesta adaptação?
Abraços!
Private Sub CommandButton1_Click()
If TextBox1.Value = "" Or TextBox5.Value = "" Or TextBox6.Value = "" Or TextBox7.Value = "" Or TextBox8.Value = "" Or ComboBox5.Value = "" Or ComboBox1.Value = "" Or ComboBox2.Value = "" Then
MsgBox "Os campos em NEGRITO são OBRIGATORIOS, favor informá-los!", vbCritical, "Ateção"
Else
Plan2.Rows("2:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Plan2.Range("a2").Value = TextBox1.Value
Plan2.Range("b2").Value = TextBox2.Value
Plan2.Range("c2").Value = TextBox3.Value
Plan2.Range("d2").Value = TextBox4.Value
Plan2.Range("e2").Value = TextBox5.Value
Plan2.Range("f2").Value = TextBox6.Value
Plan2.Range("G2").Value = TextBox7.Value
Plan2.Range("H2").Value = TextBox8.Value
Plan2.Range("I2").Value = ComboBox5.Value
Plan2.Range("j2").Value = ComboBox1.Value
Plan2.Range("k2").Value = ComboBox2.Value
Plan2.Range("l2").Value = TextBox12.Value
Plan2.Range("m2").Value = TextBox13.Value
Plan2.Range("n2").Value = TextBox14.Value
Plan2.Range("p2").Value = TextBox16.Value
Plan2.Range("q2").Value = TextBox17.Value
Plan2.Range("r2").Value = CheckBox1.Value
Plan2.Range("s2").Value = CheckBox2.Value
Plan2.Range("t2").Value = Label22.Object
Plan2.Range("o2").Select
ActiveCell.FormulaR1C1 = "=RC[-3]*RC[-2]*RC[-1]"
Range("S3").Select
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
TextBox5 = Empty
TextBox6 = Empty
TextBox7 = Empty
TextBox8 = Empty
ComboBox5 = Empty
ComboBox1 = Empty
ComboBox2 = Empty
TextBox12 = Empty
TextBox13 = Empty
TextBox14 = Empty
TextBox15 = Empty
TextBox16 = Empty
TextBox17 = Empty
CheckBox1 = Empty
CheckBox2 = Empty
MsgBox "Veículo cadastrado com sucesso!", vbInformation, "Parabéns!"
ActiveWorkbook.Save
End If
End Sub -
Olá Alexhsantos
vou postar uma função que faz busca por algum valor em um determinado intervalo.
O que você deve fazer é comparar se o retorno dessa função é igual a 0 e realizar o cadastro em caso de vedadeiro.
Insira essa função em algum módulo da sua planilha
'Função para busca em um determinado intervalo 'retornando a posição na linha se tipo = 1 ou coluna se tipo = 2 Function buscar(intervalo As String, busca As String, tipo As Integer) As Integer buscar = 0 For Each c In Range(intervalo) If busca = c Then If tipo = 1 Then buscar = c.Row Else buscar = c.Column End If Exit For End If Next End Function
Ex.
if buscar("c1:c6000", txtplaca.text, 1) = 0 then
'executa sua rotina de cadastro
else
msgbox("Placa já cadastrada")
end if
Neste exemplo estou supondo que as placas estão cadastradas na coluna C de sua planilha.
Abrç
-
Considere que o TextBox1 é a placa do carro. Se não for, troque no código abaixo:
Private Sub CommandButton1_Click() If EleOf(TextBox1.Value, Plan2.Columns("A")) > 0 Then MsgBox "Essa placa já existe no banco de dados!" Exit Sub End If If TextBox1.Value = "" Or TextBox5.Value = "" Or TextBox6.Value = "" Or TextBox7.Value = "" Or TextBox8.Value = "" Or ComboBox5.Value = "" Or ComboBox1.Value = "" Or ComboBox2.Value = "" Then MsgBox "Os campos em NEGRITO são OBRIGATORIOS, favor informá-los!", vbCritical, "Ateção" Else Plan2.Rows("2:2").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Plan2.Range("a2").Value = TextBox1.Value Plan2.Range("b2").Value = TextBox2.Value Plan2.Range("c2").Value = TextBox3.Value Plan2.Range("d2").Value = TextBox4.Value Plan2.Range("e2").Value = TextBox5.Value Plan2.Range("f2").Value = TextBox6.Value Plan2.Range("G2").Value = TextBox7.Value Plan2.Range("H2").Value = TextBox8.Value Plan2.Range("I2").Value = ComboBox5.Value Plan2.Range("j2").Value = ComboBox1.Value Plan2.Range("k2").Value = ComboBox2.Value Plan2.Range("l2").Value = TextBox12.Value Plan2.Range("m2").Value = TextBox13.Value Plan2.Range("n2").Value = TextBox14.Value Plan2.Range("p2").Value = TextBox16.Value Plan2.Range("q2").Value = TextBox17.Value Plan2.Range("r2").Value = CheckBox1.Value Plan2.Range("s2").Value = CheckBox2.Value Plan2.Range("t2").Value = Label22.Object Plan2.Range("o2").Select ActiveCell.FormulaR1C1 = "=RC[-3]*RC[-2]*RC[-1]" Range("S3").Select TextBox1 = Empty TextBox2 = Empty TextBox3 = Empty TextBox4 = Empty TextBox5 = Empty TextBox6 = Empty TextBox7 = Empty TextBox8 = Empty ComboBox5 = Empty ComboBox1 = Empty ComboBox2 = Empty TextBox12 = Empty TextBox13 = Empty TextBox14 = Empty TextBox15 = Empty TextBox16 = Empty TextBox17 = Empty CheckBox1 = Empty CheckBox2 = Empty MsgBox "Veículo cadastrado com sucesso!", vbInformation, "Parabéns!" ActiveWorkbook.Save End If End Sub Function EleOf(va As Variant, rng As Range) As Long 'Retorna o valor da linha ou coluna de um elemento num vetor. Caso não seja encontrado, é retornado 0. On Error Resume Next EleOf = WorksheetFunction.Match(va, rng, 0) End Function
Felipe Costa Gualberto - http://www.ambienteoffice.com.br
- Marcado como Resposta alexhsantos quarta-feira, 16 de maio de 2012 00:32
-