使用find方法搜索值

时间:2014-08-28 12:58:17

标签: excel vba excel-vba

当代码执行此行时,设置r = tbl.Find(what:= mx),如果单元格包含重复的零或任何数字,则重复返回相同的位置。例如,如果我有重复的零搜索:它给我结果为V1B,V1B,V1B而不是返回V1B,V2B和V2C。我认为只有在重复这些价值时才会这样做。如何修改此代码?

  V1   V2   V3 

A 1 5 9

B 0 0 0

C 7 8 4

Sub Finder()     Dim tabl As Range,headr As Range,mx As Double     Dim r As Range,colr As Range     Dim p As Long

With Sheet1

    For p = 1 To 9
        Set tbl =.Range("B3:D5")
        Set headr =.Range("B2:D2")
        Set colr =.Range("A3:A5")
        mx =.Cells(6 + p, 3)
        Set r = tbl.Find(what:=mx)
        v = Intersect(headr, r.EntireColumn).Value
        v = v & " " & Intersect(colr, r.EntireRow).Value
        .Cells(6 + p, 2) = v
    Next p
End with

End Sub

1 个答案:

答案 0 :(得分:0)

以下是查找单元格块中所有零的典型方法(它将忽略空格!)

Sub ZeroFinder()
    Dim Block As Range, Zero As Long, _
        r As Range, msg As String, st As String
    Set Block = Range("D6:O32")
    Zero = 0
    Set r = Block.Find(What:=Zero, after:=Block(1))
    If r Is Nothing Then
        MsgBox "no zeros"
        Exit Sub
    End If
    msg = r.Address(0, 0)
    st = msg
    Do Until r Is Nothing
        Set r = Block.FindNext(after:=r)
        If r.Address(0, 0) = st Then Exit Do
        msg = msg & vbCrLf & r.Address(0, 0)
    Loop
    MsgBox msg
End Sub