简体   繁体   English

工作表上的工作簿宏

[英]Workbook Macro on Worksheet

I have the following code 我有以下代码

Private Sub Worksheet_Change(ByVal Target As Range)
    If InRange(ActiveSheet.ActiveCell, Range("M4:M1048576")) Then
        MsgBox "ESTA EN EL RANGO DE M"
        If (Not IsEmpty(ActiveCell.Offset(0, -1))) And (ActiveCell.Offset(0, -1).Value > 0) Then
            Application.EnableEvents = False
            If InStr(1, ActiveCell.Text, "EFECTIVO") > 0 Then
                Call RestaEfectivo
            ElseIf InStr(1, ActiveCell.Text, "BAC Débito") > 0 Then
                Call RestaBAC
            ElseIf InStr(1, ActiveCell.Text, "CITI Débito") > 0 Then
                Call RestaCITI
            ElseIf InStr(1, ActiveCell.Text, "BAC Crédito") > 0 Then
                Call IncrementarCredito
            End If
            Application.EnableEvents = True
        End If
    End If
End Sub

which was set to a specific worksheet. 设置为特定的工作表。 However, I decided to ut it out from the worksheet and place it on ThisWorkbook. 但是,我决定将其从工作表中删除并将其放置在ThisWorkbook上。 I'm very new to VBA so I have no idea what I need to change in the code so it executes on every worksheet, right now, the code won't execute at all. 我是VBA的新手,所以我不知道我需要在代码中进行什么更改,因此它可以在每个工作表上执行,现在,代码根本不会执行。

Could anyone tell me what I need to change in my code so that it stays on ThisWorkbook and it executes on every sheet? 谁能告诉我我需要在代码中进行哪些更改,以使其保留在ThisWorkbook上并在每个工作表上执行?

the event listener sub is not the same name in ThisWorkbook module. 事件侦听器子在ThisWorkbook模块中的名称不同。

just move the body of the code into this event listener in ThisWorkbook module: 只需将代码主体移至ThisWorkbook模块中的此事件侦听器中即可:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    'Place the code here
End Sub

it will work. 它会工作。

cheers~ 干杯〜


you can see what event listener are available using the drop down menu 您可以使用下拉菜单查看可用的事件监听器

在此处输入图片说明

pick one and VBA will automatically create the sub for you. 选择一个,VBA将自动为您创建子。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM