質問

I have the TestComplete VB code that returns cell in an excel file that contains the value specified in the code.

The problem is that the words searched by the code in the excel are not the exact match of the value specified in the code.

In vba, Replacing "xlPart" with "xlWhole" gives a solution to my problem.

I want to return only the cell that matches entire cell content.

The code I use,

Function FindCell(sheet, searchRange, Value, returnWhat)

 Dim CellPosition, c, Search_In


 If searchRange = "Entire_Sheet" then
    Set Search_In = sheet.Cells
 else
     Set Search_In = sheet.Range(searchRange)
 End If


  With Search_In
     Set c = .Find(Value)
     If Not c Is Nothing Then

         if returnWhat = "Row_Number" then
           CellPosition = c.row
         elseif returnWhat = "Column_Number" then
           CellPosition = c.column 
         end if

     End If
  End With

  FindCell = CellPosition

End Function

What should I edit in here to get a solution?

役に立ちましたか?

解決

The code below solved my problem of finding the cell that contains the exact match of the value am searching for.

Function FindCell(sheet, searchRange, Value, returnWhat)

 Dim CellPosition, c, Search_In
 Dim i, CellArray()


 If searchRange = "Entire_Sheet" then
    Set Search_In = sheet.Cells
 else
     Set Search_In = sheet.Range(searchRange)
 End If


 With Search_In
   Set c = .Find(Value)
   i = 0
   If Not c Is Nothing Then
      firstAddress = c.Address
      Do
        ReDim Preserve CellArray(i)
        CellArray(i) = ColumnLetter(c.column) & c.row
        if c = Value then
           if returnWhat = "Row_Number" then
               CellPosition = c.row
           elseif  returnWhat = "Column_Number" then
              CellPosition = c.column
           End if
        'Log.Message("The Jing value is found in " & CellArray(i))
        Exit Do
        End if
        i = i + 1
       Set c = .FindNext(c)
      Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
 End With

  FindCell = CellPosition

End Function

他のヒント

This code works in VBA don't know about VB:

Function FindCell(sheet As Worksheet, searchRange As String, Valuee As Variant, returnWhat As String) As Long
 Dim CellPosition, c, Search_In, r
 CellPosition = -9999
 If searchRange = "Entire_Sheet" Then
    Set Search_In = sheet.Cells
 Else
     Set Search_In = sheet.Range(searchRange)
 End If
  With Search_In
     Set c = Nothing
     For Each r In Search_In
        If r.Value = Valuee Then
            Set c = r
        End If
     Next
     If Not c Is Nothing Then
         If returnWhat = "Row_Number" Then
           CellPosition = c.Row
         Else
           CellPosition = c.Column
         End If
     End If
  End With
  FindCell = CellPosition
End Function
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top