Buscar antes de agregar datos en Celdas

Hola, lo que tengo es lo siguiente:
Private Sub CommandButton1_Click()
Dim ultimafila As Long
Dim ultimacolumna As Long
Sheets("CLIENTES").Select
ultimafila = Range("A65536").End(xlUp).Row
ultimafila = ultimafila + 1
Cells(ultimafila, 1).Select
ActiveCell.Value = TextBox1
Cells(ultimafila, 2).Select
ActiveCell.Value = TextBox2
Cells(ultimafila, 3).Select
ActiveCell.Value = TextBox3
Cells(ultimafila, 4).Select
ActiveCell.Value = TextBox4
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
Sheets("factura").Select
Unload Me
End Sub
Quisiera que antes de realizar este procedimiento buscara si existen los datos del textbox1, si existen me salga un aviso "LOS DATOS EXISTEN"
Si no existen ahí si se deberían agregar .
Saludos espero respuesta :)

1 Respuesta

Respuesta
1
Tu código se puede optimizar, no es necesario "seleccionar" la celda para darle un valor. Checa esto:
Private Sub CommandButton1_Click() 
Dim ultimafila As Long 
Dim ultimacolumna As Long 
'Busca lo del TExtBox1
Set Existe = Range("A:A").Find(TextBox1.Value,LookAt:=xlWhole)
'Si lo encuentra avisa y detiene la macro
If Not Existe Is Nothing then
      Msgbox "Los Datos Existen" : Exit Sub
End If
Sheets("CLIENTES").Select 
ultimafila = Range("A65536").End(xlUp).Row + 1
Cells(ultimafila, 1).Value = TextBox1 
Cells(ultimafila, 2).Value = TextBox2 
Cells(ultimafila, 3).Value = TextBox3 
Cells(ultimafila, 4).Value = TextBox4 
TextBox1 = Empty 
TextBox2 = Empty 
TextBox3 = Empty 
TextBox4 = Empty 
Sheets("factura").Select 
Unload Me 
End Sub
Muchas gracias, solo un detalle :
Tuve que especificar que buscara lo del textbot1 en la hoja "clientes" ya que la buscaba en la hoja actual
Saludos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas