How to read a text from dynamic added TextBox?

PKwasu96

New Member
Joined
Mar 14, 2021
Messages
2
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I have one question about how to read a text from dynamically added TextBox. In my UserForm I have one TextBox and in this TextBox I put a number of how much new TextBoxes I want add. From this one TextBox I add two new dynamic TextBox.
VBA Code:
If ComboAdd Then
Set Ctrl = .Add("Forms.TextBox.1", Window.Name + CStr(i)) 'TextBox for code
With Ctrl
    .Width = 32
    .Height = 16
    .Top = TopPos
    .Left = 6
    .Value = Chr(64 + i) + CStr(10 + i)
    .MaxLength = 4
    .ZOrder (0)
End With
Set Ctrl = .Add("Forms.TextBox.1", Window.Name + CStr(i)) 'TextBox for comment
With Ctrl
    .Width = 240
    .Height = 16
    .Top = TopPos
    .Left = 44
    .MaxLength = 50
    .ZOrder (0)
End With
End If

First column of TextBoxes is filling automatically with a letter and a number, a second column is empty for my comments. With Finish button I want to export text from both TextBoxes to a two cells - text from first column of TextBoxes to a cell "A" and text from second column of TextBoxes to a cell "B" in new file. I wrote a function to find a text in each of new TextBoxes:
VBA Code:
Function FindName(Iter As Integer, Name As String) As String
   Dim Text As String
   Dim Ctrl As Control

   For Each Ctrl In UserForm1.Controls
   If Ctrl.Name = Name Then
      Text = Ctrl.Text
   End If
   Next Ctrl

FindName = Text

End Function

I use this function to fill up a new Excel with text from TextBoxes, but the problem is, that a text is exported only from the second column to a new file:
VBA Code:
NewFile.Worksheets(1).Cells(StartValue + i, 1) = FindName(i, "TextBox1" + CStr(i))
NewFile.Worksheets(1).Cells(StartValue + i, 2) = FindName(i, "TextBox1" + CStr(i))

Is there any solution how to distinguish the text from first column of TextBoxes and from second column of TextBoxes to export a text from first TextBox to one cell and from second TextBox to another cell?

Thank you for the help.
 

Attachments

  • UserForm.JPG
    UserForm.JPG
    44.9 KB · Views: 25

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You need to give the second textbox a different name from the first
 
Upvote 0
Solution
Two "more useful" solutions...
The first A1, E2..., could be added as Label. That overcomes the problem, stops the user tabbing or attempting to change, etc, etc.

The second is more general in allowing more flexibility:
As a matter of course, I tend to identify added controls by their TAG rather than their NAME. The TAG allows more variation. Hence, you may (were you using grid-like textboxes allowing two entries per line, with use of TAG, you could set the tag to "E5,1" and "E5,2", and then use the Split function on the tag to identify which grid-column you were working with. Can't do that with 'Name'.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,861
Members
449,052
Latest member
Fuddy_Duddy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top