Solved

Modify Macro to Copy Target.Value on Delete

Posted on 2014-09-18
3
184 Views
Last Modified: 2014-09-18
Hi Experts,

I have attached a macro that needs a small modification, within the StandardMode and ManualMode sheets the below code copies and pastes cells M2-M4 across both sheets.
Private Sub Worksheet_Change(ByVal Target As Range)
    If CascadeSelections Then Exit Sub
    Select Case Target.Address
        Case "$M$2"
            CascadeSelections = True
            Sheets("ManualMode").Range("M2").Value = Target.Value
        Case "$M$3"
            CascadeSelections = True
            Sheets("ManualMode").Range("M3").Value = Target.Value
        Case "$M$4"
            CascadeSelections = True
            Sheets("ManualMode").Range("M4").Value = Target.Value
    End Select
    CascadeSelections = False
End Sub

Open in new window

However, the macro does not execute when an user presses the "delete" key on cell m2,m3,or m4 in either of the given sheets. Is it possible to make it delete inputs across both sheets if a user presses the delete key?
EE-Safe-Copy-Macro.xlsm
0
Comment
Question by:-Polak
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40331117
The reason the Worksheet_Change event isn't capturing the [Delete] action is because the Target.Address for that spans four columns and isn't being considered in the Select Case section.  For example, if you delete the Region value, the Target.Address is "$M$2:$P$2".

So, try this instead:


Private Sub Worksheet_Change(ByVal Target As Range)
    If CascadeSelections Then Exit Sub
    Select Case Left(Target.Address, 4)
        Case "$M$2"
            CascadeSelections = True
            Sheets("StandardMode").Range("M2").Value = Target.Value
        Case "$M$3"
            CascadeSelections = True
            Sheets("StandardMode").Range("M3").Value = Target.Value
        Case "$M$4"
            CascadeSelections = True
            Sheets("StandardMode").Range("M4").Value = Target.Value
    End Select
    CascadeSelections = False
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:-Polak
ID: 40331149
Oh..... Was wondering why I couldn't get that to work on my own, that makes sense (annoyingly). Thanks Glenn.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40331197
Indeed, that is annoying.  I would think the Target.Address would be the same regardless of the action or selection.  Another good reason to avoid merged cells, though! :-)

-Glenn
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question