+ Reply to Thread
Results 1 to 11 of 11

Hide form control checkboxes using For-Next

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2014
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    13

    Hide form control checkboxes using For-Next

    Hi all, Im working on a spreadsheet that hides rows based on a form control checkbox, and also hides form control checkboxes within those rows.
    Apparently there is a bug is MS2010 that wont allow this to be done using activeX checkboxes, so I'm structuring the spreadsheet to use form control checkboxes instead.
    The code works just fine, but Id rather see it done in an array. (items in red need to move into a for-next loop)
    This should be a breeze for most you, my vba skills are lacking these days.


    Private Sub CheckBoxTest_Click()

    If CheckBoxTest = True Then
    [10:71].EntireRow.Hidden = False 'Unhide All
    ActiveSheet.CheckBox1.Visible = True
    ActiveSheet.CheckBox2.Visible = True
    ActiveSheet.CheckBox3.Visible = True
    ActiveSheet.CheckBox4.Visible = True
    ActiveSheet.CheckBox5.Visible = True
    ActiveSheet.CheckBox6.Visible = True
    ActiveSheet.CheckBox7.Visible = True

    Else:
    [10:71].EntireRow.Hidden = True 'Hide All
    ActiveSheet.CheckBox1.Visible = False
    ActiveSheet.CheckBox2.Visible = False
    ActiveSheet.CheckBox3.Visible = False
    ActiveSheet.CheckBox4.Visible = False
    ActiveSheet.CheckBox5.Visible = False
    ActiveSheet.CheckBox6.Visible = False
    ActiveSheet.CheckBox7.Visible = False

    End If
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Hide form control checkboxes using For-Next

    For a form control checkbox you can use the shapes control to access the box. Example:

    Sub example()
    Dim ws As Worksheet:    Set ws = ActiveSheet
    Dim i As Integer
    
    For i = 1 To 5
        ws.Shapes("Check Box " & i).Visible = msoFalse
    Next i
    
    End Sub

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Hide form control checkboxes using For-Next

    Perhaps.
    Sub CheckBoxTest_Click()
    Dim I As Long
    
        For I = 1 To 7
            ActiveSheet.[10:71].EntireRow.Hidden = IIf(ActiveSheet.CheckBoxes("CheckBoxTest").Value = 1, False, True)
            ActiveSheet.CheckBoxes("Check Box " & I).Visible = IIf(ActiveSheet.CheckBoxes("CheckBoxTest").Value = 1, True, False)
        Next I
    
    End Sub

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    10-03-2014
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    13

    Re: Hide form control checkboxes using For-Next

    Thanks All,
    Ok, next problem here is when I save the worksheet with the rows containing hidden checkboxes then close and re-open. My code no longer works. The rows unhide as normal, but the checkboxes within them disapear. Ive tried all 3 object positions and get the same results. Spreadsheets works as normal when checkboxes are checked and unhidden. Again, when saving/closing/re-opening while checkboxes are hidden, they appear to lose their origin.

    I haven't the slightest clue how to fix this.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Hide form control checkboxes using For-Next

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  6. #6
    Registered User
    Join Date
    10-03-2014
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    13

    Re: Hide form control checkboxes using For-Next

    Sure thing, attached is a very simple version of whats happening.
    Once you save the workbook with a checkbox un-checked, the code appears to break down.

    This is a problem for me on a much more massive scale, but the attached worksheets are a simple example of this problem.

    Thank you so much for your support.
    Attached Files Attached Files

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Hide form control checkboxes using For-Next

    I can't recreate the problem.

    Mind you in the uploaded workboos you are using ActiveX controls, not Forms.

  8. #8
    Registered User
    Join Date
    10-03-2014
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    13

    Re: Hide form control checkboxes using For-Next

    Have you tried saving while the test checkbox is unchecked, then re-opening?
    This is when the error occurs.

  9. #9
    Registered User
    Join Date
    10-03-2014
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    13

    Re: Hide form control checkboxes using For-Next

    Hmmm, my understanding is that ActiveX controls have a bug in 2010 when hidden, which leads me to use form controls.
    Created using Office 2010.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Hide form control checkboxes using For-Next

    I can't recreate the problem but the checkboxes in the workbooks you uploaded are ActiveX, not Forms.

  11. #11
    Registered User
    Join Date
    10-03-2014
    Location
    Phoenix, Arizona
    MS-Off Ver
    2010
    Posts
    13

    Re: Hide form control checkboxes using For-Next

    I found a fix!
    So after hiding the cells, the checkboxes move to the line after the original hidden selection.
    All you have to do, is reset the original position to correct the problem.
    The fix is shown in blue below.
    Now does anybody know how to do this in a For-Next loop?

    If CheckBoxFabPanelization = True Then
    ActiveSheet.CheckBox168.Visible = True
    ActiveSheet.CheckBox169.Visible = True
    ActiveSheet.CheckBox170.Visible = True
    ActiveSheet.CheckBox171.Visible = True
    ActiveSheet.CheckBox172.Visible = True
    ActiveSheet.CheckBox173.Visible = True
    ActiveSheet.CheckBox174.Visible = True
    ActiveSheet.CheckBox175.Visible = True
    ActiveSheet.CheckBox176.Visible = True
    ActiveSheet.CheckBox177.Visible = True
    ActiveSheet.CheckBox178.Visible = True
    ActiveSheet.CheckBox179.Visible = True
    ActiveSheet.CheckBox180.Visible = True
    ActiveSheet.CheckBox181.Visible = True
    [185:198].EntireRow.Hidden = False 'Unhide All
    Else:
    ActiveSheet.CheckBox168.Visible = False
    ActiveSheet.CheckBox169.Visible = False
    ActiveSheet.CheckBox170.Visible = False
    ActiveSheet.CheckBox171.Visible = False
    ActiveSheet.CheckBox172.Visible = False
    ActiveSheet.CheckBox173.Visible = False
    ActiveSheet.CheckBox174.Visible = False
    ActiveSheet.CheckBox175.Visible = False
    ActiveSheet.CheckBox176.Visible = False
    ActiveSheet.CheckBox177.Visible = False
    ActiveSheet.CheckBox178.Visible = False
    ActiveSheet.CheckBox179.Visible = False
    ActiveSheet.CheckBox180.Visible = False
    ActiveSheet.CheckBox181.Visible = False
    [185:198].EntireRow.Hidden = True 'Hide All
    End If

    CheckBox168.Top = Range("g185").Top
    CheckBox169.Top = Range("g186").Top
    CheckBox170.Top = Range("g187").Top
    CheckBox171.Top = Range("g188").Top
    CheckBox172.Top = Range("g189").Top
    CheckBox173.Top = Range("g190").Top
    CheckBox174.Top = Range("g191").Top
    CheckBox175.Top = Range("g192").Top
    CheckBox176.Top = Range("g193").Top
    CheckBox177.Top = Range("g194").Top
    CheckBox178.Top = Range("g195").Top
    CheckBox179.Top = Range("g196").Top
    CheckBox180.Top = Range("g197").Top
    CheckBox181.Top = Range("g198").Top

+ 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] Mandatory Checkboxes (Form Control)
    By MKWilson in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-19-2014, 01:26 PM
  2. [SOLVED] Form Control Checkboxes Grouped are slow to respond.
    By serfox09 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2014, 01:15 PM
  3. How to lock down other Form Control Checkboxes when one has been selected?
    By PinkieAce in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-04-2012, 05:46 PM
  4. Clearing form control checkboxes automatically on opening workbook
    By glenin in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-08-2009, 08:20 PM
  5. [SOLVED] Hide/Unhide Form Control
    By qwerty in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-06-2006, 01:30 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