Código:
Dim buscarEn As String
Dim nombreHoja As String
Dim tipoBusqueda As String
Dim valorBuscado As String
Dim hoja As Worksheet
Dim celda As Range
Dim encontrado As Boolean
buscarEn = InputBox("¿Quieres buscar en todo el libro o en una hoja específica? Escribe: libro / hoja", "Buscar en...")
If buscarEn = "" Then Exit Sub
If LCase(buscarEn) = "hoja" Then
nombreHoja = InputBox("¿En qué hoja quieres buscar? Escribe el nombre exactamente como aparece.", "Nombre de hoja")
If nombreHoja = "" Then Exit Sub
On Error Resume Next
Set hoja = ThisWorkbook.Worksheets(nombreHoja)
On Error GoTo 0
If hoja Is Nothing Then
MsgBox "La hoja '" & nombreHoja & "' no existe.", vbCritical
Exit Sub
End If
ElseIf LCase(buscarEn) = "libro" Then
Else
MsgBox "Opción inválida. Escribe solo 'libro' o 'hoja'.", vbExclamation
Exit Sub
End If
tipoBusqueda = InputBox("¿La búsqueda es exacta o parcial? Escribe: exacta / parcial", "Tipo de búsqueda")
If tipoBusqueda = "" Then Exit Sub
If LCase(tipoBusqueda) <> "exacta" And LCase(tipoBusqueda) <> "parcial" Then
MsgBox "Opción inválida. Escribe solo 'exacta' o 'parcial'.", vbExclamation
Exit Sub
End If
valorBuscado = InputBox("¿Qué valor deseas buscar?", "Valor a buscar")
If valorBuscado = "" Then Exit Sub
Application.ScreenUpdating = False
encontrado = False
If Not hoja Is Nothing Then
For Each celda In hoja.UsedRange
If (LCase(tipoBusqueda) = "exacta" And celda.Value = valorBuscado) Or (LCase(tipoBusqueda) = "parcial" And InStr(1, celda.Value, valorBuscado, vbTextCompare) > 0) Then
celda.Interior.Color = RGB(255, 255, 0)
encontrado = True
End If
Next celda
Else
For Each hoja In ThisWorkbook.Worksheets
For Each celda In hoja.UsedRange
If (LCase(tipoBusqueda) = "exacta" And celda.Value = valorBuscado) Or (LCase(tipoBusqueda) = "parcial" And InStr(1, celda.Value, valorBuscado, vbTextCompare) > 0) Then
celda.Interior.Color = RGB(255, 255, 0)
encontrado = True
End If
Next celda
Next hoja
End If
Application.ScreenUpdating = True
If encontrado Then
MsgBox "Búsqueda completada. Las coincidencias fueron resaltadas en amarillo.", vbInformation
Else
MsgBox "No se encontraron coincidencias.", vbExclamation
End If
End Sub