2012-07-09 43 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