Excel .Find,函数VB 2010的结果

时间:2012-07-09 11:46:46

标签: vb.net excel

我正在尝试在VB 2010中使用.Find来查找函数的结果。

这是我的代码:

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

我已经尝试了xlValuesxlFormulas

excel功能很简单,看起来像这样:

=G9+7

结果是一个日期,19-Jul,这就是我想要搜索的内容。

BR 塞巴斯蒂安

3 个答案:

答案 0 :(得分: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

更新

请注意,上面的代码专门搜索日期'7/16/2012',因为我没有看到您的搜索字符串所在的位置。您可以用以下代码替换该行:

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

答案 1 :(得分:0)

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

答案 2 :(得分: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