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: 8

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Kyle123

Well-known Member
Joined
Jan 24, 2012
Messages
2,714
You need to give the second textbox a different name from the first
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,129,810
Messages
5,638,483
Members
417,027
Latest member
wlknspc7

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
Top