+ Reply to Thread
Results 1 to 5 of 5

Help with checkbox

  1. #1
    Registered User
    Join Date
    01-12-2014
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    77

    Help with checkbox

    Hi,

    I've assigned the following code to a checkbox.
    -------------------------------
    Please Login or Register  to view this content.
    ------------------------------
    The code is working but when the i uncheck the checkbox, i want to undo the action done by macro. Can you please tell What changes should be made to the above code.

    Thank You.

    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 01-29-2014 at 10:26 PM. Reason: code tags

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,816

    Re: Help with checkbox

    It appears that you are using a Forms checkbox (instead of an ActiveX checkbox). My personal opinion is that the best way to do this is to link the checkbox to a cell (right-click on the checkbox, Control tab, fill in Cell Link), then check to see if that cell is True or False in this macro. Note that the same macro is called if you click on the checkbox, regardless of whether you are checking it or unchecking it. So the same macro has to do both jobs.

    But beyond that, undoing an action means that you have to save the prior value of this cell somewhere. You should identify a cell someplace to save the value before you change it. Also note that if cell D1 is changed anywhere but in this code, such as by the user, you are going to need a more complex solution than this.

    Let's say you link the checkbox to L1 (L for Link). And you use S1 to save the value (S for save).

    Please note that I have compiled but not tested this, so it is just an example to illustrate the concept. If we're both lucky it will work.

    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    01-12-2014
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    77

    Re: Help with checkbox

    Quote Originally Posted by 6StringJazzer View Post
    It appears that you are using a Forms checkbox (instead of an ActiveX checkbox). My personal opinion is that the best way to do this is to link the checkbox to a cell (right-click on the checkbox, Control tab, fill in Cell Link), then check to see if that cell is True or False in this macro. Note that the same macro is called if you click on the checkbox, regardless of whether you are checking it or unchecking it. So the same macro has to do both jobs.

    But beyond that, undoing an action means that you have to save the prior value of this cell somewhere. You should identify a cell someplace to save the value before you change it. Also note that if cell D1 is changed anywhere but in this code, such as by the user, you are going to need a more complex solution than this.

    Let's say you link the checkbox to L1 (L for Link). And you use S1 to save the value (S for save).

    Please note that I have compiled but not tested this, so it is just an example to illustrate the concept. If we're both lucky it will work.

    Please Login or Register  to view this content.
    Hi,

    Does that mean using Active X checkbox will solve the problem?

    Thank you

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,816

    Re: Help with checkbox

    First it is not necessary to quote an entire post if you are replying. It adds a lot of volume to the thread without adding clarity. If you need to emphasize something you are responding to, just quote the relevant portion.

    The problem can definitely be solved with Forms checkboxes, as I described. I would have done it for you if you had attached your file. But using ActiveX checkboxes makes many things easier, IMHO. VBA offers better support for them, although they use a slightly different concept. Instead of assigning a macro to them, you use macros with special names that are called automatically when the corresponding event occurs. For example

    Please Login or Register  to view this content.
    will be called if the user clicks on CheckBox1. You do not have to do anything to assign this macro to the checkbox, it is taken care of automatically by VBA. Then in the code you can have logic like
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-12-2014
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    77

    Re: Help with checkbox

    Thank you for informing me on how to post or reply. Going forward i will follow them.

    I've attached my excel file. can you please help on this.
    Attached Files Attached Files
    Last edited by laxminarayana; 01-31-2014 at 10:45 PM.

+ 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. Trying to scale a form checkbox in a macro, don't know how to get checkbox name
    By tstruch in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-09-2014, 12:14 AM
  2. [SOLVED] Checkbox to deselect another checkbox, shorter code
    By JamesT1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-27-2013, 09:59 AM
  3. Lock cell linked to checkbox when checkbox is ticked
    By simeony003 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2013, 10:08 AM
  4. Replies: 4
    Last Post: 07-22-2013, 01:37 PM
  5. [SOLVED] How do I change a checkbox to unchecked without running the code within the checkbox
    By jsunnb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-27-2012, 09:28 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