+ Reply to Thread
Results 1 to 9 of 9

Setting CheckBox value based on state of column (hidden/unhidden)

  1. #1
    Registered User
    Join Date
    01-13-2010
    Location
    toronto
    MS-Off Ver
    Excel 2003
    Posts
    4

    Setting CheckBox value based on state of column (hidden/unhidden)

    Hi all,

    I am very new to macro programming in Excel (I am using 2000 by the way), and am having some difficulty with check boxes.

    So far, I have created a user form with a series of check boxes corresponding to each of the columns in my spreadsheet. When any of these checkboxes are clicked, their associated column becomes unhidden (and becomes hidden when unchecked).

    However, a small problem arises when the user form is initiated. Whenever the userform is initiated, all of the check boxes appear blank (i.e. false). I would like it so that when the userform is initiated the check boxes reflect which columns are currently hidden/unhidden.

    I am not quite sure how to go about this, and would appreciate some assistance.

    Thanks in advance.

    -Jason

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Setting CheckBox value based on state of column (hidden/unhidden)

    Hello Jason,

    Welcome to the Forum!


    You should post your workbook. It will be much easier to answer your questions and provide solutions.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-13-2010
    Location
    toronto
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Setting CheckBox value based on state of column (hidden/unhidden)

    Unfortunately, I currently do not have access to my workbook. Instead, I have made a simple example workbook demonstrating my problem. Please see attached file:

    http://cid-2d13d85b93b2a504.skydrive...ic/Example.xls

    So when you click on the button I have added on Sheet1, the userform with the checkboxes should appear. However, these checkboxes always appear blank. I would like it so that when a user clicks on the button to activate the userform, the values of the checkboxes accurately reflect which columns are currently hidden/unhidden.

    For example, if all columns are unhidden, I would like all checkboxes to be checked as soon as the userform is started.

    Another example is if just the first entry ("Last Name") is hidden, then when the userform is started, all checkboxes except for the one corresponding to "Last Name" should be checked already.

    How can I modify my code to implement such a feature?

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

    Re: Setting CheckBox value based on state of column (hidden/unhidden)

    Something like this might work.
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    01-13-2010
    Location
    toronto
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Setting CheckBox value based on state of column (hidden/unhidden)

    This is exactly what I was looking for!

    I was thinking of doing something like this (i.e. looping through the columns and assigning the checkbox value based on that), but was unsure of the syntax of how to do it.

    Thanks for your help.

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

    Re: Setting CheckBox value based on state of column (hidden/unhidden)

    It might not be worth the effort with only 5 columns, but a custom Class of checkboxes would be another way to approach this, rather than seperate, identical event code for each checkbox.

  7. #7
    Registered User
    Join Date
    01-13-2010
    Location
    toronto
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Setting CheckBox value based on state of column (hidden/unhidden)

    Well the initial code that you provided works fine with my workbook (which has 20+ columns by the way), and I think I'll leave it as is.

    But out of curiosity, could you elaborate on what you mean by a "custom class" for check boxes? That is, what would be the purpose of such a class, and how would it fit in with the current code structure that I have?

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

    Re: Setting CheckBox value based on state of column (hidden/unhidden)

    By creating a custom Class, one can make a custom Object and manipulate it like the native VBA objects.

    This code will create a custom object, called a clsCheckColumn.
    clsCheckColumn objects have three properties: .ChkBox (a check box), .Column (a range) and .HideWhenValueIs (a boolean value)

    When the user clicks on a checkbox that is the .ChkBox property of a clsCheckColumn, the .Column of that clsCheckColumn will be hidden or unhidden depending on the value of the check box and the value of its .HideWhenValueIs property.

    To create this class, I inserted a class module and used the Property Window to give that module the name clsCheckColumn. And I put this code in the class module.

    Please Login or Register  to view this content.
    The three variables declared at the top, ChekBox, myCol and ShowWhenValueIs are paired with the three properties ChkBox, Column and HideWhenValueIs.

    The "WithEvents" in the declaration of ChekBox is what connects the clicking of the ChekBox to the Sub ChekBox_Click() routine, which hides or unhides myCol.

    The three properties, ChkBox, Column and HideWhenValueIs are controled by the proprerty Get Let/Set routines (Let if the properety is a data type (e.g. Boolean, Integer,...), Set if it's an object (e.g. checkbox, range))

    The property Let/Set routines call the sub SyncFromSheet to set the value of the ChekBox to the proper value. i.e. make the checkbox value match the hidden/unhidden status of the column.

    The class is used by putting this in the Userform's code module
    Please Login or Register  to view this content.
    The LinkedBoxes collection (which persists as long as the userform is loaded) will hold the clsCheckColumn objects.

    The meat of the Initialize event is the loop that
    1) creates a new instance of clsCheckColumn, with the variable LinkedBox.
    2) assigns the properties of that new clsCheckColumn, i.e. associates which check box with which column
    3) puts it into the LinkedBoxes collection
    4) clears the variable LinkedBox for the next loop

    Once a checkbox/column has been paired by being properties of the same clsCheckColumn, nothing more needs to be done. By being properties of that class, when a checkbox is clicked, its column will be hidden/unhidden.

    Using a custom class like this does not prevent you from having seperate, normal style event prodeedures for the checkboxes used as properties of the custom class.

    The machinery for the Property statements looks 1) trivial 2)long, but it does allow the class to "intervene" in the assignment of properties.
    For example, the Column property takes the argument and assigns myCol to the column defined by the upper-left cell of the property, preventing a multi-column myCol (this can be changed if you want.)
    It also calls the SyncWithSheet routine everytime that a property is changed, insuring that the checkbox matches the proper value for the hiddenness of the column.

    I could have used this as the code in the class clsCheckColumn, avoiding all the Property routines:
    Please Login or Register  to view this content.
    But that would have required an intialize loop like
    Please Login or Register  to view this content.
    Note the explicit call of .SyncFromSheet (and the making it public in the class module). Also, if the value of a clsCheckColumn is changed in the middle its life, the checkbox might not have the proper value. SyncFromSheet will have to be explicitly called. I prefer to not to have to handle internal matters (like keeping things in sync) from the outside of a custom class.

    In either formulation, it is easier to write (and alter) one event routine in a class module than the 20 event routines in a userform's module that you are looking at.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    10-13-2011
    Location
    Grand Rapids, Michigan USA
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Setting CheckBox value based on state of column (hidden/unhidden)

    Absolutely wonderful code.Miker. You are an inspiration.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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