Excel精英培训网

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

[已解决]range.find 总是出错

[复制链接]
发表于 2015-10-19 14:49 | 显示全部楼层 |阅读模式
在第1行1列输入值后查询到第一列符合条件的相应的行
红色部分一直改一直出错

Private Sub Worksheet_Change(ByVal Target As Range)
Dim b As String
Dim c As Range
Dim d
If Target.Address(0, 0) = "A1" Then
'Range("A2:j21").Borders.LineStyle = xlNone
Target.Value = UCase(Target.Value)
b = Target.Value
Set c = Range(”A2:A20").Find(b, lookat:=xlWhole)
If Not c Is Nothing Then
d = c.Row
ActiveWindow.ScrollRow = d
'Range("A3:j3").BorderAround xlContinuous, xlThick, ColorIndex:=3
End If
End If
End Sub

最佳答案
2015-10-19 15:20
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim b As String
    Dim c As Range
    Dim d
    Application.EnableEvents = False
    If Target.Address(0, 0) = "A1" Then
        'Range("A2:j21").Borders.LineStyle = xlNone
        
        Target.Value = UCase(Target.Value)
        b = Target.Value
        Set c = Range("A2:A20").Find(b, lookat:=xlWhole)
        If Not c Is Nothing Then
            d = c.Row
            ActiveWindow.ScrollRow = d
            'Range("A3:j3").BorderAround xlContinuous, xlThick, ColorIndex:=3
        End If
    End If
    Application.EnableEvents = True
End Sub
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2015-10-19 14:56 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2015-10-19 15:05 | 显示全部楼层
金樽空对月 发表于 2015-10-19 14:56
是什么样的错误提示呢?

各种错误,一会range无效,一会find无效,一会应用程序或对象定义无效
回复

使用道具 举报

 楼主| 发表于 2015-10-19 15:14 | 显示全部楼层
find作用于range时失败
Private Sub Worksheet_Change(ByVal Target As Range)
Dim b As String
Dim c As Range
Dim d
Sheet2.Activate
If Target.Address(0, 0) = "A1" Then
'Range("A2:j21").Borders.LineStyle = xlNone
Target.Value = UCase(Target.Value)
b = Target.Value
Set c = Range("A2:A20").Find(b, lookat:=xlWhole)If Not c Is Nothing Then
d = c.Row
ActiveWindow.ScrollRow = d
'Range("A3:j3").BorderAround xlContinuous, xlThick, ColorIndex:=3
End If
End If
End Sub

回复

使用道具 举报

发表于 2015-10-19 15:17 | 显示全部楼层
Set c = Range("A2:A20").Find(b, lookat:=xlWhole)   
放到编辑器里显示,左边的引号错了。
回复

使用道具 举报

发表于 2015-10-19 15:20 | 显示全部楼层    本楼为最佳答案   
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim b As String
    Dim c As Range
    Dim d
    Application.EnableEvents = False
    If Target.Address(0, 0) = "A1" Then
        'Range("A2:j21").Borders.LineStyle = xlNone
        
        Target.Value = UCase(Target.Value)
        b = Target.Value
        Set c = Range("A2:A20").Find(b, lookat:=xlWhole)
        If Not c Is Nothing Then
            d = c.Row
            ActiveWindow.ScrollRow = d
            'Range("A3:j3").BorderAround xlContinuous, xlThick, ColorIndex:=3
        End If
    End If
    Application.EnableEvents = True
End Sub
回复

使用道具 举报

 楼主| 发表于 2015-10-19 15:29 | 显示全部楼层
本帖最后由 mosaic 于 2015-10-19 15:30 编辑
grf1973 发表于 2015-10-19 15:20
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim b As String
    Dim c As Range


哥,我要哭了,我捣鼓了好久就是不好,复制了你的代码立刻好了
可以告诉我关键是哪里的问题吗?不会是那个“吧,以前"对的时候也报错
回复

使用道具 举报

发表于 2015-10-19 15:51 | 显示全部楼层
你在Worksheet_Change里用Target.Value = UCase(Target.Value)
改变单元格,有可能会陷入死循环。用 Application.EnableEvents = False禁用事件就OK了。
回复

使用道具 举报

发表于 2015-10-19 15:51 | 显示全部楼层
看一下是不是符号格式不对。
12.jpg
回复

使用道具 举报

 楼主| 发表于 2015-10-19 16:17 | 显示全部楼层
grf1973 发表于 2015-10-19 15:51
你在Worksheet_Change里用Target.Value = UCase(Target.Value)
改变单元格,有可能会陷入死循环。用 Appli ...

了解,谢谢
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 21:51 , Processed in 0.355583 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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