No me funciona la parte del código donde tiene que comprobar si ya existe el registro.

Tengo problema con la parte del código donde tiene que comprobar si ya existe el registro. El código es mayor, pero no lo puedo poner completo porque me da problemas a la hora de publicarlo.

Private Sub btn_Registrar_Click()
Dim uf As Long, nDoc As String
  Set h = Sheets("Clientes")
   uf = h.Range("A" & Rows.Count).End(xlUp).Row + 1
   Hoja6.Visible = xlSheetVisible
   Hoja6.Select
  If h.Cells(uf, 1) = Txt_NIF Then
   Txt_NIF.BackColor = &H8080FF
   MsgBox ("El cliente introducido ya está registrado")
   Txt_NombreApellido.SetFocus
  Exit Sub
  End If

1 respuesta

Respuesta
1

¿La hoja6 y la hoja "Clientes" es la misma?

Si es así, para buscar un dato no es necesario mostrar la hoja, puede ser así:

Private Sub btn_Registrar_Click()
  Dim f As Range, h As Worksheet
  '
  If Txt_NIF = "" Then
    MsgBox "Captura el NIF"
    Exit Sub
  End If
  '
  Set h = Sheets("Clientes")
  Set f = h.Range("A:A").Find(Txt_NIF, , xlValues, xlWhole)
  If Not f Is Nothing Then
    Txt_NIF.BackColor = &H8080FF
    MsgBox ("El cliente introducido ya está registrado")
    Txt_NIF.SetFocus
    Exit Sub
  End If
  Txt_NIF.BackColor = &H80000005
End Sub

Gracias Dante Amor pero, para probar, hago un registro repitiendo un valor de la columna "A" y me realiza bien el registro. Voy a intentar poner el código completo. Ayer me dio problemas, por eso solo puse la mitad.

Private Sub btn_Registrar_Click()
Dim uf As Long, f As Range, h As Worksheet, nDoc As String
  Set h = Sheets("Clientes")
   uf = h.Range("A" & Rows.Count).End(xlUp).Row + 1
   'Hoja6.Visible = xlSheetVisible
   Hoja6.Select
  Set f = h.Range("A:A").Find(Txt_NIF, , xlValues, xlWhole)
   If Not f Is Nothing Then
  'If h.Cells(uf, 1) = Txt_NIF Then
   Txt_NIF.BackColor = &H8080FF
   MsgBox ("El cliente introducido ya está registrado")
   Txt_NombreApellido.SetFocus
  Exit Sub
  End If
  nDoc = Application.InputBox("Ingrese el Tipo de Documento")
   If MsgBox("Son correctos los datos?", vbOKCancel) = vbOK Then
    'Txt_NIF.BackColor = &HE0E0E0
    h.Cells(uf, "A") = nDoc & "-" & Me.Txt_NIF.Text
    h.Cells(uf, "B") = Txt_NombreApellido.Text
    h.Cells(uf, "C") = Txt_Direccion.Text
    h.Cells(uf, "D") = Txt_Provincia.Text
    h.Cells(uf, "E") = Txt_CodigoPos.Text
    h.Cells(uf, "F") = Txt_Telefono.Text
    Txt_NIF = ""
    Txt_NombreApellido = ""
    Txt_CodigoPos = ""
    Txt_Direccion = ""
    Txt_Provincia = ""
    Txt_Telefono = ""
    Txt_NombreApellido.SetFocus
   Else
   Exit Sub
   End If
    'Hoja6.Visible = xlSheetVeryHidden
End Sub

¿Tienes un problema? ¿Cuál es el problema?

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas