D
Davy
I have written the VB below to search a genealogical database and make
visible only those rows which have the surname of the enquirer. It works OK
but is very slow; about 10 sec per 1000 rows so the whole search takes 2
mins. Can anybody see anything I am doing wrong?
I initially used a For Each loop but that seemed to search about 64000
records.
many thanks
David
Public Sub HideRows5()
Dim FoundCell As Range
Dim Searchword
Dim Surname As String
Dim RowCount As Single
Dim RowNumber As Single
Dim MatchCount As Integer
MatchCount = 0
'Set all spreadsheet rows to visible else Find searches only visible rows
left from last search.
Worksheets("Data").Rows.Hidden = False
'Obtain Surname to search for
Surname = InputBox(Prompt:="Please type into the box below the surname to
search for. The computer will then search for all surnames and maiden names
that match. This may take a couple of minutes")
'Worksheets("Data").Activate 'Switch to the Data Worksheet for following
processing
' count number of rows having data. The column being testing must have a
value in each row.
RowCount = Cells(Rows.Count, "C").End(xlUp).Row
'Step through the rows.
For RowNumber = 2 To RowCount
' The FIND method finds specific information in a range, and returns a Range
object that represents the first cell where that information is found.
Returns Nothing if no match is found. Doesn't affect the selection or the
active cell.
Set FoundCell = Range(Cells(RowNumber, 3), Cells(RowNumber,
5)).Find(What:=Surname, _
MatchCase:=False, _
LookIn:=xlValues, LookAt:=xlPart)
If FoundCell Is Nothing Then
Rows(RowNumber).Hidden = True 'set property to hidden
Else
MatchCount = MatchCount + 1
End If
Next
MsgBox Prompt:=MatchCount & " Records Found for " & Surname & " from " &
RowCount & " records."
'Unhide the title row
Worksheets("Data").Rows(1).Hidden = False
Range("A1", "A2").Activate ' Go to top of worksheet
End Sub
visible only those rows which have the surname of the enquirer. It works OK
but is very slow; about 10 sec per 1000 rows so the whole search takes 2
mins. Can anybody see anything I am doing wrong?
I initially used a For Each loop but that seemed to search about 64000
records.
many thanks
David
Public Sub HideRows5()
Dim FoundCell As Range
Dim Searchword
Dim Surname As String
Dim RowCount As Single
Dim RowNumber As Single
Dim MatchCount As Integer
MatchCount = 0
'Set all spreadsheet rows to visible else Find searches only visible rows
left from last search.
Worksheets("Data").Rows.Hidden = False
'Obtain Surname to search for
Surname = InputBox(Prompt:="Please type into the box below the surname to
search for. The computer will then search for all surnames and maiden names
that match. This may take a couple of minutes")
'Worksheets("Data").Activate 'Switch to the Data Worksheet for following
processing
' count number of rows having data. The column being testing must have a
value in each row.
RowCount = Cells(Rows.Count, "C").End(xlUp).Row
'Step through the rows.
For RowNumber = 2 To RowCount
' The FIND method finds specific information in a range, and returns a Range
object that represents the first cell where that information is found.
Returns Nothing if no match is found. Doesn't affect the selection or the
active cell.
Set FoundCell = Range(Cells(RowNumber, 3), Cells(RowNumber,
5)).Find(What:=Surname, _
MatchCase:=False, _
LookIn:=xlValues, LookAt:=xlPart)
If FoundCell Is Nothing Then
Rows(RowNumber).Hidden = True 'set property to hidden
Else
MatchCount = MatchCount + 1
End If
Next
MsgBox Prompt:=MatchCount & " Records Found for " & Surname & " from " &
RowCount & " records."
'Unhide the title row
Worksheets("Data").Rows(1).Hidden = False
Range("A1", "A2").Activate ' Go to top of worksheet
End Sub