尝试.Find()时VBA Excel宏运行时错误“1004”

时间:2015-09-02 15:02:25

标签: excel vba excel-vba

我是宏的新手,想要从工作表中的范围中找到文本或部分文本,我研究并找到了这段代码:

Set aCell = .Columns(1).Find(What:="Custom ", LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)

但是当我跑步时,它给了我1004错误。所以这是我的完整分:

Sub kl()
    Dim ws As Worksheet
    Dim aCell As Range

    Set ws = ThisWorkbook.Sheets("te-dhenat")

    With ws
        Set aCell = .Columns(1).Find(What:="Custom ", LookIn:=xlValues, _
                    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
        If Not aCell Is Nothing Then
            aCell.Value = "Test"
        Else
            MsgBox "Not Found"
        End If
    End With 

End Sub

我认为代码看起来不错,所以我不知道为什么excel会显示此错误,请提供帮助,并提前感谢

1 个答案:

答案 0 :(得分:0)

你的代码

aCell.Value = "Test"

将导致事件代码再次执行!

请参阅this页面,查找“防止事件循环”部分。

如果有其他信息值得一读。

所以添加类似的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Target.Value = Target.Value + 1
    Application.EnableEvents = True
End Sub
相关问题