+ Reply to Thread
Results 1 to 14 of 14

Changing cell properties with double click, then revert with another double click

  1. #1
    Registered User
    Join Date
    02-19-2014
    Location
    Atlanta, Georgia
    MS-Off Ver
    Excel 2013
    Posts
    9

    Changing cell properties with double click, then revert with another double click

    Good evening,

    I have wrestled with this for a week now and can't get anywhere. I have a sheet (attached below) that I am trying to write in VBA the ability to double click a cell and turn it a color, then double click again and remove the color (reverts to no fill). I want to be able to count the number of green cells, red cells, etc. Additionally, I would like to be able to restrict the user to only selecting one choice from each row (ex. row 12 can only double click "C" - turning it green - and if they double click "I" afterwards, an error message pops up and they are unable to select the second cell).

    Can anyone help me get on the right track? I initially went with checkboxes in each individual cell, linked to hidden cells so that I could count the "True" and "False" counts. This checkbox method is unwieldy, and there has to be an easier way. In the sheet attached below, the cells in question are I12:L21.

    Thanks so much in advance for any help!

    Matt

    SampleBook.xlsx

  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,425

    Re: Changing cell properties with double click, then revert with another double click

    I can't do it now, too late. But, as an idea, the double click event has a target parameter. So, when a cell is double clicked, you can check and store its address in a helper column. If no address is found, you set the colour and store the address. If an address is found and it is the same as the target address, you clear the colour and clear the stored address. If an address is found and it's not the same as the target address, you display a stop message.

    Regards, TMS
    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
    Registered User
    Join Date
    02-19-2014
    Location
    Atlanta, Georgia
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: Changing cell properties with double click, then revert with another double click

    I am a little more novice I think haha. I tried to record a macro where I simply doubleclicked a cell to see how it recorded it, but it only noted that I selected the cell...it didn't necessarily record the double click. I'm confused about the target parameter you mention. I'm assuming this parameter is the color (or non-color) of the cell, but how do I incorporate that into the code?

    Thanks for your response!

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Changing cell properties with double click, then revert with another double click

    Hi Matt and welcome to the forum,

    Trevor is talking about Event Code that is behind a worksheet instead of in a Module. When you double click on a cell this act is serviced by code that is behind the worksheet itself. When you record a macro the code ends up in a Module which isn't behind the worksheet. Read this site for more..

    http://www.cpearson.com/excel/Events.aspx

    Find the attached where I've throw some code together to do some of what you want. If you double click on a cell it will turn green or white. If it is white it will check the other cells to insure they aren't already colored and give an error message if any are.

    Your job is to read my code and make it better. GoTo statements are not good. Add other features to make the code better for you.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Registered User
    Join Date
    02-19-2014
    Location
    Atlanta, Georgia
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: Changing cell properties with double click, then revert with another double click

    Thanks guys.I have made a lot of progress tonight with your help.

    Marvin, I borrowed some of your code to set up the error message (I know what you mean about GoTo not being the best option, but I would like to get it working before I start streamlining). In the attached document, you can see where I am having trouble. I have 4 separate programs set up side by side (this side by side setup is non-negotiable). For example, on line 12 I can have one option (I,F,P,or C) selected for each of the four programs. Each of the programs is treated separately. However, if there are multiple cells doubleclicked per program, I would like to show the error message.

    If Range("I12:L12").Interior.Color <> vbWhite Or _
    Range("I12:L12").Interior.Color <> vbWhite Or _
    Range("I12:L12").Interior.Color <> vbWhite Or _
    Range("I12:L12").Interior.Color <> vbWhite Then
    MsgBox "You can't have more than one entry per line.", vbOKOnly + vbCritical, "Error"

    GoTo Ender
    End If

    As you can see in the code, I named the range that should have been affected ("I12:L12"). The error message works fine for the first program (Found from I12:L21), but when I go to doubleclick any options in the other programs, I get the error message. Why is this happening? If I named the range that encompasses the first program and nothing else, why does the error message show up when I move to other parts of the sheet?

    Thanks again for all of your help! You guys are the best
    Attached Files Attached Files

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Changing cell properties with double click, then revert with another double click

    You got rid of my
    If Not Intersect(.... ) is Nothing Then
    line of code.

    This tells vba to ignore double clicks outside the range in the Intersect.

    I think you will need to have separate If Not Intersect for each of your "programs", as you call them.
    You will need a section for I to L, Q to T etc..

    Read some here http://www.ozgrid.com/VBA/vba-intersect.htm
    and http://2toria.com/2010/07/10/using-i...t-in-vba-code/

  7. #7
    Registered User
    Join Date
    02-19-2014
    Location
    Atlanta, Georgia
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: Changing cell properties with double click, then revert with another double click

    Thanks Marvin! I will read through those and come back if I still am having problems.

  8. #8
    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,425

    Re: Changing cell properties with double click, then revert with another double click

    This was the method proposed using column F as the helper. This can be adjusted as required by changing the constant value. The column can be hidden.

    Please Login or Register  to view this content.

    Regards, TMS

  9. #9
    Registered User
    Join Date
    02-19-2014
    Location
    Atlanta, Georgia
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: Changing cell properties with double click, then revert with another double click

    So everything went well until I coded the whole sheet and i got a compile error because "Procedure Too Large". I had to do the intersect for each row of 4 cells that I am trying to only allow 1 entry for. So basically 48 of the below back to back. (I have the "Ender" at the bottom of all of the code). Everything works exactly as it should, it is just too much for VBA to handle.

    To reiterate, I have four ranges:
    -I12:L21
    -Q12:T21
    -Y12:AB21
    -AG12:AJ21

    But each line (i.e. I12:L12, or Q12:T12) can only have one selection. I had trouble using the intersect when I name the large range (ex - I12:L21) so I went with using a separate intersect for each 4 cell row (ex - I12:L12) as seen below.

    I am sure that I made it much more complicated than it should be, but I am new to VBA and it was working like I wanted, it just became too large. Any advice on trimming it or making it more efficient so that it will fit?


    Please Login or Register  to view this content.

  10. #10
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Changing cell properties with double click, then revert with another double click

    I've never seen this error message. Perhaps it is a 32 vs 64 OS problem? Read:
    http://support.microsoft.com/kb/983044

  11. #11
    Registered User
    Join Date
    02-19-2014
    Location
    Atlanta, Georgia
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: Changing cell properties with double click, then revert with another double click

    I'm not sure how that could happen. I have 32 bit and it was all written in my program.

    Do you have any tips to use less code to achieve my goal? The code runs fine up until about 42 iterations of the code I pasted above, I'm guessing the last 6 iterations push it over some kind of limit. Shortening the code should fix it, in that case.

  12. #12
    Registered User
    Join Date
    02-19-2014
    Location
    Atlanta, Georgia
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: Changing cell properties with double click, then revert with another double click

    Are there parts of my code that I could transfer to public sub and then call when I need them? So basically 48 different public subs, one for each 4 cell row that I am dealing with? I tried to do it by copying everything from the above code from (If Not Intersect...) down to the last End If, moving it to a new sub, naming it something like change1, and then calling change1 when it is required. I then get "Argument Not Optional" error.

    I know I'm in over my head and taking time to learn everything from square one would be the best, but I am in a severe time crunch and am really struggling. Thanks for any help in advance to shorten the code and avoid the "Procedure Too Large" error.

  13. #13
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Changing cell properties with double click, then revert with another double click

    I'm wondering if it has to do with the "Cancel" variable in the double-click call.

    How about putting a "Cancel = True" at the end of you code, like I did in my example.

    http://www.excel-easy.com/vba/exampl...ick-event.html
    http://stackoverflow.com/questions/8...-handled-event

    Programming is a mystery most of the time. We are trying to learn the language of the computer and have to try and fail a few times before we understand what it thinks we are trying to tell it to do. Keep trying different things and this VBA language will make more sense with each failure.

  14. #14
    Registered User
    Join Date
    02-19-2014
    Location
    Atlanta, Georgia
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: Changing cell properties with double click, then revert with another double click

    I do have a cancel = True at the very bottom of the code. Basically I have 48 versions of what I posted above, and underneath all of those I have

    Ender:

    Cancel = True

    End Sub


    I guess another possibility would be to have multiple BeforeDoubleClick events? I tried this also but when I try to copy the block of code and paste it below, using all of the same logic but simply changing the title of the sub to "BeforeDoubleClick1" it doesn't seem to work.

+ 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. Copy double-clicked cell to next empty row in different sheet with each double-click
    By erhathaway in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-25-2013, 12:06 AM
  2. [SOLVED] Double Click in Cell
    By VonEpp in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 04-04-2012, 01:37 PM
  3. Double-Click on cell
    By edwardpestian in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2006, 10:23 PM
  4. userform label double-click goes to click event
    By John Paul Fullerton in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-19-2006, 01:00 PM
  5. Replies: 4
    Last Post: 08-02-2005, 09:05 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