none
Text Boxes on User Form at Runtime RRS feed

  • Question

  • I have used some code from another source to generate a user defined number of Textboxes onto a Userform at runtime. At the same time the same number of cells in Column A Rows 4,5,6 onwards on the Worksheet "Quiz" populate with text. How do I populate generated Textboxes with the corresponding text from the Worksheet?

    The code used to generate the Textboxes is:

    Dim NumberofQuestions As Long
    Dim txtB1 As Control
    Dim i As Long
    
    NumberofQuestions = worksheets("Quiz").Range("F1")
    For i = 1 To number
    Set txtB1 = Controls.Add(“Forms.TextBox.1”)
    With txtB1
    .Name = txtBox & i
    .Height = 20
    .Width = 150
    .Left = 20
    .Top = 220 + i * 20
    End With
    Next i
    End Sub

    Thanks

    Steve

    Friday, March 8, 2019 3:30 PM

Answers

  • I'd leave it as it is, the code in this post has the same effect. If you want to add some space between the text boxes, do it like this. The following example will add 6 points spacing between the text boxes.

    .Top = 220 + i * (20 + 6)


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Stirlingmw Sunday, March 10, 2019 6:33 PM
    Sunday, March 10, 2019 10:19 AM

All replies

  • Above End With, insert

    .Text = Worksheets("Quiz").Range("A" & i + 3).Value


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, March 8, 2019 5:39 PM
  • Hans

    As always awesome thank you.

    Is there a way I can incorporate something similar to your code detailed on another post of mine which positions the auto generated autosized text boxes above each other as opposed to overlapping.

    The code you detailed was Me.TextBox2.Top = Me.TextBox1.Top + Me.TextBox1.Height + 6

    Thanks

    Saturday, March 9, 2019 10:44 PM
  • I'd leave it as it is, the code in this post has the same effect. If you want to add some space between the text boxes, do it like this. The following example will add 6 points spacing between the text boxes.

    .Top = 220 + i * (20 + 6)


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Stirlingmw Sunday, March 10, 2019 6:33 PM
    Sunday, March 10, 2019 10:19 AM
  • Again Thanks,

    Is there a definitive list of controls that can be added at runtime i.e. a scrollbar, fontsize?

    Steve

    Sunday, March 10, 2019 6:33 PM
  • Hans

    I have sorted it.

    .ScrollBars = 2

    .FontSize = 10

    Thanks

    Sunday, March 10, 2019 6:44 PM