[英]Find function in VBA - range restriction
下午
我有一个不错的工作find函数,但是它在所有列上进行搜索。 现在,我尝试对范围定义进行各种更改,并对A列设置了选择限制。
我遇到的问题是,它忽略了任何限制,并继续采用自己的方法。 代码在下面,它使用两个函数,第一个是Find,第二个是Find Next。
有人可以帮我一下,建议我如何将搜索范围限制在电子表格的A列。
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range(A:A).End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
updateFields anchorCell:=c
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
Case vbCancel
End Select
End If
Else: MsgBox strFind & " not listed"
End If
End With
谢谢
您设置范围的方式不正确。 首先,您在A:A
中缺少双引号。 其次,即使您输入双引号, rSearch
也会始终为A1
您可以通过以下两种方式设置范围
代码1
Dim lRow As Long
'~~> ws is the relevant sheet
lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rSearch = ws.Range("A1:A" & lRow)
代码2
Set rSearch = ws.Columns(1)
如果只想在A列中查找,请尝试将“ rsearch”范围定义为:
Range("A1").End(xlDown).Select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.