Solved

How to create dynamic check boxes in VBA

Posted on 2014-03-26
8
588 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 500 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
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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