2012-07-09 47 views
-1

我试图在VB 2010中使用.Find来查找函数的结果。Excel。查找,函数结果VB 2010

这是我的代码:

firstFind = xlWorkSheet.Range("E10", "CM10").Find(searchDate, ,  Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False) 

我曾经尝试都xlValuesxlFormulas

的Excel函数是非常简单的,看起来像这样:

=G9+7 

结果是一个日期,7月19日,而这正是我希望能够搜索。

BR 塞巴斯蒂安

回答

0

以下是我解决了这个问题,而无需使用.find

Const FOUND As Integer = 1, NOT_FOUND = 0 
Dim worksheetNr As Integer = 0, columnNr As Integer, dateFound As Integer = NOT_FOUND 

While worksheetNr < xlWorkBook.Worksheets.Count And dateFound = NOT_FOUND 

    Try 
     xlWorkSheet = xlWorkBook.Worksheets(worksheetNr) 
     xlWorkSheet.Activate() 
     columnNr = 7 
     While dateFound = NOT_FOUND And (columnNr < 100) 
      If (xlWorkSheet.Cells(10, columnNr).Value.Equals(searchDate)) Then 
       dateFound = FOUND 
      Else 
       columnNr += 1 
      End If 

     End While 

    Catch ex As Exception 
     Console.Write("") 
    End Try 

End While 
0

首先。 Excel将日期看作整数,因此7月19日实际上是一个数字(将单元格设置为= G9 + 7为一般) 例如2012年7月19日的日期= 41109. 希望这会有所帮助。

1

你不能循环访问列而不是使用find函数吗? 这样的事情可能会更有效或更容易定制前进:

Sub Test() 

    Dim iColumnSearch As Long 
    Dim dtSearchDate As Date 
    Dim rngFoundInCell As Range 

    dtSearchDate = "7/16/2012" 

    '- loop through columns E to CM (Column #'s 5 to 91) 
    For iColumnSearch = 5 To 91 
     If Cells(10, iColumnSearch).Value = dtSearchDate Then 

      Set rngFoundInCell = Cells(10, iColumnSearch) 
      Exit For 
     End If 
    Next iColumnSearch 

    If Not rngFoundInCell Is Nothing Then '-if was found 
     MsgBox "Found in cell: " & rngFoundInCell.Parent.Name & "!" & rngFoundInCell.Address(External:=False) 
    End If 


    Set rngFoundInCell = Nothing 

End Sub 

更新:

注意,因为我没有在上面的代码中查找日期“2012/7/16”专看看你的搜索字符串的位置。你可能会取代符合:

dtSearchDate = Range("G9").value + 7