Reputation: 151
I have a Userform where it auto creates labels and text boxes. The problem is after created I don't know the name of text box 1. I was thinking it would be TextBox1 but it isn't. How would I go about naming them TextBox1 then TextBox2 and so on? Code in question is under "AddLine".
Sub CommandButton1_Click()
Dim TBs(9) As Object
Set TBs(0) = TextBox1: Set TBs(1) = TextBox2: Set TBs(2) = TextBox3
Set TBs(3) = TextBox4: Set TBs(4) = TextBox5: Set TBs(5) = TextBox6
Set TBs(6) = TextBox7: Set TBs(7) = TextBox8: Set TBs(8) = TextBox9
Set TBs(9) = TextBox10:
For i = 0 To Amount
With ActiveDocument
If .Bookmarks("href" & i + 1).Range = ".jpg" Then
.Bookmarks("href" & i + 1).Range _
.InsertBefore TBs(i)
.Bookmarks("src" & i + 1).Range _
.InsertBefore TBs(i)
.Bookmarks("alt" & i + 1).Range _
.InsertBefore TBs(i)
End If
End With
Next
UserForm1.Hide
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".jpg "
.Replacement.Text = ".jpg"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.HomeKey Unit:=wdLine
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "/ "
.Replacement.Text = "/"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.HomeKey Unit:=wdLine
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".jpg.jpg"
.Replacement.Text = ".jpg"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.HomeKey Unit:=wdLine
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Private Sub AddLine_Click()
Dim theTextbox As Object
Dim textboxCounter As Long
For textboxCounter = 1 To Amount
Set theTextbox = UserForm1.Controls.Add("Forms.TextBox.1", "test" & textboxCounter, True)
With theTextbox
.Width = 200
.Left = 70
.Top = 30 * textboxCounter
End With
Next
Dim theLabel As Object
Dim labelCounter As Long
For labelCounter = 1 To Amount
Set theLabel = UserForm1.Controls.Add("Forms.Label.1", "Test" & labelCounter, True)
With theLabel
.Caption = "Image" & labelCounter
.Left = 20
.Width = 50
.Top = 30 * labelCounter
End With
With UserForm1
.Height = Amount * 30 + 100
End With
With CommandButton1
.Top = Amount * 30 + 40
End With
With CommandButton2
.Top = Amount * 30 + 40
End With
Next
End Sub
Upvotes: 0
Views: 585
Reputation: 53623
Your code is already giving it names like test1
, test2
, etc., that is the second parameter in the .Add
method:
Set theTextbox = UserForm1.Controls.Add("Forms.TextBox.1", "test" & textboxCounter, True)
Intellisense shows this pretty clearly:
If you don't like that, I think you can always change it from the .Name
property:
Simply assign it a name while it's being added.
For textboxCounter = 1 To Amount
Set theTextbox = UserForm1.Controls.Add("Forms.TextBox.1", "test" & textboxCounter, True)
With theTextbox
.Name = "TextBox_" & i
.Width = 200
.Left = 70
.Top = 30 * textboxCounter
End With
Next
UPDATE FROM COMMENTS
So you have this assignment here:
Set TBs(0) = TextBox1: Set TBs(1) = TextBox2: Set TBs(2) = TextBox3
Set TBs(3) = TextBox4: Set TBs(4) = TextBox5: Set TBs(5) = TextBox6
Set TBs(6) = TextBox7: Set TBs(7) = TextBox8: Set TBs(8) = TextBox9
Set TBs(9) = TextBox10:
Use of Option Explicit
at the top of your module would have warned you about these. When you run this, if you would have taken normal steps to debug, you should see that TBs(0)
, etc., are all assigned Nothing
. (And then you could have asked a better question: "Why are my variables all Nothing
?", etc... I mention these things not to lambaste you but to show you how to solve your own issues (or at least get the pertinent information so you may ask better questions in the future!
Why?
Because there is no such object in scope as TextBox1
, etc.
Why not?
Because you do not create the textboxes until later in your code, so at the time of assignment, that array is holding all Nothing
.
how to fix it?
I do not think you can refer to controls created at run-time in this way, because the code generally will not compile (again, Option Explicit
would've alerted you to this; you should ALWAYS ALWAYS ALWAYS use this at the top of each module).
You should be able to assign like:
Set TBs(0) = UserForm1.Controls("TextBox_1")
, etc. Make sure that you use the correct name as assigned in the .Add
method (or as specified in the .Name
property when you add the Textboxes to the form.
NOTE You must make this assignment after the TextBox(es) have been added to the UserForm, otherwise an error will occur.
Good luck!
Upvotes: 2