[英]Find range by value Excel-VBA
I need a function or something to return me a range in which specified value were found. 我需要一个函数或一些东西来向我返回找到指定值的范围。
I am thinking about looping through specified range checking if the value in each cell equals the value I specified and building a range. 我正在考虑遍历指定的范围,检查每个单元格中的值是否等于我指定的值并建立一个范围。 Is there more elegant method?
有没有更优雅的方法?
Here is a function I built. 这是我建立的功能。 It returns a union range where the needed value were found:
它返回找到所需值的并集范围:
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
try the below code. 试试下面的代码。 You can get the results in cells itself by using the formula.
您可以使用公式在单元格本身中获得结果。
=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
Hope you satisfied? 希望你满意吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.