测试完整VB代码以查找包含代码中指定的值(完全匹配)的单元格

时间:2014-03-17 13:04:29

标签: vb.net excel search match testcomplete

我有TestComplete VB代码,它返回包含代码中指定值的excel文件中的单元格。

问题是excel中代码搜索的单词与代码中指定的值不完全匹配。

在vba中,用“xlWhole”替换“xlPart”可以解决我的问题。

我想只返回匹配整个单元格内容的单元格。

我使用的代码,

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

我应该在这里编辑什么来获得解决方案?

2 个答案:

答案 0 :(得分:1)

下面的代码解决了我找到包含搜索值的完全匹配的单元格的问题。

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

答案 1 :(得分:0)

此代码适用于 VBA ,不知道 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