vba 特定列的 worksheet_SelectionChange

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26763407/
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-12 05:07:59  来源:igfitidea点击:

worksheet_SelectionChange for a specific column

excel-vbaeventsvbaexcel

提问by Jason

I have a worksheet that contains invoice numbers in column D. I'd like to copy the invoice number to another worksheet ("Details") when one is selected. I've added the "If IsNumeric" condition to make sure that only cells containing an invoice # will be copied over. The code seems to do nothing, can anyone help point me in the right direction?

I have a worksheet that contains invoice numbers in column D. I'd like to copy the invoice number to another worksheet ("Details") when one is selected. 我添加了“If IsNumeric”条件以确保只复制包含发票 # 的单元格。代码似乎什么也没做,有人能帮我指出正确的方向吗?

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Column = 4 Then On Error Resume Next Application.EnableEvents = False If IsNumeric(Target.Value) Then Sheets("Detail").Range("A5").Value = Target.Value Application.EnableEvents = True Sheets("Detail").Activate End If End If End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Column = 4 Then On Error Resume Next Application.EnableEvents = False If IsNumeric(Target.Value) Then Sheets("Detail").Range("A5").Value = Target.Value Application.EnableEvents = True Sheets("Detail").Activate End If End If End Sub

采纳答案by Gary's Student

Rather than:

而不是:

IsNumeric(Target.Address)

try

IsNumeric(Target)
IsNumeric(Target.Address)

尝试

IsNumeric(Target)

EDIT#1:

编辑#1:

Try this:

尝试这个:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 4 Then
        Application.EnableEvents = False
        If IsNumeric(Target.Value) Then
            Target.Copy Sheets("Detail").Range("A5")
        End If
        Application.EnableEvents = True
    End If
End Sub

EDIT#2:

编辑#2:

You need to re-enable events within the same IF

您需要在同一IF 中重新启用事件

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 4 Then
        On Error Resume Next
            If IsNumeric(Target.Value) Then
                Application.EnableEvents = False
                Sheets("Detail").Range("A5").Value = Target.Value
                Sheets("Detail").Activate
                Application.EnableEvents = True
             End If
    End If
End Sub