Home > Enterprise >  How to run this function in excel using vba
How to run this function in excel using vba

Time:10-11

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = Range("B12").Address Then

        Application.EnableEvents = False

        Dim sOldValue As String, sNewValue As String
        sNewValue = Target.Value

        Application.Undo

        Dim rOld As Range
        Set rOld = Range("A1:E1").Value

        Target.Value = sNewValue

        Range("A15:E15").Value = rOld.Value

        Application.EnableEvents = True

    End If

End Sub

How to run this function , can you please call this function

CodePudding user response:

The code you posted is for the Worksheet.Change event. The event occurs when cells on the worksheet are changed by the user or by an external link.
All you need to run this sub is to place the code in the sheet module for the relevant sheet and change B12 cell.

CodePudding user response:

Create a button, add in the following code, you might need to change your code from Private to Public

`Private Sub CommandButton1_Click()

Call Worksheet_Change

End Sub`

  • Related