+ Reply to Thread
Results 1 to 6 of 6

Why is this happening? Pressing Delete extends range for code?

  1. #1
    Registered User
    Join Date
    05-02-2018
    Location
    CT, USA
    MS-Off Ver
    2007/10
    Posts
    23

    Why is this happening? Pressing Delete extends range for code?

    Hi all, I don't think this is a major functionality issue, but I am very curious as to why this is happening...

    I have a simple code that works perfectly:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

    Set KeyCells = Range("A:A")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
    Is Nothing Then

    Range(Target.Address).Offset(0, 1) = Application.UserName

    End If
    End Sub

    However, by pure chance, I noticed that if I highlight say A1:D5 and hit Delete (only delete does this) it pasts the name in every cell in B2:F5

    why on earth does it do that? if i do the same thing but hit any other key it doesn't do that, but Delete does this weird thing?

    Any insight is appreciated. Thanks

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,750

    Re: Why is this happening? Pressing Delete extends range for code?

    Because you are selecting and deleting all those cells, that is the Target, hence the Target.Offset(0, 1) becomes the area to post the User Name.

    Note 1. You don't need to say Range(Target.Address); just use Target ... it is a range

    Note 2. You are changing cells within a Change Event handler. You should use Application.EnableEvents ... = False before the change and = True after the change.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,750

    Re: Why is this happening? Pressing Delete extends range for code?

    Oh, and ...Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional.)

  4. #4
    Registered User
    Join Date
    05-02-2018
    Location
    CT, USA
    MS-Off Ver
    2007/10
    Posts
    23

    Re: Why is this happening? Pressing Delete extends range for code?

    I am so sorry for my Delay - I have cycles were I get to try and teach my self stuff and cycles where I barely get to sleep.
    I apologize for not putting the code tags, as you noticed I am very new. I will be sure to put them- however, for the life of me I can’t find the Edit button! Where is it hidden?

    Thank you for the explanation as to why it was doing that – that makes total sense now!
    (And I will look into the Application.EnableEvents ... = False thing to understand how that works )

    Edit: I see the Edit button for this message - but not the first one.
    -i hope this doesn't count as a new request in an existing thread
    Last edited by adamtzvi; 03-11-2020 at 10:03 AM. Reason: new info / clarification

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,750

    Re: Why is this happening? Pressing Delete extends range for code?

    Maybe try like this:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-02-2018
    Location
    CT, USA
    MS-Off Ver
    2007/10
    Posts
    23

    Re: Why is this happening? Pressing Delete extends range for code?

    That makes sense! Thank you.
    It seems a lot of VBA and probably programming in general is about how you think about issues. i'm starting to see that more now/starting to find myself thinking a differently

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. edit in code for populate range of cell in list box and code for delete any items
    By mazan2010 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2017, 03:33 PM
  2. [SOLVED] Command Button Macro that Extends a Range of Formulas
    By unit285 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2015, 02:15 PM
  3. [SOLVED] Run Time Error 9 Subscript Out of Range - Happening to only 1 user?
    By excelnat in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-03-2015, 10:21 AM
  4. VBA code to delete the FILTERED range and moving the cell up (dont Delete entire row)
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2015, 07:48 AM
  5. Code look through range of cells, find blanks & delete cell range in the same row,
    By EagleInsight in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-10-2014, 11:24 AM
  6. [SOLVED] Conditional formatting that extends over a range of data
    By Mountain99 in forum Excel Formulas & Functions
    Replies: 23
    Last Post: 08-21-2013, 10:20 AM
  7. code showing user what's happening
    By mdavid800 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2011, 11:56 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1