+ Reply to Thread
Results 1 to 10 of 10

Create checkboxes automatically on userform

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Create checkboxes automatically on userform

    Hello everyone
    I need a code that enables me to create checkboxes on userform automatically ..
    The number of the checkboxes will be Sheets.Count - 1
    I mean to rename the created checkboxes with sheets' names except for activesheet

    For example: I have four sheets (Main - Report - Data -Result) .. If the Main sheet is the activesheet
    In userform intialize I need to create three checkboxes with the caption of each sheet of the other three (Report - Data -Result)

    Also I need to create a command button automatically ...
    Thanks advanced for help
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Create checkboxes automatically on userform

    Try something like this - it relies on having a commandbutton already on the userform (but it moves it to the right place...)

    Option Explicit
    
    Private Sub CreateControls()
        Dim ws As Worksheet
        Dim i As Integer
        Dim cBox As MSForms.CheckBox
        Dim iPos As Integer
        
        iPos = 5
    
        For i = 1 To Worksheets.Count
            Set ws = Worksheets(i)
            If Not ws.Name = "Main" Then
                Set cBox = Me.Controls.Add("Forms.Checkbox.1", "Checkbox" & i, True)
                With cBox
                    .Top = iPos
                    .Left = 5
                    .Width = 80
                    .Caption = Worksheets(i).Name
                End With
                iPos = iPos + 15
            End If
        Next i
        With Me.CommandButton1
            .Top = iPos + 5
            .Left = 5
            .Width = 80
            .Caption = "Button Caption"
        End With
        Me.Height = iPos + 60
        Me.Width = 30
    End Sub
    
    Private Sub CommandButton1_Click()
        Dim cb As Control
        Dim s As String
        For Each cb In Me.Controls
            If TypeName(cb) = "CheckBox" Then
                s = s & cb.Caption & vbTab & cb.Value & vbCr
            End If
        Next cb
        MsgBox s
    End Sub
    
    Private Sub UserForm_Initialize()
        Call CreateControls
    End Sub
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create checkboxes automatically on userform

    That's wonderful and awesome Mr. Olly
    Exactly as I need .. Working like charm and as expected
    Thank you very much
    Kind Regards

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create checkboxes automatically on userform

    Hello again
    I tried to edit this line
    s = s & cb.Caption & vbTab & cb.Value & vbCr
    to be like that
                If CB.Value = True Then
                    MsgBox CB.Caption
                End If
    I checked two of the three checkboxes and it is supposed to display just two messages but I received three messages (two with CB caption and an empty message)
    Why I got this empty message?

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Create checkboxes automatically on userform

    Quote Originally Posted by YasserKhalil View Post
    Hello again
    I tried to edit this line
    s = s & cb.Caption & vbTab & cb.Value & vbCr
    to be like that
                If CB.Value = True Then
                    MsgBox CB.Caption
                End If
    I checked two of the three checkboxes and it is supposed to display just two messages but I received three messages (two with CB caption and an empty message)
    Why I got this empty message?
    Did you delete the final msgbox line:
        Next cb
        MsgBox s
    End Sub

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

    Re: Create checkboxes automatically on userform

    The coding of created controls can be problematic.
    Rather than making checkboxes at run-time, have you considered using a multi-select ListBox with the .ListStyle set to fmListStyleOption?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create checkboxes automatically on userform

    Thank you very much Mr. Mike
    In fact I am so weak at dealing with forms .. Can you provide me a solution to create listbox with sheet names except for active sheet ..
    And enables me to select multiplce choices from the list ..
    Thanks a lot for advanced help

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Create checkboxes automatically on userform

    Quote Originally Posted by YasserKhalil View Post
    Thank you very much Mr. Mike
    In fact I am so weak at dealing with forms .. Can you provide me a solution to create listbox with sheet names except for active sheet ..
    And enables me to select multiplce choices from the list ..
    Thanks a lot for advanced help
    Here's code which will work for a userform with one listbox (ListBox1) and one Command Button (CommandButton1), listing all worksheets (except [Main] in the listbox, and allowing multiple selections:

    Private Sub CreateList()
        Dim ws As Worksheet
        Dim i As Integer
        Dim iPos As Integer
    
        Me.ListBox1.MultiSelect = fmMultiSelectMulti
        
        For Each ws In ThisWorkbook.Worksheets
            If Not ws.Name = "Main" Then
                Me.ListBox1.AddItem ws.Name
            End If
        Next ws
    End Sub
    
    Private Sub CommandButton1_Click()
        Dim i As Integer
        With Me.ListBox1
            If .ListIndex <> -1 Then
                For i = 0 To .ListCount - 1
                    If .Selected(i) Then MsgBox .List(i)
                Next i
            End If
        End With
    End Sub
    
    Private Sub UserForm_Initialize()
        Call CreateList
    End Sub

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create checkboxes automatically on userform

    Thanks a lot Mr. Olly
    I'm sorry I didn't notice it ... you are completely right ..
    Now It is working well
    Thanks again and sorry for disturbing you
    Best Regards

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create checkboxes automatically on userform

    Really awesome awesome awesome
    Thank you very much for this pretty gift .. I liked it a lot
    Thanks again for this unique help
    Regards

+ 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. Replies: 1
    Last Post: 08-31-2015, 09:44 AM
  2. VBA Userform: Warn user if all checkboxes on a userform are unticked.
    By MrMiyagi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2014, 12:08 AM
  3. Userform Checkboxes!
    By shredraldz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-22-2013, 10:35 AM
  4. Print Sheets based on checkboxes in UserForm, Checkboxes also control another macro
    By krackaberr in forum Excel Programming / VBA / Macros
    Replies: 34
    Last Post: 03-05-2013, 11:12 AM
  5. Automatically create checkboxes?
    By devon1215 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2011, 01:01 PM
  6. Dynamically create and Place Controls such as CheckBoxes,Text-Boxes in a UserForm
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 35
    Last Post: 05-11-2011, 11:34 AM
  7. insert elemnts from cells into array, create a userform with values checkboxes
    By usadream in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-31-2006, 11:30 AM

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