Trying to run a worksheet change event twice

前端 未结 2 340
甜味超标
甜味超标 2021-01-22 15:39

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 R         


        
2条回答
  •  猫巷女王i
    2021-01-22 16:11

    edited after OP's comment

    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
    

提交回复
热议问题