+ Reply to Thread
Results 1 to 5 of 5

Hide/show columns if checkbox (UserForm) is TRUE

Hybrid View

  1. #1
    Registered User
    Join Date
    02-05-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    Excel 2010, Excel 2016
    Posts
    72

    Hide/show columns if checkbox (UserForm) is TRUE

    Hi everybody,

    I'm newcomer into the field of VBA programming and I'm currently trying to figure out this task (for most of you probably easy to solve):

    1. load UserForm with 12 checkboxes when one can check criteria he wants
    2. macro should then, based on the user's option, hide/show all relevant columns containing detailed information.


    Private Sub CommandButton1_Click()
    
           For x = 1 To 12
    
            CheckboxX = "CheckBox" & x
    
                y = 6 + x
    
                ActiveWorkbook.Worksheets("Zamėstnanec 1").Activate
    
                If CheckboxX = True Then
    
                    Columns(y).EntireColumn.Hidden = False
    
                Else
    
                    Columns(y).EntireColumn.Hidden = True
    
                End If
    
            Next
    
    
       Unload UserForm1
    
    End Sub
    When I run the macro, it works without breaking down, but it always hide all of the columns. Could you please help me out with it?

    Thank you so much for help!
    Last edited by lukelucky; 12-15-2014 at 06:09 AM.

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Hide/show columns if checkbox (UserForm) is TRUE

    Can you upload your workbook?
    If someone has helped you then please add to their Reputation

  3. #3
    Registered User
    Join Date
    02-05-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    Excel 2010, Excel 2016
    Posts
    72

    Re: Hide/show columns if checkbox (UserForm) is TRUE

    thanks for your response!

    I'm adding the file where all the classified data have been removed. The main idea about this macro is:

    1. User clicks the button and selects the items
    2. Based on the selection, only relevant columns remain on unhidded.

    Thanks for any suggestion!



    Excel_forum, 15 12 2014 FL.xlsm

  4. #4
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Hide/show columns if checkbox (UserForm) is TRUE

    You werent actually checking the value of the checkboxes, the following line:
    CheckboxX = "CheckBox" & x
    simply created a variable and populated it with "Checkbox1", "Checkbox2" etc which never equals True.

    You need to cycle through the controls in the form and action accordingly.

    Private Sub CommandButton1_Click()
    Dim cCont As Control
        For Each cCont In Me.Controls 'cycle through all elements on the form allocating each in turn to the object "cCont"
            If (Left(cCont.Name, 5) = "Check") Then 'check if name of object begins with "Check", if so its a checkbox
                y = Int(Right(cCont.Name, Len(cCont.Name) - 8)) + 6 'strip off the word "Checkbox" to leave the number
                    If (cCont.Value = True) Then ' check if its value is True
                        Columns(y).EntireColumn.Hidden = False
                    Else
                        Columns(y).EntireColumn.Hidden = True
                    End If
            End If
        Next cCont
    Unload UserForm1
    End Sub

  5. #5
    Registered User
    Join Date
    02-05-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    Excel 2010, Excel 2016
    Posts
    72

    Re: Hide/show columns if checkbox (UserForm) is TRUE

    Thank you pjwhitfield, that was exactly what I needed! Works perfectly!

+ 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] Rows to hide/unhide when checkbox = true/false
    By PDBartlett in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 05-06-2014, 10:40 AM
  2. How to have checkbox stay True when userform is closed and reopened
    By Gal403 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-02-2014, 12:57 PM
  3. Userform Checkbox as a value when true
    By lcartwright in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-21-2014, 12:42 PM
  4. Show/Hide columns by checkbox and cell text
    By ksmoore in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2010, 02:20 PM
  5. Can't get checkbox to go true from another userform
    By mhill in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-14-2009, 12:10 PM

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