簡體   English   中英

按值查找范圍Excel-VBA

[英]Find range by value Excel-VBA

我需要一個函數或一些東西來向我返回找到指定值的范圍。

我正在考慮遍歷指定的范圍,檢查每個單元格中的值是否等於我指定的值並建立一個范圍。 有沒有更優雅的方法?

這是我建立的功能。 它返回找到所需值的並集范圍:

Private Function FindRange(what As String, lookin As Range) As Range

    Dim cell As Range

    On Error GoTo errhandler

    For Each cell In lookin.Cells
        If UCase(cell.Value) = UCase(what) Then

            If findrange Is Nothing Then
                Set findrange = cell
            Else
                Set findrange = Application.Union(findrange, cell)
            End If

        End If
    Next cell

    Exit Function

errhandler:
    findrange = CVErr(xlErrNA)

End Function

試試下面的代碼。 您可以使用公式在單元格本身中獲得結果。

=findvalueandrange(searchstring,selectyourrange)

Public Function findvalueandrange(findingtext As String, r As Range) As String
    Dim cell As Range
    For Each cell In r.Cells
        If LCase(cell.Value) = LCase(findingtext) Then
            If findvalueandrange = "" Then
                findvalueandrange = cell.Address & ":" & cell.Value
            Else
                findvalueandrange = findvalueandrange & "|" & cell.Address & ":" & cell.Value
            End If
        End If
    Next cell
End Function

希望你滿意嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM