[英]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.