我在 vb 中编写的函数有问题。该工具会打开一个 Excel 工作表,并在该工作表中搜索两个值。
Excel 工作表的结构如下:
我编写的函数,查看同一行中“M”列和“N”列中的值是否与 Creterium 1 和 2 相同。如果是这样,它将返回“O”列中的值"
我的代码如下所示:
Function twoStrSearch(ByVal criteria1 As String, ByVal criteria2 As String, ByVal strPrimarySearchColumn As String, _
ByVal Offset_Krit2 As Integer, ByVal Offset_result As Integer, _
ByVal objWorksheet As Microsoft.Office.Interop.Excel.Worksheet) As VariantType
'********************************************************************************************
'Function for Searching an Excel Sheet.
'If the Sheet Contains the two Criterias in the same row it will return the search Value
'********************************************************************************************
'Parameter: Explanation:
'criteria1 The first comparison value
'criteria2 The second comparison value
'strPrimarySearchColumn The Name of the Row where the first comparsion value is
'Offset_Krit2 The Offset Value where the second comparison value is
'Offset_Ergebnis The Offset Value where the Search result is what will be returned
'objWorksheet The object of the Excel Sheet that should be searched in
'********************************************************************************************
Dim strAddress As String
Dim area As Microsoft.Office.Interop.Excel.Range
Dim range As Microsoft.Office.Interop.Excel.Range
'Get's the letter of the Column
strAddress = objWorksheet.Cells.Find(What:=strPrimarySearchColumn).Address
strAddress = Mid(strAddress, 2, 1)
area = objWorksheet.Columns(strAddress & ":" & strAddress) 'Range over the Column
For Each range In area
'If both criteria in the same Row are True then get the result
If range.Value2.ToString = criteria1 And range.Offset(0, Offset_Krit2).Value = criteria2 Then
twoStrSearch = range.Offset(0, Offset_result).Value
Exit Function
End If
Next
twoStrSearch = "--" 'if nothing found result is "--"
End Function
如果他将单元格值与 Criteria1 和 2 进行比较,则 Eroor 发生在 For Each 循环中。
我现在坚持了一段时间,我想也许你们中的一些人有一个想法!