Thanks for reply Norman
"why A single cell search would be made in VBA. Interrogation of the cells value property would appear simpler!
With code that searches over a range or sometimes a single cell, we did not want to make two code paths one for the single cell case and another code path for the multicell case. Imagine that the range is dynamically calculated and could be anywhere from one cell to many cells, then you could use Range.Find in both cases. In Excel 2003 this does not appear to be the case. In Excel 2000 automated with VB6 it seemed like it worked just fine, Range.Find always returned a cell within the range
----- Norman Jones wrote: ----
Hi Vasant
Doing a manual search with a single cell selected this is certainly true an
is equivalent to the VBA form: Cells.Find
In VBA,however, I do not think this is true. Testing under xl2k, a searc
limited to a single cell gives the expected results for me
This, however,begs the question of why A single cell search would be made i
VBA. Interrogation of the cells value property would appear simpler
--
Regard
Norma
"Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in messag
..
When you run Find with a single cell selected VBA always assumes that yo
want to search the entire worksheet. AFAIK this is by design
--
Vasan
"Frank Jones" wrote in messag
..
Greetings
When using Range.Find on a single celled range ($A$1), Find returns cel
outside that Range
When attempting to run the following VB6 code (see below and/or attache
Zi
file) automating Excel 2003, against an Excel 2003 Fil
(FindNextTest.xls
containing one worksheet with only one row of values, with "Column A
tex
in A1 and "Column B" text in B1. The Find method on a Range set t
$A$1(single cell) returns a cell outside the range (in this case, $B$1)
I
appears as though it disregards the range it was told to look within an
looks within the entire spreadsheet
In the test Excel file the initial Range Cells(1,1) = Cells(1,1) (i.e
$A$1) contains no instance of "Column B". The first "Column B" is i
th
cell $B$1. So the expectation here would have been for then Find() o
rang
$A$1 to return null, instead oCellFound is set to $B$1, the first cel
wit
"Column B" in it. Additionally, we found that if the initial searc
rang
was expanded to not just be a single cell (i.e. made the Range more tha
single cell, like Cells(1,1), Cells(2,1) ($A$1:$A$2) ) then the Find(
doe
return null and not $B$1
Additionally if we take that return from Range.Find() and feed it into
second call Range.FindNext() we get the Error 1004 Unable to get th
FindNext property of the Range class because the initial return $B$1 i
outside of the $A$1 range, meaning that a cell returned fro
Range.Find(
may not be valid to pass into Range.FindNext()
Is the code below calling the Range.Find() incorrectly? Is there som
different behavior for single celled range versus multiple cell range a
fa
as Find staying withing the Range? According to docs it seems tha
Range.Find() should either return Null or return a cell within th
Range
but there should not be any circumstances under which Range.Find(
return
cell outside of the Range, no
Thanks for any help that can be provided
-Frank Jone
Vb6 code below
===========
VERSION 5.0
Begin VB.Form frmMai
Caption = "FindNext Test
ClientHeight = 279
ClientLeft = 6
ClientTop = 345
ClientWidth = 6000
LinkTopic = "Form1"
ScaleHeight = 2790
ScaleWidth = 6000
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command1
Caption = "Go"
Height = 375
Left = 1080
TabIndex = 4
Top = 2280
Width = 1335
End
Begin VB.TextBox Text2
Height = 975
Left = 720
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 1
Top = 1080
Width = 5055
End
Begin VB.TextBox Text1
Height = 375
Left = 840
TabIndex = 0
Top = 480
Width = 4815
End
Begin VB.Label Label2
Caption = "Result:"
Height = 495
Left = 120
TabIndex = 3
Top = 1080
Width = 735
End
Begin VB.Label Label1
Caption = "Excel File:"
Height = 375
Left = 120
TabIndex = 2
Top = 480
Width = 735
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Load()
Me.Text1.Text = App.Path & "\FindNextTest.xls"
End Sub
Private Sub Command1_Click()
Me.Text2.Text = FindTest(Text1.Text)
End Sub
Function FindTest(strFilePath As String) As String
Dim oExcel As Excel.Application
Dim oSearchRange As Range
Dim oCellFound As Range
Dim oWorksheet As Worksheet
Dim oWorkbook As Workbook
Set oExcel = New Excel.Application
Set oWorkbook = oExcel.Workbooks.Open(strFilePath, UpdateLinks:=0,
ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
Set oWorksheet = oWorkbook.Worksheets(1)
' Note: If this search Range is set to be more than just $A$1 then
it
works as expected
Set oSearchRange = oWorksheet.Range(oWorksheet.Cells(1, 1),
oWorksheet.Cells(1, 1))
Set oCellFound = oSearchRange.Find(What:="Column B",
LookIn:=XlFindLookIn.xlValues, LookAt:=XlLookAt.xlPart)
If oCellFound Is Nothing Then
FindTest = "It Worked - Should not have found a ""Column B"" in
Range " & oSearchRange.Address
Else
' This cell is outside the search range
FindTest = "Range.Find on range " & oSearchRange.Address & "
returned cell " & oCellFound.Address
' This line will get Error 1004 Unable to get the FindNext
property
of the Range class
FindTest = FindTest & vbCrLf & "FindNext(" & oCellFound.Address
& ")
Result "
On Error Resume Next
Set oCellFound = oSearchRange.FindNext(oCellFound)
If Err.Number < 0 Then
FindTest = FindTest & "Error Occurred: " & Err.Number & " "
& Err.Description
Else
FindTest = FindTest & "Succeeded!"
End If
On Error GoTo 0
End If
Call oWorkbook.Close(SaveChanges:=False)
Call oExcel.Quit
End Function