MACRO para buscar datos en una hoja y colocarlo en un label

Tengo este código el cual me funciona bien (representación de buscarv )

Si funciona bien cuando coloco LETRAS y números para buscarv

Pero cuando coloco solo números este ya no me funciona, hay alguna otra forma de realizar la búsqueda

If Not TextBox1.Value = Empty And Not Label3.Caption = Empty Then
MsgBox "ya haz iniciado el verificador", , "AVISO"
Exit Sub
End If
If TextBox1.Value = Empty Then
MsgBox "No hay clave para realizar busqueda", vbExclamation, "AVISO"
Exit Sub
End If
On Error GoTo h
Set b = Sheets(Hoja2.Name)
valor = b.Application.WorksheetFunction.VLookup(UCase(TextBox1.Value), Sheets("COLABORADORES").Range("B:C"), 2, 0)
valor2 = b.Application.WorksheetFunction.VLookup(UCase(TextBox1.Value), Sheets("COLABORADORES").Range("B:D"), 3, 0)
Label3.Caption = valor
Label4.Caption = valor2
VERIFICADOR_SALIDA_COMER
Exit Sub
h:
MsgBox "Error, El dato que ingresas es incorrecto", vbCritical, "ErrR1"
TextBox1.Value = Empty
TextBox1.SetFocus
End Sub

1 Respuesta

Respuesta
1

Prueba con lo siguiente:

Private Sub CommandButton1_Click()
'Por Dante Amor
    If Not TextBox1.Value = Empty And Not Label3.Caption = Empty Then
        MsgBox "ya haz iniciado el verificador", , "AVISO"
        Exit Sub
    End If
    '
    If TextBox1.Value = Empty Then
        MsgBox "No hay clave para realizar busqueda", vbExclamation, "AVISO"
        Exit Sub
    End If
    '
    Set h = Sheets(Hoja2.Name)
    Set b = h.Columns("B").Find(TextBox1.Value, lookat:=xlWhole)
    If Not b Is Nothing Then
        Label3.Caption = h.Cells(b.Row, "C")
        Label4.Caption = h.Cells(b.Row, "D")
        VERIFICADOR_SALIDA_COMER
    Else
        MsgBox "Error, El dato que ingresas es incorrecto", vbCritical, "ErrR1"
        TextBox1.Value = Empty
        TextBox1.SetFocus
    End If
End Sub

Cambia el vlookup por el método Find, de esa forma puedes controlar el error y eliminar la instrucción On Error, ya que cualquier error siempre te enviará al GoTo y no sabrás qué error tienes.



Algunos tips

La nemotecnia también es importante para entender el significado del programa,

set h para hoja,

set b para buscar


Puedes utilizar la nemotecnia que tu quieras, pero siempre y cuando haga sentido

Set b = Sheets(Hoja2.Name)

La letra b no hace referencia ni a hoja ni a sheet.

On Error GoTo h

La letra h no hace referencia a un error.


Sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas