vba 粘贴值后工作表更改事件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11020472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:29:03  来源:igfitidea点击:

Worksheet Change event not working after pasting values

excel-vbavbaexcel

提问by user1049518

I have a code in Worksheet_Change

我有一个代码 Worksheet_Change

If a column 9 is updated then the column 8 will be automatically populated by multiplying with col 9 and col 11.

如果第 9 列被更新,则第 8 列将通过乘以第 9 列和第 11 列自动填充。

But when the user pastes the values in the column, the change event does not work correctly. Only the first cell in the Col 8 gets updated.

但是当用户将值粘贴到列中时,更改事件无法正常工作。只有第 8 列中的第一个单元格得到更新。

How can I overcome from this?

我该如何克服?

采纳答案by Siddharth Rout

Try this

尝试这个

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa
    Application.EnableEvents = False

    If Target.Columns.Count > 1 Then GoTo LetsContinue

    If Not Intersect(Target, Columns(9)) Is Nothing Then
        Dim aCell As Range

        For Each aCell In Target
            aCell.Offset(, -1).Value = aCell.Value * aCell.Offset(, 2)
        Next
    End If

LetsContinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub