+ Reply to Thread
Results 1 to 23 of 23

Clear cell contents if not the target cell

  1. #1
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Clear cell contents if not the target cell

    Hello,

    I have a code that references over 100 columns, and 9 rows deep. Basically, when a cell is double-clicked in one of these columns, the other row's cells in that column are erased. (A mutually exclusive 'selection').
    The only problem is I want to add hidden columns and move ranges around and I'm wondering if there is a faster way of re-writing the code than repeating the following 'cases' 100 times (see,somewhat inelegant). Any ideas are most welcome!

    Cheers!

    M

    Select Case Target.Address
    Case Is = "$E$5", "$E$6", "$E$7", "$E$8", "$E$9", "$E$10", "$E$11", "$E$12", "$E$13"
    'Clear Contents of cells that are not the target
    'Asset 1, scenario 1
    If Target.Address = "$E$5" Then [E6:E13].ClearContents
    If Target.Address = "$E$6" Then [E5,E7:E13].ClearContents
    If Target.Address = "$E$7" Then [E5:E6,E8:E13].ClearContents
    If Target.Address = "$E$8" Then [E5:E7,E9:E13].ClearContents
    If Target.Address = "$E$9" Then [E5:E8,E10:E13].ClearContents
    If Target.Address = "$E$10" Then [E5:E9,E11:E13].ClearContents
    If Target.Address = "$E$11" Then [E5:E10,E12:E13].ClearContents
    If Target.Address = "$E$12" Then [E5:E11,E13].ClearContents
    If Target.Address = "$E$13" Then [E5:E12].ClearContents

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    Are the 100+ columns consecutive? If so, what is the column range?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Clear cell contents if not the target cell

    One way would be
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    This macro in the worksheet code module should work for any column as long as you don't double click in a column that is not in your 100+ columns.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    Super, thanks Mumps1 and mikerikson. The columns are, unfortunately, discontinuous, my apologies, I should have mentioned this. Mikerikson's first response is likely going to be the way i go, as I'm afriad of overwriting formula linked values which separate the target columns. Although props mumps1, very clever solution!!

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    If the target columns are always the same, you could restrict the clearing of the cells to only those columns. I would need to know which columns.

  7. #7
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    I see... the pattern is three columns of formulas, followed by two columns relating to this code, starting in column D. There are other non-mutually exclusive double-click rows below the ones being discussed however, in those columns so the row range would still need to be restricted.

  8. #8
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    Full disclosure... in this lower column section with non-mutually exclusive marlett font 'checks', I also use about a bajillion lines of code like this (to help speed up user driven selection) across multiple rows in the same column. Not sure if there is a better way for this to happen either...

    Case Is = "$E$22"
    Range("$E$23:$E$37") = Target.Value
    Case Is = "$E$40"
    Range("$E$41:$E$56") = Target.Value

  9. #9
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    Would it be possible to attach a copy of your file including any macros you are currently using, de-sensitized if necessary? This would make it easier to see how your data is organized and test possible solutions.

  10. #10
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    Hi Mumps, yeah let me upload something...

  11. #11
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28
    Here is an example of the 'scenario dashboard.' The whole idea is to make this user friendly, but my code is kind of ugly. There will need to be about 50 of these assets built out so you can see how the string gets very long...

    Many thanks,
    m
    Attached Files Attached Files

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    I'm a little bit confused. The code that I suggested was a Worksheet_BeforeDoubleClick event while in your file, it seems to be in a Worksheet_Change event. Do you want the cells cleared on a cell change or on a double click? Please clarify.

  13. #13
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    Sorry for the confusion.
    I thought because I already have the worksheet_beforedoubleclick event in use (for the non-exclusive selection of cells and as it applies to all row ranges in the columns) I could specify a separate _change event just for the mutually exclusive cells. I'm actually happy with all the functionality as is demonstrated, but just not excited about the long code string over 100 columns of mutually exclusive change event and mutually non-exclusive before double click events.

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    Try the attached file. It should take care of all the appropriate columns.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    Thanks Mumps1!

  16. #16
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    My pleasure.

  17. #17
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    I suppose the only problem is that while the code is much tidier, it actually runs 3x slower than then explicit formulas originally used... Any ideas?

  18. #18
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    yeah. It's just too slow so I have changed it back.
    is there a way to protect the hidden outlined columns from selection? (They are included in the double click named range, I can't exclude them because otherwise here would be too many ranges selected under the same named range...)

  19. #19
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    I'm not sure what you mean by
    is there a way to protect the hidden outlined columns from selection?
    Can you explain in detail?

  20. #20
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    Sorry, to make matters more complex I had some hidden columns in between each intended rows cell that were hidden via 'outlining' (grouped columns) which I didn't wish to overwrite.
    I did figure out how to leave those ones alone with this:
    If target.HasFormula Then Exit Sub

    I am, however, considering getting rid of this nice user interface altogether, because as my model grows employing the doubleclick interface vba seems to get slower and slower...
    Sorry mumps, nothing wrong with your code, just my modeling!

  21. #21
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    So is everything OK now?

  22. #22
    Registered User
    Join Date
    07-28-2009
    Location
    Stavanger, Norway
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Clear cell contents if not the target cell

    Yes, thank you

  23. #23
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,055

    Re: Clear cell contents if not the target cell

    Good to hear.

+ 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. [SOLVED] Needing VBA code to clear contents of cell when the contents of another cell is cleared
    By jeh0714 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-15-2016, 03:49 PM
  2. Clear merged cell contents in comand button(clear all)
    By mohan_984 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-25-2015, 10:39 AM
  3. VBA to clear contents of the cell in a column of data in a cell starts with specific word.
    By winmaxservices1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2015, 08:25 AM
  4. [SOLVED] move down column and for every blank cell, clear contents of cell to the left
    By hopefulhart in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-01-2014, 11:23 AM
  5. Clear contents in one cell when contents of another cell are deleted
    By rlbush2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-22-2012, 10:29 PM
  6. Macro that will clear contents of cell based on format of text in adjacent cell
    By judasdac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-12-2012, 01:56 AM
  7. Target Offset and Clear Contents
    By happySpotter in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-11-2007, 12:00 PM

Tags for this Thread

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