Coincidencia exacta en un texbox

Soy principiante, y he confeccionado un formulario que lee códigos de barras, el problema es que hay veces que cuando no lo encuentra me devuelve el más próximo, habría forma de que me si no es exactamente el código me diera un mensaje, el código que estoy usando es

Private Sub TextBox1_AfterUpdate()
Set h = Sheets("Hoja2")
Set b = h.Columns("b").Find(TextBox1)
Set c = h.Columns("b").Find(TextBox1)
Set d = h.Columns("b").Find(TextBox1)
If Not b Is Nothing Then
TextBox2 = h.Cells(b.Row, "C")
TextBox3 = h.Cells(c.Row, "d")
TextBox4 = h.Cells(d.Row, "E")
Else
MsgBox "DATO NO ENCONTRADO"
TextBox1.SetFocus
End If
End Sub

Respuesta
1

Prueba con esta macro a ver si te va bien.

Private Sub TextBox1_AfterUpdate()
 Set h = Sheets("Hoja2")
 If TextBox1 = "" Then Exit Sub
    x = TextBox1.Value
 Set b = h.Columns("B").Find(x, lookat:=xlWhole)
   If b Is Nothing Then
      MsgBox "DATO NO ENCONTRADO", vbInformation
      TextBox1 = ""
      TextBox1.SetFocus
      Else
      TextBox2 = h.Cells(b.Row, "C")
      TextBox3 = h.Cells(b.Row, "D")
      TextBox4 = h.Cells(b.Row, "E")
   End If
End Sub

Salu2 Carlos Arrocha

1 respuesta más de otro experto

Respuesta
2

¿Cuándo capturas el código de barras en automático se sale del textbox1?

Si es así, entonces es correcto que utilices el evento AfterUpdate. Si no tienes configurado tu lector de código de barras para que envíe un enter al final de la lectura, tal vez debes utilizar el evento Change.

Recomendación: Es recomendable declarar la variables apropiadamente. Te ayudará a evitar errores en la programación y utilizar la ayuda de VBA mientras capturas el código (Intellisense).


Aquí otra opción a considerar:

Private Sub TextBox1_AfterUpdate()
  Dim h As Worksheet
  Dim b As Range
  '
  Set h = Sheets("Hoja2")
  Set b = h.Range("B:B").Find(TextBox1.Value, , xlValues, xlWhole, , , False)
  If Not b Is Nothing Then
    TextBox2.Value = h.Range("C" & b.Row)
    TextBox3.Value = h.Range("D" & b.Row)
    TextBox4.Value = h.Range("E" & b.Row)
  Else
    MsgBox "DATO NO ENCONTRADO"
    TextBox1.SetFocus
  End If
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas