vba - 尝试两次运行工作表更改事件

标签 vba excel worksheet intersect

我正在尝试为两个不同的列 (A) 和 (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 和 I 的逻辑。

最佳答案

只需扩展您设置为 A 变量的范围。

Set A = Range("A:A, I:I")

改写为,
Private Sub Worksheet_Change(ByVal Target As Range)
    if not intersect(range("A:A, I:I"), target) is nothing then
        'add error control
        on error goto safe_exit
        'don't do anything until you know something has to be done
        dim r as range
        Application.EnableEvents = False
        For Each r In intersect(range("A:A, I:I"), target)
            r.Offset(0, 1).Value = Date   'do you want Date or Now?
        Next r
    end if
safe_exit:
    Application.EnableEvents = True 
End Sub

关于vba - 尝试两次运行工作表更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48898113/

相关文章:

excel - 使用 VBA 和 ActiveX 减少 WithEvent 声明和 subs

python - 使用 Python 对工作表中的列进行排序

vba - 宏失败, 'The macro may not be available in this...'

VBA Excel根据列中的值删除行

vba - 在调试 VBA 时运行循环

vba - Excel UDF 筛选范围

Excel - 日期范围包括一天中的给定时间(更好的方法)

c# - 将数据集放入工作表的最快方法

excel - 在 VBA 中将变量从窗体传递到模块

VBA:从特定格式的字符串中读取日期