用多个search参数search

我有一个function,我写在VB中的问题。 该工具打开一个Excel工作表,并在该工作表中search两个值。

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 

Eroor发生在For Each循环中,如果他将Cell值与Criteria1和2比较。

我现在坚持一会儿,我想也许你们有一个想法!

For Each Loop有两个主要问题:它遍历列中的所有范围,而不指定实际上需要单元格(列是范围,单元格是范围等),默认情况下,你想遍历整个列,因此.Value2触发一个错误(它不能被应用到许多单元格)。 此外,您正在使用.ToString没有确认在给定的单元格中的值不是空(不是Nothing )。 更正后的代码:

 For Each range In area.Cells 'If both criteria in the same Row are True then get the result If (range.Value2 IsNot Nothing) Then 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 End If Next 

你的代码还有一些其他的问题,例如:

 strAddress = objWorksheet.Cells.Find(What:=strPrimarySearchColumn).Address 

如果没有find预期的结果,这条线会触发一个错误。 更合适的方法是:

 strAddress = "" Dim tempRange As Microsoft.Office.Interop.Excel.Range = objWorksheet.Cells.Find(What:=strPrimarySearchColumn) If (tempRange IsNot Nothing) Then strAddress = tempRange.Address End If 

您可以直接从范围( tempRange )获取行/列,而使用当前的方法。 请参阅上面的Siddharth Rout的评论。

不适用于积分(仅用于解释目的)

概要。

  1. 如果search文本位于列表AA1则提取地址的方式会导致错误
  2. 你不需要地址来构build你的范围。 您可以使用列号。
  3. 尽pipe所有单元格都没有点循环(在xl2007 +的情况下为1048576)通过查找search列中的最后一行来构build相关的范围

假设你的数据看起来像这样

在这里输入图像说明

代码:(在VS 2010 Ultimate + Office 2010教授中进行了testing和testing)

尝试这个。 我已经评论了代码,所以让我知道是否有什么不合理的。

 Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 '~~> Define your Excel Objects Dim xlApp As New Excel.Application Dim xlWorkBook As Excel.Workbook Dim objWorksheet As Excel.Worksheet Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click '~~> Open relevant file xlWorkBook = xlApp.Workbooks.Open("C:\MyFile.xlsx") '~~> Display Excel xlApp.Visible = True '~~> Set your first worksheet objWorksheet = xlWorkBook.Sheets(1) Dim Ret = twoStrSearch("1", "text-x", "TextNumber", -1, 1) MsgBox (Ret) '~~> Close the File xlWorkBook.Close (False) '~~> Quit the Excel Application xlApp.Quit() '~~> Clean Up releaseObject (objWorksheet) releaseObject (xlWorkBook) releaseObject (xlApp) End Sub Function twoStrSearch(ByVal criteria1 As String, ByVal criteria2 As String, ByVal strPrimarySearchColumn As String, ByVal Offset_Krit2 As Integer, ByVal Offset_result As Integer) As String Dim area As Excel.Range = Nothing Dim range As Excel.Range = Nothing Dim aCell As Excel.Range = Nothing Dim ColNo As Integer, lRow As Integer '~~> Find which column as the search text aCell = objWorksheet.Cells.Find(What:=strPrimarySearchColumn) '~~> Set it to "--" in case nothing is found twoStrSearch = "--" '~~> if found If aCell IsNot Nothing Then '~~> Get the column number ColNo = aCell.Column '~~> Get last row of that column lRow = objWorksheet.Cells(objWorksheet.Rows.Count, ColNo).End(Excel.XlDirection.xlUp).Row '~~> Construct your range from row 2 onwards. Row1 has headers area = objWorksheet.range(objWorksheet.Cells(2, ColNo), objWorksheet.Cells(lRow, ColNo)) 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(, Offset_Krit2).Value = criteria2 Then twoStrSearch = range.Offset(, Offset_result).Value Exit For End If Next End If releaseObject (area) releaseObject (range) releaseObject (aCell) Return twoStrSearch End Function '~~> Release the objects Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject (obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub End Class 

输出:

在这里输入图像描述