¿Por qué visual basic solo me busca datos en una hoja de excel y yo quiero que lo haga en 2 o 3?

hola una consulta fijaque yo me hice un formulario para buscar
datos pero los datos los tengo en 2 hojas diferentes y solo me busca
sobre la hoja que acciono el buscdor VB como hago paraq me busque datos
en las dos hojas sin q importe de donde abra el VB las hojas las tengo
con el nobre de hoja1 hoja 2
este es el codigo q use
no se q es lo que me hizo flata ahi colocar
Private Sub CommandButton1_Click()
Dim codigo_master, idbusca As String
Dim fila As Integer
fila = 5
codigo_master = TextBox1
Do While idbusca <> codigo_master
fila = fila + 1
idbusca = Range("B" & fila).Value
If idbusca = Empty Then
MsgBox "CODIGO NO ENCONTRADO"
Exit Do
End If
Loop
Dim codigo As String
codigo = Range("B"
& fila).Value
TextBox2 = Range("C" & fila).Value
TextBox3 = Range("E" & fila).Value
TextBox4 = Range("D" & fila).Value
TextBox5 = Range("H" & fila).Value
TextBox6 = Range("F" & fila).Value
TextBox7 = Range("G" & fila).Value
TextBox8 = Range("K" & fila).Value
TextBox9 = Range("I" & fila).Value
TextBox10 = Range("J" & fila).Value
TextBox11 = Range("L" & fila).Value
TextBox12 = Range("N" & fila).Value
TextBox13 = Range("O" & fila).Value
TextBox14 = Range("M" & fila).Value
TextBox15 = Range("P" & fila).Value
TextBox1.SetFocus
End Sub

1 respuesta

Respuesta
1

Así quedaría el código, primero busca en la hoja1, si no lo encuentra busca en la hoja2 y si no lo encuentra envía el mensaje de error. Si lo encuentra entonces llena los textbox con la información de la hoja en dónde encontró el código.

Private Sub CommandButton1_Click()
'Mod.Por.DAM
'Busca en la hoja1
Set b = Sheets("Hoja1").Range("B:B").Find(TextBox1, lookat:=xlWhole)
If Not b Is Nothing Then
    Set h = Sheets("Hoja1")
Else
    'busca en la hoja2
    Set b = Sheets("Hoja2").Range("B:B").Find(TextBox1, lookat:=xlWhole)
    If Not b Is Nothing Then
        Set h = Sheets("Hoja2")
    Else
        MsgBox "CODIGO NO ENCONTRADO"
        Exit Sub
    End If
End If
TextBox2 = h.Range("C" & b.Row).Value
TextBox3 = h.Range("E" & b.Row).Value
TextBox4 = h.Range("D" & b.Row).Value
TextBox5 = h.Range("H" & b.Row).Value
TextBox6 = h.Range("F" & b.Row).Value
TextBox7 = h.Range("G" & b.Row).Value
TextBox8 = h.Range("K" & b.Row).Value
TextBox9 = h.Range("I" & b.Row).Value
TextBox10 = h.Range("J" & b.Row).Value
TextBox11 = h.Range("L" & b.Row).Value
TextBox12 = h.Range("N" & b.Row).Value
TextBox13 = h.Range("O" & b.Row).Value
TextBox14 = h.Range("M" & b.Row).Value
TextBox15 = h.Range("P" & b.Row).Value
TextBox1.SetFocus
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas