Textbox que cambie datos de dos hojas dependiendo si encuentra el valor

Tengo una macro que me proporciono dan el cual me cambia el dato(cliente) de una celda de un rango de la hoja CLIENTES dependiendo si lo busca el textbox3 y si es así este cambia el dato buscado por el textbox3(actualización de datos)

El problema es que en la hoja HISTORY también tengo el mismo dato (cliente) ya que como su nombre lo indica se ha guardado el historial de dicho dato (cliente) lo que requiero es que al cambiar el dato de la hoja CLIENTES

También me cambie los datos del la hoja HISTORIAL buscando y cambiando los datos de (cliente) que vaya encontrando en la col A

Tengo el código que según yo estoy adaptando pero obvio se que esta mal

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Por Dante Amor
    Set h2 = Sheets("clientes")
    FOLIO = CONSULTA_CLIENTE.ListBox1.List(ListBox1.ListIndex, 1)
    If FOLIO = "" Then
        MsgBox "Pon algo en el label"
        Exit Sub
    End If
    Set r = h2.Columns("C") 'rango de búsqueda
    'Busca el nuevo cliente
    Set b = r.Find(TextBox3, lookat:=xlWhole)
    If Not b Is Nothing Then
        'si ya existe
        MsgBox "El Cliente ya existe", vbExclamation
        Unload Me
        Exit Sub
    End If
    '
    Set b = r.Find(FOLIO, lookat:=xlWhole)      'busca folio (nombre anterior)
    If Not b Is Nothing Then
        'Datos del cliente
         h2.Cells(b.Row, "C") = TextBox3.Value
        MsgBox "Datos actualizados", , "EXCELENTE"
     TextBox3.BackColor = RGB(255, 255, 255) 'blanco
End If
'
On Error Resume Next
    'Por Dante Amor
    Set h3 = Sheets("history")
    FOLIO1 = CONSULTA_CLIENTE.ListBox1.List(ListBox1.ListIndex, 1)
    If FOLIO1 = "" Then
        MsgBox "Pon algo en el label"
        Exit Sub
    End If
    Set r1 = h3.Columns("A") 'rango de búsqueda
    'Busca el nuevo cliente
    Set b1 = r1.Find(TextBox3, lookat:=xlWhole)
    If Not b1 Is Nothing Then
        'si ya existe
        MsgBox "El Cliente ya existe", vbExclamation
        Unload Me
        Exit Sub
    End If
    '
    Set b1 = r1.Find(FOLIO1, lookat:=xlWhole)      'busca folio (nombre anterior)
    If Not b1 Is Nothing Then
    Celda = b1.Address
        Do
        'Datos del cliente
         h3.Cells(b1.Row, "A") = TextBox3.Value
        MsgBox "Datos actualizados", , "EXCELENTE"
     TextBox3.BackColor = RGB(255, 255, 255) 'blanco
     Set b1 = r.FindNext(b1)
        Loop While Not b1 Is Nothing And b1.Address <> Celda
    Else
        MsgBox "El folio no existe", , ""
        End If
End Sub

1 Respuesta

Respuesta
1

Te dejo la parte de la búsqueda en Historial. Creo que tu fallo estaba en la línea comentada

Además le retiré la búsqueda de FOLIO ya que no hace falta que la repitas.

On Error Resume Next
    'Por Dante Amor  - arreglada x Elsamatilde
    Set h3 = Sheets("history")
    'no hace falta volver a guardar el FOLIO
        'FOLIO1 = CONSULTA_CLIENTE.ListBox1.List(ListBox1.ListIndex, 1)
        'If FOLIO1 = "" Then
            'MsgBox "Pon algo en el label"
            'Exit Sub
        'End If
    Set r1 = h3.Columns("A") 'rango de búsqueda en Historial
    'Busca el nuevo cliente
    Set b1 = r1.Find(TextBox3, lookat:=xlWhole)
    If Not b1 Is Nothing Then
        'si ya existe ------ no lo modificará
        MsgBox "El Cliente ya existe", vbExclamation
        Unload Me
        Exit Sub
    End If
    '
    Set b1 = r1.Find(FOLIO1, lookat:=xlWhole)      'busca folio (nombre anterior)
    If Not b1 Is Nothing Then
        celda = b1.Address
        Do
        'Datos del cliente
         h3.Cells(b1.Row, "A") = TextBox3.Value
         MsgBox "Datos actualizados", , "EXCELENTE"
         TextBox3.BackColor = RGB(255, 255, 255) 'blanco
         Set b1 = r1.FindNext(b1)       'ATENCIÓN. repetir la búsqueda en r1
       Loop While Not b1 Is Nothing And b1.Address <> celda
    Else
        MsgBox "El folio no existe", , ""
    End If
End Sub

Sdos.

Elsa

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas