尝试两次运行工作表更改事件 [英] Trying to run a worksheet change event twice

查看:66
本文介绍了尝试两次运行工作表更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为两个不同的列(A)和(I)运行此工作表更改事件...

I am trying to run this worksheet change event for two different columns(A) and (I)...

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim A As Range, B As Range, Inte As Range, r As Range
        Set A = Range("A:A")
        Set Inte = Intersect(A, Target)
        If Inte Is Nothing Then Exit Sub
        Application.EnableEvents = False
            For Each r In Inte
                r.Offset(0, 1).Value = Date
            Next r
        Application.EnableEvents = True 
    End Sub

此活动是我在此论坛上发现的.其目的是使它成为可能,以便每当将数据输入到"a"列时,它都会自动将日期直接输入到它所在的单元格中.我希望这种情况在工作表上发生两次.我不知道如何更改/添加到它.我正在尝试使其在电子表格上运行A列和我的逻辑.

This event is something i found on this forum. Its purpose is to make it so whenever data is ever entered into column "a" it auto inputs the date into the cell directly right of it. I want this to happen twice on the worksheet. I can't figure out how to change/add to it. I am trying to get it to run the logic for column A and I on my spreadsheet.

推荐答案

在OP评论后进行了编辑

edited after OP's comment

扩展@Jeeped解决方案,可以避免循环:

expanding on @Jeeped solution, you can avoid looping:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range

    Set rng = Intersect(Range("A:A, I:I"), Target) ' define range of interest
    If Not rng Is Nothing Then ' check it's not "nothing"
        If WorksheetFunction.CountA(rng) = rng.Count Then 'check for all of its cells being not empty
            On Error GoTo safe_exit 'add error control
            Application.EnableEvents = False 'don't do anything until you know something has to be done
            rng.Offset(, 1).Value = Date 'write Date next to all relevant changed cells
        End If
    End If

safe_exit:
    Application.EnableEvents = True
End Sub

这篇关于尝试两次运行工作表更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆