|
Public Sub 查找数据()
Dim myRange1 As Range
Dim myRange2 As Range
Dim myRow As Long
Dim str As String
str = InputBox("请输入查找内容:")
If str = "" Then End
Set myRange1 = Cells '指定查询范围
On Error Resume Next
myRow = WorksheetFunction.Match(str, myRange1, 0) '指定查询条件
On Error GoTo 0
If myRow = 0 Then
MsgBox "没有找到符合条件的单元格"
Else
Set myRange2 = myRange1.Cells(myRow)
MsgBox "符合条件的单元格为:" & myRange2.Address(False, False)
myRange2.EntireRow.Select
End If
Set myRange1 = Nothing
Set myRange2 = Nothing
End Sub
想请大家修改成 查询当前工作表B4:J500这个区域内容
查询的内容,改为 提示框中输入内容,就是当我们执行本宏命令时,弹出一个提示框,让我们输入查找的字符后,确定,首先显示查找字符的位置,确定后,光标选中查找字符所在的行。
请老师们帮忙。。谢谢大家
回复 yjwdjfqb 的帖子
- Public Sub 查找数据()
- Dim myRange1 As Range
- Dim myRange2 As Range
- Dim myRow As Long
- Dim str As String
- On Error Resume Next
-
- str = InputBox("请输入查找内容:")
- If str = "" Then End
- Set myRange1 = ActiveSheet.Range("B4:J500") '指定查询范围
- With myRange1
- Set c = .Find(str, LookIn:=xlValues)
- If Not c Is Nothing Then
- firstAddress = c.Address(0, 0)
- Do
- Set c = .FindNext(c)
- Loop While Not c Is Nothing And c.Address(0, 0) <> firstAddress
- End If
- End With
- If c Is Nothing Then
- MsgBox "没有找到符合条件的单元格"
- Else
- MsgBox "符合条件的单元格为:" & firstAddress
- Range(firstAddress).EntireRow.Select
- End If
- End Sub
复制代码
|
|