Si esta repetido, no permitir que se agrege!...

gusmario2011

New Member
Joined
Aug 4, 2011
Messages
27
:biggrin:Hola, estoy haciendo una base que tiene un userform1 y tiene 4 textbox1,2,3,4...
textbox1 = nombre = columna A
textbox2 = direccion = columna B
textbox3 = telefono = columna C
textbox4 = cuota = columna D
y tiene un boton1 "guardar nuevo", boton2 "buscar", boton3 "editar", boton4 "eliminar", el codigo funciona bien, si guarda correctamente la informacion, pero quiero agregarle una linea al codigo que me mande decir "el dato ya existe en la base" y no me permita agregarlo. Incluyo el resto del codigo para los demas botones, por si alguien esta interesado, por mi no hay inconveniente si lo copian para realizar algun proyecto, Por favor alguien me ayudaria a terminarlo!...

Gracias por anticipado...:rolleyes:

----------------------------------------------------------------------------------

Option Explicit
Dim Rango As Range

----------------------------------------------------------------------------------

Private Sub CommandButton1_Click()
Dim strfila$, ctr As Control
'verificamos que todos los campos esten llenos.
If TextBox1 = "" Or TextBox2 = "" Or TextBox3 = "" Or TextBox4 = "" Then
'mandamos el mensaje pidiendonos llenemos todos los campos.
MsgBox "No dejes ningun campo en blanco", vbOKOnly + vbInformation, "AVISO"
'movemos el focus al textbox1 = nombre.
TextBox1.SetFocus
Exit Sub
End If

Set Rango = Range("A:A").Find(What:=TextBox1, _
LookAt:=xlWhole, LookIn:=xlValues)


'AQUI DEBE DE IR EL CODIGO QUE BUSCO.<<<<
'AQUI DEBE DE IR EL CODIGO QUE BUSCO.<<<<

'mandame el mensaje si el dato ya existe.
MsgBox "El dato ya existe", vbOKOnly + vbInformation, "AVISO"
'movemos el focus al textbox1 = nombre.
TextBox1.SetFocus
Exit Sub
End If
'grabamos los datos en los rangos enlistados abajo.
strfila$ = [A65536].End(xlUp).Offset(1, 0).Row
Range("A" & strfila$) = TextBox1 'nombre
Range("B" & strfila$) = TextBox2 'direccion
Range("C" & strfila$) = TextBox3 'telefono
Range("D" & strfila$) = Val(TextBox4) 'cuota
Range("D" & strfila$).NumberFormat = "$ #,##0.00"
For Each ctr In Me.Controls
If TypeOf ctr Is MSForms.TextBox Then
ctr = ""
End If
Next ctr
'damos el rango.
Range("A" & strfila$ & ":D" & strfila$).HorizontalAlignment = xlCenter
'movemos el focus al textbox1 = nombre.
TextBox1.SetFocus
End Sub

---------------------------------------------------------------------------------------

Private Sub CommandButton2_Click()
If TextBox1 = "" Then
MsgBox "Coloca algun dato para buscar", vbOKOnly + vbInformation, "AVISO"
TextBox1.SetFocus
Exit Sub
End If
Set Rango = Range("A:A").Find(What:=TextBox1, _
LookAt:=xlWhole, LookIn:=xlValues)
If Rango Is Nothing Then
MsgBox "El dato no fue encontrado", vbOKOnly + vbInformation, "AVISO"
TextBox1 = "": TextBox1.SetFocus
Exit Sub
Else
TextBox2 = Range("B" & Rango.Row)
TextBox3 = Range("C" & Rango.Row)
TextBox4 = Range("D" & Rango.Row)
End If
End Sub

---------------------------------------------------------------------------------------

Private Sub CommandButton4_Click()
Dim ctr As Control
If Rango Is Nothing Then
MsgBox "Aun no buscas ningun dato", vbOKOnly + vbInformation, "AVISO"
TextBox1.SetFocus
Exit Sub
End If
If TextBox2 = "" Or TextBox3 = "" Or TextBox4 = "" Then
MsgBox "No dejes ningun campo en blanco", vbOKOnly + vbInformation, "AVISO"
Exit Sub
End If
Range("B" & Rango.Row) = TextBox2
Range("C" & Rango.Row) = TextBox3
Range("D" & Rango.Row) = Val(TextBox4)
Range("D" & Rango.Row).NumberFormat = "$ #,##0.00"
For Each ctr In Me.Controls
If TypeOf ctr Is MSForms.TextBox Then
ctr = ""
End If
Next ctr
TextBox1.SetFocus
Set ctr = Nothing
Set Rango = Nothing
End Sub

----------------------------------------------------------------------------------------

Private Sub CommandButton3_Click()
Dim respuesta As Integer, ctr As Control
If Rango Is Nothing Then
MsgBox "Aun no buscas ningun dato", vbOKOnly + vbInformation, "AVISO"
TextBox1.SetFocus
Exit Sub
End If
respuesta = MsgBox("¿Estas seguro de eliminar el registro elegido?", vbCritical + vbOKCancel, "AVISO")
If respuesta = vbOK Then
Cells(Rango.Row, Rango.Column).EntireRow.Delete
For Each ctr In Me.Controls
If TypeOf ctr Is MSForms.TextBox Then
ctr = ""
End If
Next ctr
TextBox1.SetFocus
Exit Sub
End If
MsgBox "Operacion cancelada", vbOKOnly + vbInformation, "AVISO"
For Each ctr In Me.Controls
If TypeOf ctr Is MSForms.TextBox Then
ctr = ""
End If
Next ctr
TextBox1.SetFocus
End Sub

--------------------------------------------------------------------------------------

Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
KeyAscii = 0
MsgBox "Solo puede ingresar numeros", vbOKOnly + vbInformation, "AVISO"
End If
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top