Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4822|回复: 6

[已解决]VBA_查找数据代码修改

[复制链接]
发表于 2011-8-15 10:19 | 显示全部楼层 |阅读模式
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这个区域内容
查询的内容,改为 提示框中输入内容,就是当我们执行本宏命令时,弹出一个提示框,让我们输入查找的字符后,确定,首先显示查找字符的位置,确定后,光标选中查找字符所在的行。

请老师们帮忙。。谢谢大家

最佳答案
2011-8-15 11:00
回复 yjwdjfqb 的帖子

  1. Public Sub 查找数据()
  2.     Dim myRange1 As Range
  3.     Dim myRange2 As Range
  4.     Dim myRow As Long
  5.     Dim str As String
  6.     On Error Resume Next
  7.    
  8.     str = InputBox("请输入查找内容:")
  9.     If str = "" Then End
  10.     Set myRange1 = ActiveSheet.Range("B4:J500")       '指定查询范围
  11.     With myRange1
  12.         Set c = .Find(str, LookIn:=xlValues)
  13.         If Not c Is Nothing Then
  14.             firstAddress = c.Address(0, 0)
  15.             Do
  16.                 Set c = .FindNext(c)
  17.             Loop While Not c Is Nothing And c.Address(0, 0) <> firstAddress
  18.         End If
  19.     End With
  20.     If c Is Nothing Then
  21.         MsgBox "没有找到符合条件的单元格"
  22.     Else
  23.         MsgBox "符合条件的单元格为:" & firstAddress
  24.         Range(firstAddress).EntireRow.Select
  25.     End If
  26. End Sub
复制代码

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2011-8-15 10:29 | 显示全部楼层
回复 yjwdjfqb 的帖子

  1. Public Sub 查找数据()
  2.     Dim myRange1 As Range
  3.     Dim myRange2 As Range
  4.     Dim myRow  As Long
  5.     Dim str As String
  6.    
  7.     str = InputBox("请输入查找内容:")
  8.     If str = "" Then End
  9.     Set myRange1 = ActiveSheet.Range("B4:J500")       '指定查询范围
  10.     On Error Resume Next
  11.     myRow = WorksheetFunction.Match(str, myRange1, 0)    '指定查询条件
  12.     On Error GoTo 0
  13.     If myRow = 0 Then
  14.         MsgBox "没有找到符合条件的单元格"
  15.     Else
  16.         Set myRange2 = myRange1.Cells(myRow)
  17.         MsgBox "符合条件的单元格为:" & myRange2.Address(False, False)
  18.         myRange2.EntireRow.Select
  19.     End If
  20.     Set myRange1 = Nothing
  21.     Set myRange2 = Nothing
  22. End Sub
复制代码

回复

使用道具 举报

 楼主| 发表于 2011-8-15 10:38 | 显示全部楼层
回复 那么的帅 的帖子

帅哥,我测试了下,不行,麻烦传个附件好吧
回复

使用道具 举报

发表于 2011-8-15 10:43 | 显示全部楼层
回复 yjwdjfqb 的帖子

晕倒,让我提供附件?
回复

使用道具 举报

 楼主| 发表于 2011-8-15 10:48 | 显示全部楼层
回复 那么的帅 的帖子

哦,搞错了,应该是我上传个附件给帅哥,帮忙解决下才对,,

不好意思帅哥。。

请看下这个附件问题在那儿。。
谢谢!!!

查找.rar

7.24 KB, 下载次数: 112

回复

使用道具 举报

发表于 2011-8-15 11:00 | 显示全部楼层    本楼为最佳答案   
回复 yjwdjfqb 的帖子

  1. Public Sub 查找数据()
  2.     Dim myRange1 As Range
  3.     Dim myRange2 As Range
  4.     Dim myRow As Long
  5.     Dim str As String
  6.     On Error Resume Next
  7.    
  8.     str = InputBox("请输入查找内容:")
  9.     If str = "" Then End
  10.     Set myRange1 = ActiveSheet.Range("B4:J500")       '指定查询范围
  11.     With myRange1
  12.         Set c = .Find(str, LookIn:=xlValues)
  13.         If Not c Is Nothing Then
  14.             firstAddress = c.Address(0, 0)
  15.             Do
  16.                 Set c = .FindNext(c)
  17.             Loop While Not c Is Nothing And c.Address(0, 0) <> firstAddress
  18.         End If
  19.     End With
  20.     If c Is Nothing Then
  21.         MsgBox "没有找到符合条件的单元格"
  22.     Else
  23.         MsgBox "符合条件的单元格为:" & firstAddress
  24.         Range(firstAddress).EntireRow.Select
  25.     End If
  26. End Sub
复制代码

回复

使用道具 举报

 楼主| 发表于 2011-8-15 14:57 | 显示全部楼层
回复 那么的帅 的帖子

谢谢帅哥,问题解决了!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-25 07:40 , Processed in 0.144352 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表