我有一个功能,我写在VB中的问题。 该工具将打开一个Excel工作表并且也在该工作表中搜索两个值。用多个搜索参数搜索
我写的函数,看上去如果在列“M”,并在同一行的列“N”的值是相同的:
的Excel工作表的结构如下
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
的Eroor:如Creterium 1和2,如果是这样的话,它会在列“O”
我的代码看起来像返回值发生在For Each循环中,如果他将Criteria1和2比较Cell值。
我现在坚持了一段时间,我想也许你们中的一些人有一个想法!
'strAddress =中(strAddress,2,1)'这是错误的方式来获得地址。它会失败说'$ AA $ 1' –
另一个提示。 'objWorksheet.Columns(strAddress&“:”&strAddress)'为什么要循环遍历该列中的所有单元格(1048576,如果是xl2007 +)?找到该列的最后一行,然后构建你的内容。 –
@ Siddharth Rout:感谢您的提示与地址。我没有想到这一点。另外我会尽快实施。感谢提示 – Moosli