我有一個功能,我寫在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