自动填充时出现错误 80010108

发布于 2024-10-27 19:32:48 字数 555 浏览 1 评论 0原文

我正在编写代码,但收到 -2147417848 (80010108) 错误。我知道这是每次它“得到”空单元格时造成的,但我不知道如何修复它,有人可以帮助我吗?代码如下:

Dim i, g As Integer

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    g = 12

    For i = 3 To 5000

        If Worksheets("Registo_EPI´s").Cells(i, 1).Value = Cells(4, 20).Value Then
            Cells(g, 21).Value = Worksheets("Registo_EPI´s").Cells(i, 5).Value
            g = g + 1
        End If

    Next i

End Sub

每当 Cells(i,5) 范围内的单元格为空白时,代码就会崩溃。例如,如果在单元格(3,5)中我有一个数字或日期,如果在单元格(4,5)中我没有任何东西,它就会崩溃。希望你们能帮助我。谢谢

I´m writting a code but i´m getting an -2147417848 (80010108) error. I know that is caused everytime it "gets" a empty cell but i don´t know how to fix it, can someone help me please? Here´s the code

Dim i, g As Integer

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    g = 12

    For i = 3 To 5000

        If Worksheets("Registo_EPI´s").Cells(i, 1).Value = Cells(4, 20).Value Then
            Cells(g, 21).Value = Worksheets("Registo_EPI´s").Cells(i, 5).Value
            g = g + 1
        End If

    Next i

End Sub

Whenever a cell within the range Cells(i,5) is blank, the code crash. Por example if in cell(3,5) i have a number or date and if in cell(4,5) i don´t have anything, its crashes. Hope ou guys can help me. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2024-11-03 19:32:48

我认为您的 Worksheet_Change 事件处理程序很可能无意中调用自身,这可能是问题的根源。

例如,如果显示的 Worksheet_Change 事件处理程序适用于 Sheet1(因此像 Cells(4, 20).Value 这样的非限定引用也引用Sheet1) 那么当您更改 Cells(g, 21) 的值时,Sheet1Worksheet_Change 事件处理程序为又打来电话等等 在。

您应该使用事件处理程序的 Target 参数来防止这种情况。例如,如果您只对 Cells(4, 20) 的更改感兴趣,那么您可以:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If (Target.Address(ReferenceStyle:=xlR1C1) = "R4C20") Then
        g = 12

        For i = 3 To 5000

            If Worksheets("Registo_EPI´s").Cells(i, 1).Value = Cells(4, 20).Value Then
                Cells(g, 21).Value = Worksheets("Registo_EPI´s").Cells(i, 5).Value
                g = g + 1
            End If

        Next i
    End If
End Sub

I think it's quite likely that your Worksheet_Change event handler is unintentionally calling itself and that this may be the source of your problem.

For example, if the Worksheet_Change event handler shown applies to Sheet1 (and thus unqualified references like Cells(4, 20).Value also refer to Sheet1) then when you change the value of Cells(g, 21) the Worksheet_Change event handler for Sheet1is called again and so on.

You should use the Target parameter of the event handler to guard against this. If, for example, you were only interested in changes to Cells(4, 20) then you could have:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If (Target.Address(ReferenceStyle:=xlR1C1) = "R4C20") Then
        g = 12

        For i = 3 To 5000

            If Worksheets("Registo_EPI´s").Cells(i, 1).Value = Cells(4, 20).Value Then
                Cells(g, 21).Value = Worksheets("Registo_EPI´s").Cells(i, 5).Value
                g = g + 1
            End If

        Next i
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文