excel - 如何在 range.find 中查找双数据类型

标签 excel vba find range double

输入:

enter image description here

Sub RangeFindWithDoubleType()
    Dim strResult As range
    
    With ActiveSheet.range("A2:A11")
        On Error Resume Next
        
        Set strResult = .Find(range("D1")).Address
        
        If strResult Is Nothing Then
            MsgBox "Value not found"
        Else
            MsgBox strResult
        End If
    End With
    
    range("D2") = strResult
End Sub

使用find函数查找double类型数据时,得到的结果为Nothing。

如何找到双数据类型单元格的地址?

最佳答案

使用 Range.Find 在范围内首次出现数字

Sub RangeFindWithDoubleType()
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    Dim srg As Range: Set srg = ws.Range("A2:A11") ' Source Range
    Dim cCell As Range: Set cCell = ws.Range("D1") ' Criteria Cell
    Dim dCell As Range: Set dCell = ws.Range("D2") ' Destination Cell
    
    Dim cValue As Variant: cValue = cCell.Value
    
    Dim sCell As Range ' First Found Cell in Source Range
    
    If VarType(cValue) = vbDouble Then ' is a number
        Set sCell = srg.Find( _
            cValue, srg.Cells(srg.Cells.Count), xlFormulas, xlWhole)
        If sCell Is Nothing Then ' number not found
            dCell.Value = Empty
        Else ' number found
            dCell.Value = sCell.Address(0, 0)
        End If
    Else ' not a number
        dCell.Value = Empty
    End If
    
End Sub

关于excel - 如何在 range.find 中查找双数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72242833/

相关文章:

excel - 从 Tableau 中越来越多的 Excel 文件/CSV 中引用数据?

excel - IF 函数不会将结果返回为 Pass 但数据可用

excel - 电力BI(DAX): Distinct Count Filtered by Condition

vba - 这个 Excel VBA 宏太慢了

c - 使用 execv 查找异常结果

php - 带有嵌入图表的 Excel.Application 复制表

excel - 在 protected 工作表上启用宏按钮 - Excel

vba - shape.Parent 不返回 Slide

c++ - MPL pos 是一个未记录的元函数吗?

C++ std::find lambda 表达式