Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range.Find fails on ranges that are both hidden AND part of a filter

Tags:

excel

vba

I'm experiencing a peculiar problem in Excel 2003 where the Range.Find method fails when searching for a value in a cell that is both hidden and part of a filtered range.

To be clear, this is the method call in question:

Cells.Find(SearchString, LookIn:=xlFormulas, LookAt:=xlWhole)
  • If the cell containing SearchString is merely hidden, Range.Find works.
  • If the cell containing SearchString is merely part of a filtered range (but not hidden), Range.Find works.
  • If the cell containing SearchString is both hidden (by a filter or otherwise), AND is part of a filtered range, Range.Find fails.

Many sources on various Excel sites and forums claim that specifying "LookIn:=xlFormulas" will force Range.Find to search within hidden cells. Though nonsensical, that seems to be true if SearchString is in a cell that is merely hidden. If the cell is both hidden and part of a filtered range, it fails.

Note that it doesn't matter if the cell is being hidden by the filter. For example, you can search for the heading of a filtered range (which will never be hidden by the filter itself), but if that heading happens to be in a column you've hidden, Range.Find will fail.

Is there any Excel method that will just reliably search cells without regard for whether or not they happen to be hidden and/or part of a filter?

like image 462
Nickolas Avatar asked Apr 09 '15 23:04

Nickolas


People also ask

How do you find the range in VBA?

VBA to Find Value in a Range – MatchCase The start cell must be in the specified range. When we write MatchCase:=True that means find string is case sensitive. This macro will search for the string “ab” from “B3” and returns matched range address. As we mentioned MatchCase:=True, it will look for exact match only.

What does Range find return if nothing is found?

This method returns Nothing if no match is found. The Find method does not affect the selection or the active cell.

How do you pass the range of a function?

To allow for a variable number of ranges to be used in the function, you need to declare a ParamArray variant array in your argument list. Then, you can process each of the ranges in the array in turn. This function could then be used in the worksheet to add multiple ranges.


1 Answers

More detail would be helpful

  • for a 1D range you could use Match
  • for 2D a variant array either looking through each element of the array, or applying MATCH to each column of the array

Samples below 1D

Sub D1()

Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Range("C5:C100")
Dim StrTest As String
Dim X As Variant

StrTest = "Filtered"

X = Application.Match(StrTest, rng1, 0)
If IsError(X) Then
    MsgBox "no match"
Else
    MsgBox "Found in position " & X
    Set rng2 = rng1.Cells(X)
End If
End Sub

Sub D2()

2D

Dim X
Dim lngRow As Long
Dim lngCol As Long
Dim StrTest As String


X = Range("C5:D100").Value2
StrTest = "Filtered"

For lngRow = 1 To UBound(X, 1)
    For lngCol = 1 To UBound(X, 2)
        If X(lngRow, lngCol) = StrTest Then
         Set rng1 = [c5].Offset(lngRow - 1, lngCol - 1)
         MsgBox "Found in position " & rng1.Address
        End If
    Next
Next

End Sub
like image 175
brettdj Avatar answered Oct 16 '22 10:10

brettdj