?
Solved

How to create dynamic check boxes in VBA

Posted on 2014-03-26
8
Medium Priority
?
594 Views
Last Modified: 2014-04-06
I have a userform that will contain text strings.  The text strings will be kept in a separate document so the strings can be added to, deleted or modified.

I won't know how many of them there will be or how many the user will want to choose, so I want to create a check box with one of the strings next to it, and continue that until all text strings and check boxes have been created.

That will allow the user to choose as many text strings as they need.

What I need is the code to accomplish the creation of the text boxes with the associated strings

Thanks for the help
0
Comment
Question by:c7c4c7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 39958200
The scenario wasn't too precise but to demonstrate I have made some assumptions:
The strings documdent might be open but if not open it for the purpose
The strings document is a defined document i.e. not necessarily the active one
The document containing the macro is the current one
The strings are entered as paragraphs

On this assumption then try:

Sub Q_28398555()
Dim doc As Document
Dim bolFound As Boolean
Dim arrStrings As Variant
Dim frm As UserForm
Dim intCount As Integer
Const strDocName = "Q_28398555.docx"
Const strdocPath = "C:\"

    bolFound = False
    For Each doc In Application.Documents
        If LCase(doc.Name) = LCase(strDocName) Then
            bolFound = True
            Exit For
        End If
        Set doc = Nothing
    Next
    If Not bolFound Then Set doc = Application.Documents.Open(strdocPath & strDocName)
    arrStrings = Split(doc.Range.Text, vbCr)
    Load UserForm1
        For intCount = LBound(arrStrings) To UBound(arrStrings)
            If arrStrings(intCount) <> "" Then
                With UserForm1.Controls.Add("Forms.CheckBox.1", "Checkbox_" & intCount, True)
                    .Top = 20 * intCount
                    .Caption = arrStrings(intCount)
                End With
            End If
        Next
    UserForm1.Show
End Sub

Open in new window


Chris
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39958209
Hi c7c4c7 - Rename zip to dotm.
UserFormExample--II.zip
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39958217
Hi Chris,

Hope you're well. You must have posted just as I started :-)   I think this question relates to an earlier question.
0
New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39958231
Hi yourself, I have seen you around a few times but Feb/March tends to be a quiet period for me.

I take your point that it was an ongoing question but there was no mention to this in the question and as a passing comment I understood the number of lines of text was an unknown that will vary in use therefore your solution looks (to my interpretation) as though it doesn't quite hit the spot ;o)

Chris
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39958248
I only mentioned it to give some BG info, I agree with your assessment - mine is a bit to the left of center.

With clarificaiton from the OP I'm thinking a userform could work if it is driven by a macro that loops through the rows in a table. Or tables in a document.

Just providing a starting spot.

Cheers,
0
 

Author Comment

by:c7c4c7
ID: 39959265
I haven't had a chance to work with the responses.  Dr. Tribos is correct it is an extension of the previous question.  

I won't have time to work on this until later today but thanks for the posts
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39960273
No worries - let us know how you go
0
 

Author Closing Comment

by:c7c4c7
ID: 39981413
Thanks for the help
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question