Solved

How to create dynamic check boxes in VBA

Posted on 2014-03-26
8
573 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

773 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