Solved

Modify Macro to Copy Target.Value on Delete

Posted on 2014-09-18
3
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

729 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