Comportamiento irregular de una macro dentro de un formulario

Mi consulta trata de encontrar una macro que actualice los nombres de los clientes en la hoja Pedidos, cuando se modifican en la hoja Clientes.

En la pregunta realizada anteriormente “Actualizar datos de una hoja a otra”, la rutina propuesta funciona perfectamente cuando se ejecuta sola, pero si la ejecuto después de introducir los datos desde un formulario, en todas las celdas pone: "ID no encontrado", cuando realmente sí está el ID. Sólo cuando se reinicia la PC o cuando se reinicia el libro sí funciona a veces, aunque no siempre ¿A qué se puede deber?. Les dejo las rutinas del formulario y la macro que copia los datos

Muchas gracias de antemano

Sub CopiarId()

'Por.Dante Amor

Set h1 = Sheets("Pedidos")

   Set h2 = Sheets("Clientes")

   For i = 2 To h1.Range("AB" & Rows.Count).End(xlUp).Row

       If Not IsError(h1.Cells(i, "AB")) Then

           Set B = h2.Columns("A").Find(h1.Cells(i, "AB"))

           If Not B Is Nothing Then

               h1.Cells(i, "B") = h2.Cells(B.Row, "B")

            Else

               h1.Cells(i, "B") = "Id no encontrado"

           End If

       Else

           h1.Cells(i, "B") = "Celda con error"

       End If

   Next

End Sub

Private Sub CommandButton2_Click()

Unload Me

End Sub

Private Sub CommandButton1_Click(): On Error Resume Next

If Not ListBox1.ListIndex = -1 Then

   Range("B" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox1.Value

   Range("C" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox2.Value

   Range("D" & ListBox1.List(ListBox1.ListIndex, 18)) = ComboBox1.Value

   Range("E" & ListBox1.List(ListBox1.ListIndex, 18)) = ComboBox2.Value

   Range("F" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox3.Value

   Range("G" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox7.Value

   Range("H" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox4.Value

   Range("I" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox5.Value

   Range("J" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox6.Value

   Range("K" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox8.Value

   Range("L" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox9.Value

   Range("M" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox13.Value

   Range("N" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox10.Value

   Range("O" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox11.Value

   Range("P" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox12.Value

   Range("Q" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox14.Value

   Range("T" & ListBox1.List(ListBox1.ListIndex, 18)) = TextBox15.Value

Call CopiarId

End If

End Sub

Private Sub CommandButton5_Click()

Unload Me

Call introducir_DatosCl

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

If CloseMode = vbFormControlMenu Then

       Cancel = True

   End If

End Sub

Private Sub ListBox1_Click()

Label1.Caption = ""

TextBox1.Value = ""

TextBox2.Value = ""

TextBox3.Value = ""

TextBox4.Value = ""

TextBox5.Value = ""

TextBox6.Value = ""

TextBox7.Value = ""

ComboBox1.Value = ""

ComboBox2.Value = ""

TextBox8.Value = ""

TextBox9.Value = ""

TextBox10.Value = ""

TextBox11.Value = ""

TextBox12.Value = ""

TextBox13.Value = ""

TextBox14.Value = ""

TextBox15.Value = ""

If Not ListBox1.ListIndex = -1 Then

Label1.Caption = "   " & ListBox1.List(ListBox1.ListIndex, 1)

   TextBox1.Text = ListBox1.List(ListBox1.ListIndex, 1)

   TextBox2.Text = ListBox1.List(ListBox1.ListIndex, 2)

   ComboBox1.Text = ListBox1.List(ListBox1.ListIndex, 3)

   ComboBox2.Text = ListBox1.List(ListBox1.ListIndex, 4)

   TextBox3.Text = ListBox1.List(ListBox1.ListIndex, 5)

   TextBox4.Text = ListBox1.List(ListBox1.ListIndex, 7)

   TextBox5.Text = ListBox1.List(ListBox1.ListIndex, 8)

   TextBox6.Text = ListBox1.List(ListBox1.ListIndex, 9)

   TextBox7.Text = ListBox1.List(ListBox1.ListIndex, 6)

   TextBox8.Text = ListBox1.List(ListBox1.ListIndex, 10)

   TextBox9.Text = ListBox1.List(ListBox1.ListIndex, 11)

   TextBox13.Text = ListBox1.List(ListBox1.ListIndex, 12)

   TextBox10.Text = ListBox1.List(ListBox1.ListIndex, 13)

   TextBox11.Text = ListBox1.List(ListBox1.ListIndex, 14)

   TextBox12.Text = ListBox1.List(ListBox1.ListIndex, 15)

   TextBox14.Text = ListBox1.List(ListBox1.ListIndex, 16)

   TextBox15.Text = ListBox1.List(ListBox1.ListIndex, 19)

   Rows(ListBox1.List(ListBox1.ListIndex, 18)).Select

Set rango = Worksheets("inicio").Range("Tabla4[Paises]")

For Each Celda In rango

   ComboBox1.AddItem Celda.Value

Next Celda

Set rango = Worksheets("inicio").Range("Tabla5[Tipo de cliente]")

For Each Celda In rango

   ComboBox2.AddItem Celda.Value

Next Celda

End If

End Sub

Private Sub UserForm_Activate()

ListBox1.List = Range("Tabla1").Value

For x = 0 To ListBox1.ListCount - 1: ListBox1.List(x, 18) = x + 2: Next

ListBox1.ListIndex = 0

ListBox1.SetFocus

End Sub

2 respuestas

Respuesta
1

Voy a esperar a ver si Dante arregla su macro cuando llegue, sino con mucho gusto esta tarde me ocupo de tu consulta.

Sdos

Elsa

Respuesta
1

Tengo las siguientes observaciones: De la macro original que te envié a la macro que estás poniendo cambiaste la columna de Idcliente. En tu ejemplo original el IdCliente era un dato alfanumérico, pero si es numérico y estás pasando el dato de un listbox a una celda, el dato se almacena como texto, luego entonces, si buscas ese texto en una columna de números no lo va a encontrar.

Podrías enviarme tu archivo con la macro para revisar los datos y probarla desde que ejecutas el formulario.

Mi correo [email protected]

En el asunto del correo escribe tu nombre de usuario “angeles88” y el título de esta pregunta.

Estimado Dante

Ya te he enviado el correo con el archivo

Gracias

Te envié unos comentarios.

Ninguno de los datos que tienes en la hoja "Pedidos" coincide con los Id de la hoja "Clientes", por eso a todos les pone "ID no encontrado", hice varias pruebas utilizando los ID de "clientes" en la hoja "pedidos" y a todos los encuentra. Todas las pruebas las hice sin salir de excel y en todos los casos pone el nombre correctamente.

Te anexo un vídeo con las pruebas que realicé.

https://www.youtube.com/watch?v=jhAqQxLlObM&feature=youtu.be 

¡Gracias! 

He rehecho el proyecto desde el inicio y funciona perfectamente.

No sé qué extraño suceso pudo colarse con el libro anterior.

Seguiré haciendo pruebas.

Como siempre quedo muy agradecido por vuestra ayuda

También se me hacer raro, hice pruebas con el archivo que me enviaste y no tuve problemas, si tienes alguna otra duda puedes consultarme.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas