Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy-paste doesn't trigger worksheet_change

Tags:

macos

excel

vba

Here is what I need to do: When I have written something into a cell in the sheet, my Worksheet_Change code should check if the cell contains certain characters and then replace those characters. That part of the code is working fine.

However there is a slightly odd behavior. Here's the code so far:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Target.Value = Replace(Target.Value, "ß", "ß")

        MsgBox "This is the value: " & Target.Value
    End Sub

What's happening is that when I have the characters that need to be changed in my clipboard (using Ctrl+C). When I double-click onto the cell, paste the characters into the cell using Ctrl+V, and then press Enter, the code works just fine, the characters are changed. (Note: Without the double-click, you can't see the cursor.)

However, if I just go to the cell with my arrow keys and paste over whatever else is in the cell, nothing happens. I suspect the Worksheet_Change isn't even triggered, or else it would at least display my MsgBox.

(I don't know if it's relevant to this, but i am using Excel 2010 in a Mac)

like image 226
flamingo Avatar asked Apr 16 '13 23:04

flamingo


People also ask

When can a Worksheet_Change be triggered?

Occurs when cells on the worksheet are changed by the user or by an external link.

What is Worksheet_Change?

In Excel a Worksheet Change Event is a trigger for a macro when a cell or group of cells change. I will start out by showing how a change to a single cell can trigger an action. The following will colour cell B2 Red whenever the cell changes.

What is Worksheet_Change ByVal target as range?

Private Sub Worksheet_Change(ByVal Target As Range) Target is passed as an argument when the event fires. It is the Range that changed and caused the event to fire. You can use it to run your code only when certain cells change.

Can you have two Worksheet_Change?

You cannot have more than one Worksheet_Change event procedure in the same worksheet module.


2 Answers

You can use Worksheet_Calculate on a trigger sheet.

  • Create a new sheet which only contains a link to the range you want to watch (i.e. =Sheet1!A1:B3)
  • Add a macro called Worksheet_Calculate to your trigger sheet
  • Now whenever any date in the linked range changes a recalculation of your trigger sheet gets triggered which in turn sets off your macro

This also works when the user enters a formula in the corresponding cell and one of the predecessors changes.

With manual calculation the recalculation of the trigger sheet and the macro only happen when the user presses F9.

like image 150
unique2 Avatar answered Sep 30 '22 12:09

unique2


Always use EnableEvents property when working with Worksheet_Change event. For more details link

  Private Sub Worksheet_Change(ByVal Target As Range)

 On Error GoTo err_rout


    Application.EnableEvents = False

    If Target.Count > 1 Then Exit Sub

    Target.Value = Replace(Target.Value, "ß", "ß")

    MsgBox "This is the value: " & Target.Value

err_rout:
    Application.EnableEvents = True
End Sub

enter image description here

like image 39
Santosh Avatar answered Sep 30 '22 11:09

Santosh