将VBA更改为自动运行或在移动选项卡时运行

时间:2018-12-14 14:56:57

标签: excel vba excel-formula

我有下面的VBA,它仅在单元更新时运行。有一个公式可以更新单元格,但VBA不会将其作为更改。我如何使VBA自动运行,所以我不必在每个单元格内使用Tab键即可使VBA运行。最好是全自动的,但如果方便的话,可以更改选项卡等。

任何帮助将不胜感激。

Private Sub Worksheet_Change(ByVal Target As Range)    
     If Target.Column = 9 Or Target.Column = 13 Then 'Column I or column M
         If UCase(Target) = "YES" Then Target.Offset(0, -1).ClearContents
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

@DMO

在这里,经过测试,效果很好。而且有点快

Public Sub UpdatedEIghtAndTwelve()
    Dim arr As Variant
    arr = ThisWorkbook.ActiveSheet.UsedRange.Formula
    Dim i As Long, j As Long, z As Long

    j = UBound(arr, 1)
    z = UBound(arr, 2)

    For i = LBound(arr, 1) To UBound(arr, 1)
        If arr(i, 9) = "YES" Then
            arr(i, 8) = vbNullString
        End If
        If arr(i, 13) = "YES" Then
            arr(i, 12) = vbNullString
        End If
    Next i

    With ThisWorkbook.ActiveSheet
        .Range(.Cells(1, 1), .Cells(j, z)).Value2 = arr
    End With

End Sub

我忘了补充说,您需要将其放在要运行此表的工作表代码的后面

 Private Sub Worksheet_Activate()
     Call UpdatedEIghtAndTwelve
 End Sub